Re-land: C++ readability review
[chromium-blink-merge.git] / net / spdy / spdy_stream_test_util.cc
blob4cd526467763385c8a9ff9ae196c2416480571e3
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::OnClose(int status) {
37 DCHECK(stream_);
38 stream_->Close();
39 // The |stream_| may still be alive (if it is our delegate).
42 StreamDelegateBase::StreamDelegateBase(
43 const base::WeakPtr<SpdyStream>& stream)
44 : stream_(stream),
45 stream_id_(0),
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) {
66 if (buffer)
67 received_data_queue_.Enqueue(buffer.Pass());
70 void StreamDelegateBase::OnDataSent() {}
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);
80 int StreamDelegateBase::WaitForClose() {
81 int result = callback_.WaitForResult();
82 EXPECT_TRUE(!stream_.get());
83 return result;
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));
94 return received_data;
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),
114 data_(data) {}
116 StreamDelegateSendImmediate::~StreamDelegateSendImmediate() {
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);
127 return status;
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);
144 StreamDelegateCloseOnHeaders::StreamDelegateCloseOnHeaders(
145 const base::WeakPtr<SpdyStream>& stream)
146 : StreamDelegateBase(stream) {
149 StreamDelegateCloseOnHeaders::~StreamDelegateCloseOnHeaders() {
152 SpdyResponseHeadersStatus
153 StreamDelegateCloseOnHeaders::OnResponseHeadersUpdated(
154 const SpdyHeaderBlock& response_headers) {
155 stream()->Cancel();
156 return RESPONSE_HEADERS_ARE_COMPLETE;
159 } // namespace test
161 } // namespace net