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::OnClose(int status
) {
39 // The |stream_| may still be alive (if it is our delegate).
42 StreamDelegateBase::StreamDelegateBase(
43 const base::WeakPtr
<SpdyStream
>& stream
)
46 send_headers_completed_(false) {
49 StreamDelegateBase::~StreamDelegateBase() {
52 void StreamDelegateBase::OnRequestHeadersSent() {
53 stream_id_
= stream_
->stream_id();
54 EXPECT_NE(stream_id_
, 0u);
55 send_headers_completed_
= true;
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
;
65 void StreamDelegateBase::OnDataReceived(scoped_ptr
<SpdyBuffer
> buffer
) {
67 received_data_queue_
.Enqueue(buffer
.Pass());
70 void StreamDelegateBase::OnDataSent() {}
72 void StreamDelegateBase::OnClose(int status
) {
75 stream_id_
= stream_
->stream_id();
77 callback_
.callback().Run(status
);
80 int StreamDelegateBase::WaitForClose() {
81 int result
= callback_
.WaitForResult();
82 EXPECT_TRUE(!stream_
.get());
86 std::string
StreamDelegateBase::TakeReceivedData() {
87 size_t len
= received_data_queue_
.GetTotalSize();
88 std::string
received_data(len
, '\0');
92 received_data_queue_
.Dequeue(string_as_array(&received_data
), len
));
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
;
103 StreamDelegateDoNothing::StreamDelegateDoNothing(
104 const base::WeakPtr
<SpdyStream
>& stream
)
105 : StreamDelegateBase(stream
) {}
107 StreamDelegateDoNothing::~StreamDelegateDoNothing() {
110 StreamDelegateSendImmediate::StreamDelegateSendImmediate(
111 const base::WeakPtr
<SpdyStream
>& stream
,
112 base::StringPiece data
)
113 : StreamDelegateBase(stream
),
116 StreamDelegateSendImmediate::~StreamDelegateSendImmediate() {
119 SpdyResponseHeadersStatus
StreamDelegateSendImmediate::OnResponseHeadersUpdated(
120 const SpdyHeaderBlock
& response_headers
) {
121 SpdyResponseHeadersStatus status
=
122 StreamDelegateBase::OnResponseHeadersUpdated(response_headers
);
124 scoped_refptr
<StringIOBuffer
> buf(new StringIOBuffer(data_
.as_string()));
125 stream()->SendData(buf
.get(), buf
->size(), MORE_DATA_TO_SEND
);
130 StreamDelegateWithBody::StreamDelegateWithBody(
131 const base::WeakPtr
<SpdyStream
>& stream
,
132 base::StringPiece data
)
133 : StreamDelegateBase(stream
),
134 buf_(new StringIOBuffer(data
.as_string())) {}
136 StreamDelegateWithBody::~StreamDelegateWithBody() {
139 void StreamDelegateWithBody::OnRequestHeadersSent() {
140 StreamDelegateBase::OnRequestHeadersSent();
141 stream()->SendData(buf_
.get(), buf_
->size(), NO_MORE_DATA_TO_SEND
);