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.
5 #include "net/spdy/spdy_stream_test_util.h"
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"
18 ClosingDelegate::ClosingDelegate(
19 const base::WeakPtr
<SpdyStream
>& stream
) : stream_(stream
) {
23 ClosingDelegate::~ClosingDelegate() {}
25 void ClosingDelegate::OnRequestHeadersSent() {}
27 SpdyResponseHeadersStatus
ClosingDelegate::OnResponseHeadersUpdated(
28 const SpdyHeaderBlock
& response_headers
) {
29 return RESPONSE_HEADERS_ARE_COMPLETE
;
32 void ClosingDelegate::OnDataReceived(scoped_ptr
<SpdyBuffer
> buffer
) {}
34 void ClosingDelegate::OnDataSent() {}
36 void ClosingDelegate::OnTrailers(const SpdyHeaderBlock
& trailers
) {}
38 void ClosingDelegate::OnClose(int status
) {
41 // The |stream_| may still be alive (if it is our delegate).
44 StreamDelegateBase::StreamDelegateBase(
45 const base::WeakPtr
<SpdyStream
>& stream
)
48 send_headers_completed_(false) {
51 StreamDelegateBase::~StreamDelegateBase() {
54 void StreamDelegateBase::OnRequestHeadersSent() {
55 stream_id_
= stream_
->stream_id();
56 EXPECT_NE(stream_id_
, 0u);
57 send_headers_completed_
= true;
60 SpdyResponseHeadersStatus
StreamDelegateBase::OnResponseHeadersUpdated(
61 const SpdyHeaderBlock
& response_headers
) {
62 EXPECT_EQ(stream_
->type() != SPDY_PUSH_STREAM
, send_headers_completed_
);
63 response_headers_
= response_headers
;
64 return RESPONSE_HEADERS_ARE_COMPLETE
;
67 void StreamDelegateBase::OnDataReceived(scoped_ptr
<SpdyBuffer
> buffer
) {
69 received_data_queue_
.Enqueue(buffer
.Pass());
72 void StreamDelegateBase::OnDataSent() {}
74 void StreamDelegateBase::OnTrailers(const SpdyHeaderBlock
& trailers
) {}
76 void StreamDelegateBase::OnClose(int status
) {
79 stream_id_
= stream_
->stream_id();
81 callback_
.callback().Run(status
);
84 int StreamDelegateBase::WaitForClose() {
85 int result
= callback_
.WaitForResult();
86 EXPECT_TRUE(!stream_
.get());
90 std::string
StreamDelegateBase::TakeReceivedData() {
91 size_t len
= received_data_queue_
.GetTotalSize();
92 std::string
received_data(len
, '\0');
96 received_data_queue_
.Dequeue(string_as_array(&received_data
), len
));
101 std::string
StreamDelegateBase::GetResponseHeaderValue(
102 const std::string
& name
) const {
103 SpdyHeaderBlock::const_iterator it
= response_headers_
.find(name
);
104 return (it
== response_headers_
.end()) ? std::string() : it
->second
;
107 StreamDelegateDoNothing::StreamDelegateDoNothing(
108 const base::WeakPtr
<SpdyStream
>& stream
)
109 : StreamDelegateBase(stream
) {}
111 StreamDelegateDoNothing::~StreamDelegateDoNothing() {
114 StreamDelegateSendImmediate::StreamDelegateSendImmediate(
115 const base::WeakPtr
<SpdyStream
>& stream
,
116 base::StringPiece data
)
117 : StreamDelegateBase(stream
),
120 StreamDelegateSendImmediate::~StreamDelegateSendImmediate() {
123 SpdyResponseHeadersStatus
StreamDelegateSendImmediate::OnResponseHeadersUpdated(
124 const SpdyHeaderBlock
& response_headers
) {
125 SpdyResponseHeadersStatus status
=
126 StreamDelegateBase::OnResponseHeadersUpdated(response_headers
);
128 scoped_refptr
<StringIOBuffer
> buf(new StringIOBuffer(data_
.as_string()));
129 stream()->SendData(buf
.get(), buf
->size(), MORE_DATA_TO_SEND
);
134 StreamDelegateWithBody::StreamDelegateWithBody(
135 const base::WeakPtr
<SpdyStream
>& stream
,
136 base::StringPiece data
)
137 : StreamDelegateBase(stream
),
138 buf_(new StringIOBuffer(data
.as_string())) {}
140 StreamDelegateWithBody::~StreamDelegateWithBody() {
143 void StreamDelegateWithBody::OnRequestHeadersSent() {
144 StreamDelegateBase::OnRequestHeadersSent();
145 stream()->SendData(buf_
.get(), buf_
->size(), NO_MORE_DATA_TO_SEND
);
148 StreamDelegateCloseOnHeaders::StreamDelegateCloseOnHeaders(
149 const base::WeakPtr
<SpdyStream
>& stream
)
150 : StreamDelegateBase(stream
) {
153 StreamDelegateCloseOnHeaders::~StreamDelegateCloseOnHeaders() {
156 SpdyResponseHeadersStatus
157 StreamDelegateCloseOnHeaders::OnResponseHeadersUpdated(
158 const SpdyHeaderBlock
& response_headers
) {
160 return RESPONSE_HEADERS_ARE_COMPLETE
;