summaryrefslogtreecommitdiffstats
path: root/net/spdy/spdy_http_stream_unittest.cc
blob: 9c23b54433f46b124c34c52029d6609482b71bdb (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
// Copyright (c) 2010 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_http_stream.h"
#include "net/spdy/spdy_session.h"
#include "net/spdy/spdy_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

class SpdyHttpStreamTest : public testing::Test {
 protected:
  SpdyHttpStreamTest() {}

  void EnableCompression(bool enabled) {
    spdy::SpdyFramer::set_enable_compression_default(enabled);
  }

  virtual void TearDown() {
    MessageLoop::current()->RunAllPending();
  }
  int InitSession(MockRead* reads, size_t reads_count,
                  MockWrite* writes, size_t writes_count,
                  HostPortPair& host_port_pair) {
    data_ = new OrderedSocketData(reads, reads_count, writes, writes_count);
    session_deps_.socket_factory.AddSocketDataProvider(data_.get());
    http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_);
    session_ = http_session_->spdy_session_pool()->
      Get(host_port_pair, http_session_.get(), BoundNetLog());
    tcp_params_ = new TCPSocketParams(host_port_pair.host(),
                                      host_port_pair.port(),
                                      MEDIUM, GURL(), false);
    return session_->Connect(host_port_pair.host(), tcp_params_, MEDIUM);
  }
  SpdySessionDependencies session_deps_;
  scoped_refptr<OrderedSocketData> data_;
  scoped_refptr<HttpNetworkSession> http_session_;
  scoped_refptr<SpdySession> session_;
  scoped_refptr<TCPSocketParams> tcp_params_;
};

TEST_F(SpdyHttpStreamTest, SendRequest) {
  EnableCompression(false);
  SpdySession::SetSSLMode(false);

  SpdySessionDependencies session_deps;
  scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
  MockWrite writes[] = {
    CreateMockWrite(*req.get(), 1),
  };
  MockRead reads[] = {
    MockRead(false, 0, 2)  // EOF
  };

  HostPortPair host_port_pair("www.google.com", 80);
  EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes),
      host_port_pair));

  HttpRequestInfo request;
  request.method = "GET";
  request.url = GURL("http://www.google.com/");
  TestCompletionCallback callback;
  HttpResponseInfo response;
  scoped_ptr<SpdyHttpStream> http_stream(new SpdyHttpStream());
  ASSERT_EQ(
      OK,
      http_stream->InitializeStream(session_, request, BoundNetLog(), NULL));
  http_stream->InitializeRequest(base::Time::Now(), NULL);

  EXPECT_EQ(ERR_IO_PENDING,
            http_stream->SendRequest(&response, &callback));
  MessageLoop::current()->RunAllPending();
  EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(host_port_pair));
  http_session_->spdy_session_pool()->Remove(session_);
}

// Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058
TEST_F(SpdyHttpStreamTest, SpdyURLTest) {
  EnableCompression(false);
  SpdySession::SetSSLMode(false);

  scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
  MockWrite writes[] = {
    CreateMockWrite(*req.get(), 1),
  };
  MockRead reads[] = {
    MockRead(false, 0, 2),  // EOF
  };

  HostPortPair host_port_pair("www.google.com", 80);
  EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes),
      host_port_pair));

  HttpRequestInfo request;
  request.method = "GET";
  request.url = GURL("http://www.google.com/foo?query=what#anchor");
  TestCompletionCallback callback;
  HttpResponseInfo response;
  scoped_ptr<SpdyHttpStream> http_stream(new SpdyHttpStream());
  ASSERT_EQ(
      OK,
      http_stream->InitializeStream(session_, request, BoundNetLog(), NULL));
  http_stream->InitializeRequest(base::Time::Now(), NULL);

  spdy::SpdyHeaderBlock* spdy_header =
    http_stream->stream()->spdy_headers().get();
  if (spdy_header->find("url") != spdy_header->end())
    EXPECT_EQ("http://www.google.com/foo?query=what",
              spdy_header->find("url")->second);
  else
    FAIL() << "No url is set in spdy_header!";

  MessageLoop::current()->RunAllPending();
  EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(host_port_pair));
  http_session_->spdy_session_pool()->Remove(session_);
}

// TODO(willchan): Write a longer test for SpdyStream that exercises all
// methods.

}  // namespace net