Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / spdy / spdy_stream_test_util.cc
blob7e10e1cede380c275e164392134b03bc22eaf702
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"
7 #include <cstddef>
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"
14 namespace net {
16 namespace test {
18 ClosingDelegate::ClosingDelegate(
19 const base::WeakPtr<SpdyStream>& stream) : stream_(stream) {
20 DCHECK(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) {
39 DCHECK(stream_);
40 stream_->Close();
41 // The |stream_| may still be alive (if it is our delegate).
44 StreamDelegateBase::StreamDelegateBase(
45 const base::WeakPtr<SpdyStream>& stream)
46 : stream_(stream),
47 stream_id_(0),
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) {
68 if (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) {
77 if (!stream_.get())
78 return;
79 stream_id_ = stream_->stream_id();
80 stream_.reset();
81 callback_.callback().Run(status);
84 int StreamDelegateBase::WaitForClose() {
85 int result = callback_.WaitForResult();
86 EXPECT_TRUE(!stream_.get());
87 return result;
90 std::string StreamDelegateBase::TakeReceivedData() {
91 size_t len = received_data_queue_.GetTotalSize();
92 std::string received_data(len, '\0');
93 if (len > 0) {
94 EXPECT_EQ(
95 len,
96 received_data_queue_.Dequeue(string_as_array(&received_data), len));
98 return received_data;
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),
118 data_(data) {}
120 StreamDelegateSendImmediate::~StreamDelegateSendImmediate() {
123 SpdyResponseHeadersStatus StreamDelegateSendImmediate::OnResponseHeadersUpdated(
124 const SpdyHeaderBlock& response_headers) {
125 SpdyResponseHeadersStatus status =
126 StreamDelegateBase::OnResponseHeadersUpdated(response_headers);
127 if (data_.data()) {
128 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(data_.as_string()));
129 stream()->SendData(buf.get(), buf->size(), MORE_DATA_TO_SEND);
131 return status;
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) {
159 stream()->Cancel();
160 return RESPONSE_HEADERS_ARE_COMPLETE;
163 } // namespace test
165 } // namespace net