Remove redundant Tab constructors.
[chromium-blink-merge.git] / net / url_request / url_request_http_job_unittest.cc
blob3d73146eb51287c3fb79462ec4f0ebc2483b41a0
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"
7 #include <stdint.h>
9 #include <cstddef>
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/run_loop.h"
15 #include "base/strings/string_split.h"
16 #include "net/base/auth.h"
17 #include "net/base/request_priority.h"
18 #include "net/base/test_data_directory.h"
19 #include "net/cookies/cookie_store_test_helpers.h"
20 #include "net/http/http_transaction_factory.h"
21 #include "net/http/http_transaction_test_util.h"
22 #include "net/socket/socket_test_util.h"
23 #include "net/test/cert_test_util.h"
24 #include "net/url_request/url_request.h"
25 #include "net/url_request/url_request_status.h"
26 #include "net/url_request/url_request_test_util.h"
27 #include "net/websockets/websocket_handshake_stream_base.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "url/gurl.h"
32 namespace net {
34 namespace {
36 using ::testing::Return;
38 // Inherit from URLRequestHttpJob to expose the priority and some
39 // other hidden functions.
40 class TestURLRequestHttpJob : public URLRequestHttpJob {
41 public:
42 explicit TestURLRequestHttpJob(URLRequest* request)
43 : URLRequestHttpJob(request, request->context()->network_delegate(),
44 request->context()->http_user_agent_settings()) {}
46 using URLRequestHttpJob::SetPriority;
47 using URLRequestHttpJob::Start;
48 using URLRequestHttpJob::Kill;
49 using URLRequestHttpJob::priority;
51 protected:
52 ~TestURLRequestHttpJob() override {}
55 class URLRequestHttpJobTest : public ::testing::Test {
56 protected:
57 URLRequestHttpJobTest()
58 : req_(context_.CreateRequest(GURL("http://www.example.com"),
59 DEFAULT_PRIORITY,
60 &delegate_)) {
61 context_.set_http_transaction_factory(&network_layer_);
64 bool TransactionAcceptsSdchEncoding() {
65 base::WeakPtr<MockNetworkTransaction> transaction(
66 network_layer_.last_transaction());
67 EXPECT_TRUE(transaction);
68 if (!transaction) return false;
70 const HttpRequestInfo* request_info = transaction->request();
71 EXPECT_TRUE(request_info);
72 if (!request_info) return false;
74 std::string encoding_headers;
75 bool get_success = request_info->extra_headers.GetHeader(
76 "Accept-Encoding", &encoding_headers);
77 EXPECT_TRUE(get_success);
78 if (!get_success) return false;
80 // This check isn't wrapped with EXPECT* macros because different
81 // results from this function may be expected in different tests.
82 for (const std::string& token :
83 base::SplitString(encoding_headers, ", ", base::KEEP_WHITESPACE,
84 base::SPLIT_WANT_NONEMPTY)) {
85 if (base::EqualsCaseInsensitiveASCII(token, "sdch"))
86 return true;
88 return false;
91 void EnableSdch() {
92 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass());
95 MockNetworkLayer network_layer_;
96 TestURLRequestContext context_;
97 TestDelegate delegate_;
98 scoped_ptr<URLRequest> req_;
101 class URLRequestHttpJobWithMockSocketsTest : public ::testing::Test {
102 protected:
103 URLRequestHttpJobWithMockSocketsTest()
104 : context_(new TestURLRequestContext(true)) {
105 context_->set_client_socket_factory(&socket_factory_);
106 context_->set_network_delegate(&network_delegate_);
107 context_->set_backoff_manager(&manager_);
108 context_->Init();
111 MockClientSocketFactory socket_factory_;
112 TestNetworkDelegate network_delegate_;
113 URLRequestBackoffManager manager_;
114 scoped_ptr<TestURLRequestContext> context_;
117 const char kSimpleGetMockWrite[] =
118 "GET / HTTP/1.1\r\n"
119 "Host: www.example.com\r\n"
120 "Connection: keep-alive\r\n"
121 "User-Agent:\r\n"
122 "Accept-Encoding: gzip, deflate\r\n"
123 "Accept-Language: en-us,fr\r\n\r\n";
125 TEST_F(URLRequestHttpJobWithMockSocketsTest,
126 TestContentLengthSuccessfulRequest) {
127 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
128 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
129 "Content-Length: 12\r\n\r\n"),
130 MockRead("Test Content")};
132 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
133 arraysize(writes));
134 socket_factory_.AddSocketDataProvider(&socket_data);
136 TestDelegate delegate;
137 scoped_ptr<URLRequest> request =
138 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
139 &delegate)
140 .Pass();
142 request->Start();
143 ASSERT_TRUE(request->is_pending());
144 base::RunLoop().Run();
146 EXPECT_TRUE(request->status().is_success());
147 EXPECT_EQ(12, request->received_response_content_length());
148 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
149 request->GetTotalSentBytes());
150 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
151 request->GetTotalReceivedBytes());
152 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
153 network_delegate_.total_network_bytes_received());
156 TEST_F(URLRequestHttpJobWithMockSocketsTest,
157 TestContentLengthSuccessfulHttp09Request) {
158 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
159 MockRead reads[] = {MockRead("Test Content"),
160 MockRead(net::SYNCHRONOUS, net::OK)};
162 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0);
163 socket_factory_.AddSocketDataProvider(&socket_data);
165 TestDelegate delegate;
166 scoped_ptr<URLRequest> request =
167 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
168 &delegate)
169 .Pass();
171 request->Start();
172 ASSERT_TRUE(request->is_pending());
173 base::RunLoop().Run();
175 EXPECT_TRUE(request->status().is_success());
176 EXPECT_EQ(12, request->received_response_content_length());
177 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
178 request->GetTotalSentBytes());
179 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
180 request->GetTotalReceivedBytes());
181 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
182 network_delegate_.total_network_bytes_received());
185 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestContentLengthFailedRequest) {
186 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
187 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
188 "Content-Length: 20\r\n\r\n"),
189 MockRead("Test Content"),
190 MockRead(net::SYNCHRONOUS, net::ERR_FAILED)};
192 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
193 arraysize(writes));
194 socket_factory_.AddSocketDataProvider(&socket_data);
196 TestDelegate delegate;
197 scoped_ptr<URLRequest> request =
198 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
199 &delegate)
200 .Pass();
202 request->Start();
203 ASSERT_TRUE(request->is_pending());
204 base::RunLoop().Run();
206 EXPECT_EQ(URLRequestStatus::FAILED, request->status().status());
207 EXPECT_EQ(12, request->received_response_content_length());
208 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
209 request->GetTotalSentBytes());
210 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
211 request->GetTotalReceivedBytes());
212 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
213 network_delegate_.total_network_bytes_received());
216 TEST_F(URLRequestHttpJobWithMockSocketsTest,
217 TestContentLengthCancelledRequest) {
218 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
219 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
220 "Content-Length: 20\r\n\r\n"),
221 MockRead("Test Content"),
222 MockRead(net::SYNCHRONOUS, net::ERR_IO_PENDING)};
224 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
225 arraysize(writes));
226 socket_factory_.AddSocketDataProvider(&socket_data);
228 TestDelegate delegate;
229 scoped_ptr<URLRequest> request =
230 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
231 &delegate)
232 .Pass();
234 delegate.set_cancel_in_received_data(true);
235 request->Start();
236 base::RunLoop().RunUntilIdle();
238 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status());
239 EXPECT_EQ(12, request->received_response_content_length());
240 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
241 request->GetTotalSentBytes());
242 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
243 request->GetTotalReceivedBytes());
244 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
245 network_delegate_.total_network_bytes_received());
248 TEST_F(URLRequestHttpJobWithMockSocketsTest,
249 TestNetworkBytesRedirectedRequest) {
250 MockWrite redirect_writes[] = {
251 MockWrite("GET / HTTP/1.1\r\n"
252 "Host: www.redirect.com\r\n"
253 "Connection: keep-alive\r\n"
254 "User-Agent:\r\n"
255 "Accept-Encoding: gzip, deflate\r\n"
256 "Accept-Language: en-us,fr\r\n\r\n")};
258 MockRead redirect_reads[] = {
259 MockRead("HTTP/1.1 302 Found\r\n"
260 "Location: http://www.example.com\r\n\r\n"),
262 StaticSocketDataProvider redirect_socket_data(
263 redirect_reads, arraysize(redirect_reads), redirect_writes,
264 arraysize(redirect_writes));
265 socket_factory_.AddSocketDataProvider(&redirect_socket_data);
267 MockWrite final_writes[] = {MockWrite(kSimpleGetMockWrite)};
268 MockRead final_reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
269 "Content-Length: 12\r\n\r\n"),
270 MockRead("Test Content")};
271 StaticSocketDataProvider final_socket_data(
272 final_reads, arraysize(final_reads), final_writes,
273 arraysize(final_writes));
274 socket_factory_.AddSocketDataProvider(&final_socket_data);
276 TestDelegate delegate;
277 scoped_ptr<URLRequest> request =
278 context_->CreateRequest(GURL("http://www.redirect.com"), DEFAULT_PRIORITY,
279 &delegate)
280 .Pass();
282 request->Start();
283 ASSERT_TRUE(request->is_pending());
284 base::RunLoop().RunUntilIdle();
286 EXPECT_TRUE(request->status().is_success());
287 EXPECT_EQ(12, request->received_response_content_length());
288 // Should not include the redirect.
289 EXPECT_EQ(CountWriteBytes(final_writes, arraysize(final_writes)),
290 request->GetTotalSentBytes());
291 EXPECT_EQ(CountReadBytes(final_reads, arraysize(final_reads)),
292 request->GetTotalReceivedBytes());
293 // Should include the redirect as well as the final response.
294 EXPECT_EQ(CountReadBytes(redirect_reads, arraysize(redirect_reads)) +
295 CountReadBytes(final_reads, arraysize(final_reads)),
296 network_delegate_.total_network_bytes_received());
299 TEST_F(URLRequestHttpJobWithMockSocketsTest,
300 TestNetworkBytesCancelledAfterHeaders) {
301 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
302 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n\r\n")};
303 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
304 arraysize(writes));
305 socket_factory_.AddSocketDataProvider(&socket_data);
307 TestDelegate delegate;
308 scoped_ptr<URLRequest> request =
309 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
310 &delegate)
311 .Pass();
313 delegate.set_cancel_in_response_started(true);
314 request->Start();
315 base::RunLoop().RunUntilIdle();
317 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status());
318 EXPECT_EQ(0, request->received_response_content_length());
319 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
320 request->GetTotalSentBytes());
321 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
322 request->GetTotalReceivedBytes());
323 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
324 network_delegate_.total_network_bytes_received());
327 TEST_F(URLRequestHttpJobWithMockSocketsTest,
328 TestNetworkBytesCancelledImmediately) {
329 StaticSocketDataProvider socket_data(nullptr, 0, nullptr, 0);
330 socket_factory_.AddSocketDataProvider(&socket_data);
332 TestDelegate delegate;
333 scoped_ptr<URLRequest> request =
334 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
335 &delegate)
336 .Pass();
338 request->Start();
339 request->Cancel();
340 base::RunLoop().RunUntilIdle();
342 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status());
343 EXPECT_EQ(0, request->received_response_content_length());
344 EXPECT_EQ(0, request->GetTotalSentBytes());
345 EXPECT_EQ(0, request->GetTotalReceivedBytes());
346 EXPECT_EQ(0, network_delegate_.total_network_bytes_received());
349 TEST_F(URLRequestHttpJobWithMockSocketsTest, BackoffHeader) {
350 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
351 MockRead reads[] = {MockRead(
352 "HTTP/1.1 200 OK\r\n"
353 "Backoff: 3600\r\n"
354 "Content-Length: 9\r\n\r\n"),
355 MockRead("test.html")};
357 net::SSLSocketDataProvider ssl_socket_data_provider(net::ASYNC, net::OK);
358 ssl_socket_data_provider.SetNextProto(kProtoHTTP11);
359 ssl_socket_data_provider.cert =
360 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der");
361 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data_provider);
363 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
364 arraysize(writes));
365 socket_factory_.AddSocketDataProvider(&socket_data);
367 TestDelegate delegate1;
368 scoped_ptr<URLRequest> request1 =
369 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY,
370 &delegate1).Pass();
372 request1->Start();
373 ASSERT_TRUE(request1->is_pending());
374 base::RunLoop().Run();
376 EXPECT_TRUE(request1->status().is_success());
377 EXPECT_EQ("test.html", delegate1.data_received());
378 EXPECT_EQ(1, delegate1.received_before_network_start_count());
379 EXPECT_EQ(1, manager_.GetNumberOfEntriesForTests());
381 // Issue another request, and backoff logic should apply.
382 TestDelegate delegate2;
383 scoped_ptr<URLRequest> request2 =
384 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY,
385 &delegate2).Pass();
387 request2->Start();
388 ASSERT_TRUE(request2->is_pending());
389 base::RunLoop().Run();
391 EXPECT_FALSE(request2->status().is_success());
392 EXPECT_EQ(ERR_TEMPORARY_BACKOFF, request2->status().error());
393 EXPECT_EQ(0, delegate2.received_before_network_start_count());
396 TEST_F(URLRequestHttpJobWithMockSocketsTest, BackoffHeaderNotSecure) {
397 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
398 MockRead reads[] = {MockRead(
399 "HTTP/1.1 200 OK\r\n"
400 "Backoff: 3600\r\n"
401 "Content-Length: 9\r\n\r\n"),
402 MockRead("test.html")};
404 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
405 arraysize(writes));
406 socket_factory_.AddSocketDataProvider(&socket_data);
408 TestDelegate delegate;
409 scoped_ptr<URLRequest> request =
410 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
411 &delegate).Pass();
413 request->Start();
414 ASSERT_TRUE(request->is_pending());
415 base::RunLoop().Run();
417 EXPECT_TRUE(request->status().is_success());
418 EXPECT_EQ("test.html", delegate.data_received());
419 EXPECT_EQ(1, delegate.received_before_network_start_count());
420 // Backoff logic does not apply to plain HTTP request.
421 EXPECT_EQ(0, manager_.GetNumberOfEntriesForTests());
424 TEST_F(URLRequestHttpJobWithMockSocketsTest, BackoffHeaderCachedResponse) {
425 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
426 MockRead reads[] = {MockRead(
427 "HTTP/1.1 200 OK\r\n"
428 "Backoff: 3600\r\n"
429 "Cache-Control: max-age=120\r\n"
430 "Content-Length: 9\r\n\r\n"),
431 MockRead("test.html")};
433 net::SSLSocketDataProvider ssl_socket_data_provider(net::ASYNC, net::OK);
434 ssl_socket_data_provider.SetNextProto(kProtoHTTP11);
435 ssl_socket_data_provider.cert =
436 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der");
437 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data_provider);
439 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
440 arraysize(writes));
441 socket_factory_.AddSocketDataProvider(&socket_data);
443 TestDelegate delegate1;
444 scoped_ptr<URLRequest> request1 =
445 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY,
446 &delegate1).Pass();
448 request1->Start();
449 ASSERT_TRUE(request1->is_pending());
450 base::RunLoop().Run();
452 EXPECT_TRUE(request1->status().is_success());
453 EXPECT_EQ("test.html", delegate1.data_received());
454 EXPECT_EQ(1, delegate1.received_before_network_start_count());
455 EXPECT_EQ(1, manager_.GetNumberOfEntriesForTests());
457 // Backoff logic does not apply to a second request, since it is fetched
458 // from cache.
459 TestDelegate delegate2;
460 scoped_ptr<URLRequest> request2 =
461 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY,
462 &delegate2).Pass();
464 request2->Start();
465 ASSERT_TRUE(request2->is_pending());
466 base::RunLoop().Run();
467 EXPECT_TRUE(request2->was_cached());
468 EXPECT_TRUE(request2->status().is_success());
469 EXPECT_EQ(0, delegate2.received_before_network_start_count());
472 TEST_F(URLRequestHttpJobTest, TestCancelWhileReadingCookies) {
473 context_.set_cookie_store(new DelayedCookieMonster());
475 TestDelegate delegate;
476 scoped_ptr<URLRequest> request =
477 context_.CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
478 &delegate)
479 .Pass();
481 request->Start();
482 request->Cancel();
483 base::RunLoop().Run();
485 DCHECK_EQ(0, delegate.received_before_network_start_count());
486 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status());
489 // Make sure that SetPriority actually sets the URLRequestHttpJob's
490 // priority, both before and after start.
491 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) {
492 scoped_refptr<TestURLRequestHttpJob> job(
493 new TestURLRequestHttpJob(req_.get()));
494 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
496 job->SetPriority(LOWEST);
497 EXPECT_EQ(LOWEST, job->priority());
499 job->SetPriority(LOW);
500 EXPECT_EQ(LOW, job->priority());
502 job->Start();
503 EXPECT_EQ(LOW, job->priority());
505 job->SetPriority(MEDIUM);
506 EXPECT_EQ(MEDIUM, job->priority());
509 // Make sure that URLRequestHttpJob passes on its priority to its
510 // transaction on start.
511 TEST_F(URLRequestHttpJobTest, SetTransactionPriorityOnStart) {
512 scoped_refptr<TestURLRequestHttpJob> job(
513 new TestURLRequestHttpJob(req_.get()));
514 job->SetPriority(LOW);
516 EXPECT_FALSE(network_layer_.last_transaction());
518 job->Start();
520 ASSERT_TRUE(network_layer_.last_transaction());
521 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority());
524 // Make sure that URLRequestHttpJob passes on its priority updates to
525 // its transaction.
526 TEST_F(URLRequestHttpJobTest, SetTransactionPriority) {
527 scoped_refptr<TestURLRequestHttpJob> job(
528 new TestURLRequestHttpJob(req_.get()));
529 job->SetPriority(LOW);
530 job->Start();
531 ASSERT_TRUE(network_layer_.last_transaction());
532 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority());
534 job->SetPriority(HIGHEST);
535 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority());
538 // Confirm we do advertise SDCH encoding in the case of a GET.
539 TEST_F(URLRequestHttpJobTest, SdchAdvertisementGet) {
540 EnableSdch();
541 req_->set_method("GET"); // Redundant with default.
542 scoped_refptr<TestURLRequestHttpJob> job(
543 new TestURLRequestHttpJob(req_.get()));
544 job->Start();
545 EXPECT_TRUE(TransactionAcceptsSdchEncoding());
548 // Confirm we don't advertise SDCH encoding in the case of a POST.
549 TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) {
550 EnableSdch();
551 req_->set_method("POST");
552 scoped_refptr<TestURLRequestHttpJob> job(
553 new TestURLRequestHttpJob(req_.get()));
554 job->Start();
555 EXPECT_FALSE(TransactionAcceptsSdchEncoding());
558 // This base class just serves to set up some things before the TestURLRequest
559 // constructor is called.
560 class URLRequestHttpJobWebSocketTestBase : public ::testing::Test {
561 protected:
562 URLRequestHttpJobWebSocketTestBase() : socket_data_(nullptr, 0, nullptr, 0),
563 context_(true) {
564 // A Network Delegate is required for the WebSocketHandshakeStreamBase
565 // object to be passed on to the HttpNetworkTransaction.
566 context_.set_network_delegate(&network_delegate_);
568 // Attempting to create real ClientSocketHandles is not going to work out so
569 // well. Set up a fake socket factory.
570 socket_factory_.AddSocketDataProvider(&socket_data_);
571 context_.set_client_socket_factory(&socket_factory_);
572 context_.Init();
575 StaticSocketDataProvider socket_data_;
576 TestNetworkDelegate network_delegate_;
577 MockClientSocketFactory socket_factory_;
578 TestURLRequestContext context_;
581 class URLRequestHttpJobWebSocketTest
582 : public URLRequestHttpJobWebSocketTestBase {
583 protected:
584 URLRequestHttpJobWebSocketTest()
585 : req_(context_.CreateRequest(GURL("ws://www.example.com"),
586 DEFAULT_PRIORITY,
587 &delegate_)) {
588 // The TestNetworkDelegate expects a call to NotifyBeforeURLRequest before
589 // anything else happens.
590 GURL url("ws://localhost/");
591 TestCompletionCallback dummy;
592 network_delegate_.NotifyBeforeURLRequest(
593 req_.get(), dummy.callback(), &url);
596 TestDelegate delegate_;
597 scoped_ptr<URLRequest> req_;
600 class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper {
601 public:
602 // GoogleMock does not appear to play nicely with move-only types like
603 // scoped_ptr, so this forwarding method acts as a workaround.
604 WebSocketHandshakeStreamBase* CreateBasicStream(
605 scoped_ptr<ClientSocketHandle> connection,
606 bool using_proxy) override {
607 // Discard the arguments since we don't need them anyway.
608 return CreateBasicStreamMock();
611 MOCK_METHOD0(CreateBasicStreamMock,
612 WebSocketHandshakeStreamBase*());
614 MOCK_METHOD2(CreateSpdyStream,
615 WebSocketHandshakeStreamBase*(const base::WeakPtr<SpdySession>&,
616 bool));
619 class FakeWebSocketHandshakeStream : public WebSocketHandshakeStreamBase {
620 public:
621 FakeWebSocketHandshakeStream() : initialize_stream_was_called_(false) {}
623 bool initialize_stream_was_called() const {
624 return initialize_stream_was_called_;
627 // Fake implementation of HttpStreamBase methods.
628 int InitializeStream(const HttpRequestInfo* request_info,
629 RequestPriority priority,
630 const BoundNetLog& net_log,
631 const CompletionCallback& callback) override {
632 initialize_stream_was_called_ = true;
633 return ERR_IO_PENDING;
636 int SendRequest(const HttpRequestHeaders& request_headers,
637 HttpResponseInfo* response,
638 const CompletionCallback& callback) override {
639 return ERR_IO_PENDING;
642 int ReadResponseHeaders(const CompletionCallback& callback) override {
643 return ERR_IO_PENDING;
646 int ReadResponseBody(IOBuffer* buf,
647 int buf_len,
648 const CompletionCallback& callback) override {
649 return ERR_IO_PENDING;
652 void Close(bool not_reusable) override {}
654 bool IsResponseBodyComplete() const override { return false; }
656 bool IsConnectionReused() const override { return false; }
657 void SetConnectionReused() override {}
659 bool CanReuseConnection() const override { return false; }
661 int64 GetTotalReceivedBytes() const override { return 0; }
662 int64_t GetTotalSentBytes() const override { return 0; }
664 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override {
665 return false;
668 void GetSSLInfo(SSLInfo* ssl_info) override {}
670 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override {}
672 void Drain(HttpNetworkSession* session) override {}
674 void SetPriority(RequestPriority priority) override {}
676 UploadProgress GetUploadProgress() const override {
677 return UploadProgress();
680 HttpStream* RenewStreamForAuth() override { return nullptr; }
682 // Fake implementation of WebSocketHandshakeStreamBase method(s)
683 scoped_ptr<WebSocketStream> Upgrade() override {
684 return scoped_ptr<WebSocketStream>();
687 private:
688 bool initialize_stream_was_called_;
691 TEST_F(URLRequestHttpJobWebSocketTest, RejectedWithoutCreateHelper) {
692 scoped_refptr<TestURLRequestHttpJob> job(
693 new TestURLRequestHttpJob(req_.get()));
694 job->Start();
695 base::RunLoop().RunUntilIdle();
696 EXPECT_EQ(URLRequestStatus::FAILED, req_->status().status());
697 EXPECT_EQ(ERR_DISALLOWED_URL_SCHEME, req_->status().error());
700 TEST_F(URLRequestHttpJobWebSocketTest, CreateHelperPassedThrough) {
701 scoped_refptr<TestURLRequestHttpJob> job(
702 new TestURLRequestHttpJob(req_.get()));
703 scoped_ptr<MockCreateHelper> create_helper(
704 new ::testing::StrictMock<MockCreateHelper>());
705 FakeWebSocketHandshakeStream* fake_handshake_stream(
706 new FakeWebSocketHandshakeStream);
707 // Ownership of fake_handshake_stream is transferred when CreateBasicStream()
708 // is called.
709 EXPECT_CALL(*create_helper, CreateBasicStreamMock())
710 .WillOnce(Return(fake_handshake_stream));
711 req_->SetUserData(WebSocketHandshakeStreamBase::CreateHelper::DataKey(),
712 create_helper.release());
713 req_->SetLoadFlags(LOAD_DISABLE_CACHE);
714 job->Start();
715 base::RunLoop().RunUntilIdle();
716 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status());
717 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called());
720 } // namespace
722 } // namespace net