1 // Copyright (c) 2013 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/url_request/url_request_http_job.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "net/base/auth.h"
14 #include "net/base/request_priority.h"
15 #include "net/http/http_transaction_factory.h"
16 #include "net/http/http_transaction_test_util.h"
17 #include "net/socket/socket_test_util.h"
18 #include "net/url_request/url_request.h"
19 #include "net/url_request/url_request_status.h"
20 #include "net/url_request/url_request_test_util.h"
21 #include "net/websockets/websocket_handshake_stream_base.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
30 using ::testing::Return
;
32 // Inherit from URLRequestHttpJob to expose the priority and some
33 // other hidden functions.
34 class TestURLRequestHttpJob
: public URLRequestHttpJob
{
36 explicit TestURLRequestHttpJob(URLRequest
* request
)
37 : URLRequestHttpJob(request
, request
->context()->network_delegate(),
38 request
->context()->http_user_agent_settings()) {}
40 using URLRequestHttpJob::SetPriority
;
41 using URLRequestHttpJob::Start
;
42 using URLRequestHttpJob::Kill
;
43 using URLRequestHttpJob::priority
;
46 ~TestURLRequestHttpJob() override
{}
49 class URLRequestHttpJobTest
: public ::testing::Test
{
51 URLRequestHttpJobTest()
52 : req_(context_
.CreateRequest(GURL("http://www.example.com"),
56 context_
.set_http_transaction_factory(&network_layer_
);
59 bool TransactionAcceptsSdchEncoding() {
60 base::WeakPtr
<MockNetworkTransaction
> transaction(
61 network_layer_
.last_transaction());
62 EXPECT_TRUE(transaction
);
63 if (!transaction
) return false;
65 const HttpRequestInfo
* request_info
= transaction
->request();
66 EXPECT_TRUE(request_info
);
67 if (!request_info
) return false;
69 std::string encoding_headers
;
70 bool get_success
= request_info
->extra_headers
.GetHeader(
71 "Accept-Encoding", &encoding_headers
);
72 EXPECT_TRUE(get_success
);
73 if (!get_success
) return false;
75 // This check isn't wrapped with EXPECT* macros because different
76 // results from this function may be expected in different tests.
77 std::vector
<std::string
> tokens
;
78 size_t num_tokens
= Tokenize(encoding_headers
, ", ", &tokens
);
79 for (size_t i
= 0; i
< num_tokens
; i
++) {
80 if (!base::strncasecmp(tokens
[i
].data(), "sdch", tokens
[i
].length()))
87 context_
.SetSdchManager(scoped_ptr
<SdchManager
>(new SdchManager
).Pass());
90 MockNetworkLayer network_layer_
;
91 TestURLRequestContext context_
;
92 TestDelegate delegate_
;
93 scoped_ptr
<URLRequest
> req_
;
96 // Make sure that SetPriority actually sets the URLRequestHttpJob's
97 // priority, both before and after start.
98 TEST_F(URLRequestHttpJobTest
, SetPriorityBasic
) {
99 scoped_refptr
<TestURLRequestHttpJob
> job(
100 new TestURLRequestHttpJob(req_
.get()));
101 EXPECT_EQ(DEFAULT_PRIORITY
, job
->priority());
103 job
->SetPriority(LOWEST
);
104 EXPECT_EQ(LOWEST
, job
->priority());
106 job
->SetPriority(LOW
);
107 EXPECT_EQ(LOW
, job
->priority());
110 EXPECT_EQ(LOW
, job
->priority());
112 job
->SetPriority(MEDIUM
);
113 EXPECT_EQ(MEDIUM
, job
->priority());
116 // Make sure that URLRequestHttpJob passes on its priority to its
117 // transaction on start.
118 TEST_F(URLRequestHttpJobTest
, SetTransactionPriorityOnStart
) {
119 scoped_refptr
<TestURLRequestHttpJob
> job(
120 new TestURLRequestHttpJob(req_
.get()));
121 job
->SetPriority(LOW
);
123 EXPECT_FALSE(network_layer_
.last_transaction());
127 ASSERT_TRUE(network_layer_
.last_transaction());
128 EXPECT_EQ(LOW
, network_layer_
.last_transaction()->priority());
131 // Make sure that URLRequestHttpJob passes on its priority updates to
133 TEST_F(URLRequestHttpJobTest
, SetTransactionPriority
) {
134 scoped_refptr
<TestURLRequestHttpJob
> job(
135 new TestURLRequestHttpJob(req_
.get()));
136 job
->SetPriority(LOW
);
138 ASSERT_TRUE(network_layer_
.last_transaction());
139 EXPECT_EQ(LOW
, network_layer_
.last_transaction()->priority());
141 job
->SetPriority(HIGHEST
);
142 EXPECT_EQ(HIGHEST
, network_layer_
.last_transaction()->priority());
145 // Make sure that URLRequestHttpJob passes on its priority updates to
146 // newly-created transactions after the first one.
147 TEST_F(URLRequestHttpJobTest
, SetSubsequentTransactionPriority
) {
148 scoped_refptr
<TestURLRequestHttpJob
> job(
149 new TestURLRequestHttpJob(req_
.get()));
152 job
->SetPriority(LOW
);
153 ASSERT_TRUE(network_layer_
.last_transaction());
154 EXPECT_EQ(LOW
, network_layer_
.last_transaction()->priority());
157 network_layer_
.ClearLastTransaction();
159 // Creates a second transaction.
161 ASSERT_TRUE(network_layer_
.last_transaction());
162 EXPECT_EQ(LOW
, network_layer_
.last_transaction()->priority());
165 // Confirm we do advertise SDCH encoding in the case of a GET.
166 TEST_F(URLRequestHttpJobTest
, SdchAdvertisementGet
) {
168 req_
->set_method("GET"); // Redundant with default.
169 scoped_refptr
<TestURLRequestHttpJob
> job(
170 new TestURLRequestHttpJob(req_
.get()));
172 EXPECT_TRUE(TransactionAcceptsSdchEncoding());
175 // Confirm we don't advertise SDCH encoding in the case of a POST.
176 TEST_F(URLRequestHttpJobTest
, SdchAdvertisementPost
) {
178 req_
->set_method("POST");
179 scoped_refptr
<TestURLRequestHttpJob
> job(
180 new TestURLRequestHttpJob(req_
.get()));
182 EXPECT_FALSE(TransactionAcceptsSdchEncoding());
185 // This base class just serves to set up some things before the TestURLRequest
186 // constructor is called.
187 class URLRequestHttpJobWebSocketTestBase
: public ::testing::Test
{
189 URLRequestHttpJobWebSocketTestBase() : socket_data_(NULL
, 0, NULL
, 0),
191 // A Network Delegate is required for the WebSocketHandshakeStreamBase
192 // object to be passed on to the HttpNetworkTransaction.
193 context_
.set_network_delegate(&network_delegate_
);
195 // Attempting to create real ClientSocketHandles is not going to work out so
196 // well. Set up a fake socket factory.
197 socket_factory_
.AddSocketDataProvider(&socket_data_
);
198 context_
.set_client_socket_factory(&socket_factory_
);
202 StaticSocketDataProvider socket_data_
;
203 TestNetworkDelegate network_delegate_
;
204 MockClientSocketFactory socket_factory_
;
205 TestURLRequestContext context_
;
208 class URLRequestHttpJobWebSocketTest
209 : public URLRequestHttpJobWebSocketTestBase
{
211 URLRequestHttpJobWebSocketTest()
212 : req_(context_
.CreateRequest(GURL("ws://www.example.com"),
216 // The TestNetworkDelegate expects a call to NotifyBeforeURLRequest before
217 // anything else happens.
218 GURL
url("ws://localhost/");
219 TestCompletionCallback dummy
;
220 network_delegate_
.NotifyBeforeURLRequest(
221 req_
.get(), dummy
.callback(), &url
);
224 TestDelegate delegate_
;
225 scoped_ptr
<URLRequest
> req_
;
228 class MockCreateHelper
: public WebSocketHandshakeStreamBase::CreateHelper
{
230 // GoogleMock does not appear to play nicely with move-only types like
231 // scoped_ptr, so this forwarding method acts as a workaround.
232 virtual WebSocketHandshakeStreamBase
* CreateBasicStream(
233 scoped_ptr
<ClientSocketHandle
> connection
,
234 bool using_proxy
) override
{
235 // Discard the arguments since we don't need them anyway.
236 return CreateBasicStreamMock();
239 MOCK_METHOD0(CreateBasicStreamMock
,
240 WebSocketHandshakeStreamBase
*());
242 MOCK_METHOD2(CreateSpdyStream
,
243 WebSocketHandshakeStreamBase
*(const base::WeakPtr
<SpdySession
>&,
247 class FakeWebSocketHandshakeStream
: public WebSocketHandshakeStreamBase
{
249 FakeWebSocketHandshakeStream() : initialize_stream_was_called_(false) {}
251 bool initialize_stream_was_called() const {
252 return initialize_stream_was_called_
;
255 // Fake implementation of HttpStreamBase methods.
256 int InitializeStream(const HttpRequestInfo
* request_info
,
257 RequestPriority priority
,
258 const BoundNetLog
& net_log
,
259 const CompletionCallback
& callback
) override
{
260 initialize_stream_was_called_
= true;
261 return ERR_IO_PENDING
;
264 int SendRequest(const HttpRequestHeaders
& request_headers
,
265 HttpResponseInfo
* response
,
266 const CompletionCallback
& callback
) override
{
267 return ERR_IO_PENDING
;
270 int ReadResponseHeaders(const CompletionCallback
& callback
) override
{
271 return ERR_IO_PENDING
;
274 int ReadResponseBody(IOBuffer
* buf
,
276 const CompletionCallback
& callback
) override
{
277 return ERR_IO_PENDING
;
280 void Close(bool not_reusable
) override
{}
282 bool IsResponseBodyComplete() const override
{ return false; }
284 bool CanFindEndOfResponse() const override
{ return false; }
286 bool IsConnectionReused() const override
{ return false; }
287 void SetConnectionReused() override
{}
289 bool IsConnectionReusable() const override
{ return false; }
291 int64
GetTotalReceivedBytes() const override
{ return 0; }
293 bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
{
297 void GetSSLInfo(SSLInfo
* ssl_info
) override
{}
299 void GetSSLCertRequestInfo(SSLCertRequestInfo
* cert_request_info
) override
{}
301 bool IsSpdyHttpStream() const override
{ return false; }
303 void Drain(HttpNetworkSession
* session
) override
{}
305 void SetPriority(RequestPriority priority
) override
{}
307 // Fake implementation of WebSocketHandshakeStreamBase method(s)
308 scoped_ptr
<WebSocketStream
> Upgrade() override
{
309 return scoped_ptr
<WebSocketStream
>();
313 bool initialize_stream_was_called_
;
316 TEST_F(URLRequestHttpJobWebSocketTest
, RejectedWithoutCreateHelper
) {
317 scoped_refptr
<TestURLRequestHttpJob
> job(
318 new TestURLRequestHttpJob(req_
.get()));
320 base::RunLoop().RunUntilIdle();
321 EXPECT_EQ(URLRequestStatus::FAILED
, req_
->status().status());
322 EXPECT_EQ(ERR_DISALLOWED_URL_SCHEME
, req_
->status().error());
325 TEST_F(URLRequestHttpJobWebSocketTest
, CreateHelperPassedThrough
) {
326 scoped_refptr
<TestURLRequestHttpJob
> job(
327 new TestURLRequestHttpJob(req_
.get()));
328 scoped_ptr
<MockCreateHelper
> create_helper(
329 new ::testing::StrictMock
<MockCreateHelper
>());
330 FakeWebSocketHandshakeStream
* fake_handshake_stream(
331 new FakeWebSocketHandshakeStream
);
332 // Ownership of fake_handshake_stream is transferred when CreateBasicStream()
334 EXPECT_CALL(*create_helper
, CreateBasicStreamMock())
335 .WillOnce(Return(fake_handshake_stream
));
336 req_
->SetUserData(WebSocketHandshakeStreamBase::CreateHelper::DataKey(),
337 create_helper
.release());
338 req_
->SetLoadFlags(LOAD_DISABLE_CACHE
);
340 base::RunLoop().RunUntilIdle();
341 EXPECT_EQ(URLRequestStatus::IO_PENDING
, req_
->status().status());
342 EXPECT_TRUE(fake_handshake_stream
->initialize_stream_was_called());