summaryrefslogtreecommitdiffstats
path: root/net/spdy/spdy_stream_test_util.cc
blob: e2144aece022f25434066f75320daf0a17da9faf (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
// 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 "net/base/completion_callback.h"
#include "net/spdy/spdy_stream.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace test {

TestSpdyStreamDelegate::TestSpdyStreamDelegate(
    SpdyStream* stream,
    SpdyHeaderBlock* headers,
    IOBufferWithSize* buf,
    const CompletionCallback& callback)
    : stream_(stream),
      headers_(headers),
      buf_(buf),
      callback_(callback),
      send_headers_completed_(false),
      response_(new SpdyHeaderBlock),
      headers_sent_(0),
      data_sent_(0),
      closed_(false) {
}

TestSpdyStreamDelegate::~TestSpdyStreamDelegate() {
}

bool TestSpdyStreamDelegate::OnSendHeadersComplete(int status) {
  send_headers_completed_ = true;
  return true;
}

int TestSpdyStreamDelegate::OnSendBody() {
  ADD_FAILURE() << "OnSendBody should not be called";
  return ERR_UNEXPECTED;
}
int TestSpdyStreamDelegate::OnSendBodyComplete(int /*status*/, bool* /*eof*/) {
  ADD_FAILURE() << "OnSendBodyComplete should not be called";
  return ERR_UNEXPECTED;
}

int TestSpdyStreamDelegate::OnResponseReceived(const SpdyHeaderBlock& response,
                                               base::Time response_time,
                                               int status) {
  EXPECT_TRUE(send_headers_completed_);
  *response_ = response;
  if (headers_.get()) {
    EXPECT_EQ(ERR_IO_PENDING,
              stream_->WriteHeaders(headers_.release()));
  }
  if (buf_) {
    EXPECT_EQ(ERR_IO_PENDING,
              stream_->WriteStreamData(buf_.get(), buf_->size(),
                                       DATA_FLAG_NONE));
  }
  return status;
}

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

int TestSpdyStreamDelegate::OnDataReceived(const char* buffer, int bytes) {
  received_data_ += std::string(buffer, bytes);
  return OK;
}

void TestSpdyStreamDelegate::OnDataSent(int length) {
  data_sent_ += length;
}

void TestSpdyStreamDelegate::OnClose(int status) {
  closed_ = true;
  CompletionCallback callback = callback_;
  callback_.Reset();
  callback.Run(OK);
}

} // namespace test

} // namespace net