summaryrefslogtreecommitdiffstats
path: root/net/spdy/spdy_stream_test_util.cc
blob: b434f3e6c2ecb5d177651f579ad522690d0eb545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/spdy/spdy_stream_test_util.h"

#include <cstddef>

#include "base/stl_util.h"
#include "net/base/completion_callback.h"
#include "net/spdy/spdy_stream.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace test {

ClosingDelegate::ClosingDelegate(
    const base::WeakPtr<SpdyStream>& stream) : stream_(stream) {}

ClosingDelegate::~ClosingDelegate() {}

SpdySendStatus ClosingDelegate::OnSendHeadersComplete() {
  return NO_MORE_DATA_TO_SEND;
}

int ClosingDelegate::OnSendBody() {
  return OK;
}

SpdySendStatus ClosingDelegate::OnSendBodyComplete(size_t /*bytes_sent*/) {
  return NO_MORE_DATA_TO_SEND;
}

int ClosingDelegate::OnResponseReceived(const SpdyHeaderBlock& response,
                                        base::Time response_time,
                                        int status) {
  return OK;
}

void ClosingDelegate::OnHeadersSent() {}

int ClosingDelegate::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) {
  return OK;
}

void ClosingDelegate::OnDataSent(size_t bytes_sent) {}

void ClosingDelegate::OnClose(int status) {
  DCHECK(stream_);
  stream_->Close();
  DCHECK(!stream_);
}

StreamDelegateBase::StreamDelegateBase(
    const base::WeakPtr<SpdyStream>& stream)
    : stream_(stream),
      stream_id_(0),
      send_headers_completed_(false),
      headers_sent_(0),
      data_sent_(0) {
}

StreamDelegateBase::~StreamDelegateBase() {
}

SpdySendStatus StreamDelegateBase::OnSendHeadersComplete() {
  stream_id_ = stream_->stream_id();
  EXPECT_NE(stream_id_, 0u);
  send_headers_completed_ = true;
  return NO_MORE_DATA_TO_SEND;
}

int StreamDelegateBase::OnResponseReceived(const SpdyHeaderBlock& response,
                                           base::Time response_time,
                                           int status) {
  EXPECT_TRUE(send_headers_completed_);
  response_ = response;
  return status;
}

void StreamDelegateBase::OnHeadersSent() {
  headers_sent_++;
}

int StreamDelegateBase::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) {
  if (buffer)
    received_data_queue_.Enqueue(buffer.Pass());
  return OK;
}

void StreamDelegateBase::OnDataSent(size_t bytes_sent) {
  data_sent_ += bytes_sent;
}

void StreamDelegateBase::OnClose(int status) {
  if (!stream_)
    return;
  stream_id_ = stream_->stream_id();
  stream_.reset();
  callback_.callback().Run(status);
}

int StreamDelegateBase::WaitForClose() {
  int result = callback_.WaitForResult();
  EXPECT_TRUE(!stream_.get());
  return result;
}

std::string StreamDelegateBase::TakeReceivedData() {
  size_t len = received_data_queue_.GetTotalSize();
  std::string received_data(len, '\0');
  if (len > 0) {
    EXPECT_EQ(
        len,
        received_data_queue_.Dequeue(string_as_array(&received_data), len));
  }
  return received_data;
}

std::string StreamDelegateBase::GetResponseHeaderValue(
    const std::string& name) const {
  SpdyHeaderBlock::const_iterator it = response_.find(name);
  return (it == response_.end()) ? std::string() : it->second;
}

StreamDelegateDoNothing::StreamDelegateDoNothing(
    const base::WeakPtr<SpdyStream>& stream)
    : StreamDelegateBase(stream) {}

StreamDelegateDoNothing::~StreamDelegateDoNothing() {
}

int StreamDelegateDoNothing::OnSendBody() {
  return OK;
}
SpdySendStatus StreamDelegateDoNothing::OnSendBodyComplete(
    size_t /*bytes_sent*/) {
  return NO_MORE_DATA_TO_SEND;
}

StreamDelegateSendImmediate::StreamDelegateSendImmediate(
    const base::WeakPtr<SpdyStream>& stream,
    scoped_ptr<SpdyHeaderBlock> headers,
    base::StringPiece data)
    : StreamDelegateBase(stream),
      headers_(headers.Pass()),
      data_(data) {}

StreamDelegateSendImmediate::~StreamDelegateSendImmediate() {
}

int StreamDelegateSendImmediate::OnSendBody() {
  ADD_FAILURE() << "OnSendBody should not be called";
  return ERR_UNEXPECTED;
}
SpdySendStatus StreamDelegateSendImmediate::OnSendBodyComplete(
    size_t /*bytes_sent*/) {
  ADD_FAILURE() << "OnSendBodyComplete should not be called";
  return NO_MORE_DATA_TO_SEND;
}

int StreamDelegateSendImmediate::OnResponseReceived(
    const SpdyHeaderBlock& response,
    base::Time response_time,
    int status) {
  status =
      StreamDelegateBase::OnResponseReceived(response, response_time, status);
  if (headers_.get()) {
    stream()->QueueHeaders(headers_.Pass());
  }
  if (data_.data()) {
    scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(data_.as_string()));
    stream()->QueueStreamData(buf, buf->size(), DATA_FLAG_NONE);
  }
  return status;
}

StreamDelegateWithBody::StreamDelegateWithBody(
    const base::WeakPtr<SpdyStream>& stream,
    base::StringPiece data)
    : StreamDelegateBase(stream),
      buf_(new DrainableIOBuffer(new StringIOBuffer(data.as_string()),
                                 data.size())),
      body_data_sent_(0) {}

StreamDelegateWithBody::~StreamDelegateWithBody() {
}

SpdySendStatus StreamDelegateWithBody::OnSendHeadersComplete() {
  StreamDelegateBase::OnSendHeadersComplete();
  return MORE_DATA_TO_SEND;
}

int StreamDelegateWithBody::OnSendBody() {
  stream()->QueueStreamData(buf_.get(), buf_->BytesRemaining(),
                            DATA_FLAG_NONE);
  return ERR_IO_PENDING;
}

SpdySendStatus StreamDelegateWithBody::OnSendBodyComplete(size_t bytes_sent) {
  EXPECT_GT(bytes_sent, 0u);

  buf_->DidConsume(bytes_sent);
  body_data_sent_ += bytes_sent;
  if (buf_->BytesRemaining() > 0) {
    // Go back to OnSendBody() to send the remaining data.
    return MORE_DATA_TO_SEND;
  }

  return NO_MORE_DATA_TO_SEND;
}

} // namespace test

} // namespace net