OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/spdy/spdy_stream_test_util.h" | |
6 | |
7 #include <cstddef> | |
8 | |
9 #include "base/stl_util.h" | |
10 #include "net/base/completion_callback.h" | |
11 #include "net/spdy/spdy_stream.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace net { | |
15 | |
16 namespace test { | |
17 | |
18 ClosingDelegate::ClosingDelegate( | |
19 const base::WeakPtr<SpdyStream>& stream) : stream_(stream) { | |
20 DCHECK(stream_); | |
21 } | |
22 | |
23 ClosingDelegate::~ClosingDelegate() {} | |
24 | |
25 void ClosingDelegate::OnRequestHeadersSent() {} | |
26 | |
27 SpdyResponseHeadersStatus ClosingDelegate::OnResponseHeadersUpdated( | |
28 const SpdyHeaderBlock& response_headers) { | |
29 return RESPONSE_HEADERS_ARE_COMPLETE; | |
30 } | |
31 | |
32 void ClosingDelegate::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) {} | |
33 | |
34 void ClosingDelegate::OnDataSent() {} | |
35 | |
36 void ClosingDelegate::OnClose(int status) { | |
37 DCHECK(stream_); | |
38 stream_->Close(); | |
39 // The |stream_| may still be alive (if it is our delegate). | |
40 } | |
41 | |
42 StreamDelegateBase::StreamDelegateBase( | |
43 const base::WeakPtr<SpdyStream>& stream) | |
44 : stream_(stream), | |
45 stream_id_(0), | |
46 send_headers_completed_(false) { | |
47 } | |
48 | |
49 StreamDelegateBase::~StreamDelegateBase() { | |
50 } | |
51 | |
52 void StreamDelegateBase::OnRequestHeadersSent() { | |
53 stream_id_ = stream_->stream_id(); | |
54 EXPECT_NE(stream_id_, 0u); | |
55 send_headers_completed_ = true; | |
56 } | |
57 | |
58 SpdyResponseHeadersStatus StreamDelegateBase::OnResponseHeadersUpdated( | |
59 const SpdyHeaderBlock& response_headers) { | |
60 EXPECT_EQ(stream_->type() != SPDY_PUSH_STREAM, send_headers_completed_); | |
61 response_headers_ = response_headers; | |
62 return RESPONSE_HEADERS_ARE_COMPLETE; | |
63 } | |
64 | |
65 void StreamDelegateBase::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) { | |
66 if (buffer) | |
67 received_data_queue_.Enqueue(buffer.Pass()); | |
68 } | |
69 | |
70 void StreamDelegateBase::OnDataSent() {} | |
71 | |
72 void StreamDelegateBase::OnClose(int status) { | |
73 if (!stream_.get()) | |
74 return; | |
75 stream_id_ = stream_->stream_id(); | |
76 stream_.reset(); | |
77 callback_.callback().Run(status); | |
78 } | |
79 | |
80 int StreamDelegateBase::WaitForClose() { | |
81 int result = callback_.WaitForResult(); | |
82 EXPECT_TRUE(!stream_.get()); | |
83 return result; | |
84 } | |
85 | |
86 std::string StreamDelegateBase::TakeReceivedData() { | |
87 size_t len = received_data_queue_.GetTotalSize(); | |
88 std::string received_data(len, '\0'); | |
89 if (len > 0) { | |
90 EXPECT_EQ( | |
91 len, | |
92 received_data_queue_.Dequeue(string_as_array(&received_data), len)); | |
93 } | |
94 return received_data; | |
95 } | |
96 | |
97 std::string StreamDelegateBase::GetResponseHeaderValue( | |
98 const std::string& name) const { | |
99 SpdyHeaderBlock::const_iterator it = response_headers_.find(name); | |
100 return (it == response_headers_.end()) ? std::string() : it->second; | |
101 } | |
102 | |
103 StreamDelegateDoNothing::StreamDelegateDoNothing( | |
104 const base::WeakPtr<SpdyStream>& stream) | |
105 : StreamDelegateBase(stream) {} | |
106 | |
107 StreamDelegateDoNothing::~StreamDelegateDoNothing() { | |
108 } | |
109 | |
110 StreamDelegateSendImmediate::StreamDelegateSendImmediate( | |
111 const base::WeakPtr<SpdyStream>& stream, | |
112 base::StringPiece data) | |
113 : StreamDelegateBase(stream), | |
114 data_(data) {} | |
115 | |
116 StreamDelegateSendImmediate::~StreamDelegateSendImmediate() { | |
117 } | |
118 | |
119 SpdyResponseHeadersStatus StreamDelegateSendImmediate::OnResponseHeadersUpdated( | |
120 const SpdyHeaderBlock& response_headers) { | |
121 SpdyResponseHeadersStatus status = | |
122 StreamDelegateBase::OnResponseHeadersUpdated(response_headers); | |
123 if (data_.data()) { | |
124 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(data_.as_string())); | |
125 stream()->SendData(buf.get(), buf->size(), MORE_DATA_TO_SEND); | |
126 } | |
127 return status; | |
128 } | |
129 | |
130 StreamDelegateWithBody::StreamDelegateWithBody( | |
131 const base::WeakPtr<SpdyStream>& stream, | |
132 base::StringPiece data) | |
133 : StreamDelegateBase(stream), | |
134 buf_(new StringIOBuffer(data.as_string())) {} | |
135 | |
136 StreamDelegateWithBody::~StreamDelegateWithBody() { | |
137 } | |
138 | |
139 void StreamDelegateWithBody::OnRequestHeadersSent() { | |
140 StreamDelegateBase::OnRequestHeadersSent(); | |
141 stream()->SendData(buf_.get(), buf_->size(), NO_MORE_DATA_TO_SEND); | |
142 } | |
143 | |
144 StreamDelegateCloseOnHeaders::StreamDelegateCloseOnHeaders( | |
145 const base::WeakPtr<SpdyStream>& stream) | |
146 : StreamDelegateBase(stream) { | |
147 } | |
148 | |
149 StreamDelegateCloseOnHeaders::~StreamDelegateCloseOnHeaders() { | |
150 } | |
151 | |
152 SpdyResponseHeadersStatus | |
153 StreamDelegateCloseOnHeaders::OnResponseHeadersUpdated( | |
154 const SpdyHeaderBlock& response_headers) { | |
155 stream()->Cancel(); | |
156 return RESPONSE_HEADERS_ARE_COMPLETE; | |
157 } | |
158 | |
159 } // namespace test | |
160 | |
161 } // namespace net | |
OLD | NEW |