OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_network_layer.h" | 5 #include "net/http/http_network_layer.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "net/cert/ct_policy_enforcer.h" | 10 #include "net/cert/ct_policy_enforcer.h" |
11 #include "net/cert/mock_cert_verifier.h" | 11 #include "net/cert/mock_cert_verifier.h" |
12 #include "net/cert/multi_log_ct_verifier.h" | 12 #include "net/cert/multi_log_ct_verifier.h" |
13 #include "net/dns/mock_host_resolver.h" | 13 #include "net/dns/mock_host_resolver.h" |
14 #include "net/http/http_network_session.h" | 14 #include "net/http/http_network_session.h" |
15 #include "net/http/http_server_properties_impl.h" | 15 #include "net/http/http_server_properties_impl.h" |
16 #include "net/http/http_transaction_test_util.h" | 16 #include "net/http/http_transaction_test_util.h" |
17 #include "net/http/transport_security_state.h" | 17 #include "net/http/transport_security_state.h" |
18 #include "net/log/net_log.h" | 18 #include "net/log/net_log.h" |
19 #include "net/proxy/proxy_service.h" | 19 #include "net/proxy/proxy_service.h" |
20 #include "net/socket/socket_test_util.h" | 20 #include "net/socket/socket_test_util.h" |
21 #include "net/spdy/spdy_session_pool.h" | 21 #include "net/spdy/spdy_session_pool.h" |
22 #include "net/ssl/ssl_config_service_defaults.h" | 22 #include "net/ssl/ssl_config_service_defaults.h" |
| 23 #include "net/test/gtest_util.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "testing/platform_test.h" | 26 #include "testing/platform_test.h" |
25 | 27 |
| 28 using net::test::IsError; |
| 29 using net::test::IsOk; |
| 30 |
26 namespace net { | 31 namespace net { |
27 | 32 |
28 namespace { | 33 namespace { |
29 | 34 |
30 class HttpNetworkLayerTest : public PlatformTest { | 35 class HttpNetworkLayerTest : public PlatformTest { |
31 protected: | 36 protected: |
32 HttpNetworkLayerTest() : ssl_config_service_(new SSLConfigServiceDefaults) {} | 37 HttpNetworkLayerTest() : ssl_config_service_(new SSLConfigServiceDefaults) {} |
33 | 38 |
34 void SetUp() override { | 39 void SetUp() override { |
35 ConfigureTestDependencies(ProxyService::CreateDirect()); | 40 ConfigureTestDependencies(ProxyService::CreateDirect()); |
(...skipping 23 matching lines...) Expand all Loading... |
59 const std::string& value) { | 64 const std::string& value) { |
60 TestCompletionCallback callback; | 65 TestCompletionCallback callback; |
61 | 66 |
62 HttpRequestInfo request_info; | 67 HttpRequestInfo request_info; |
63 request_info.url = GURL("http://www.google.com/"); | 68 request_info.url = GURL("http://www.google.com/"); |
64 request_info.method = method; | 69 request_info.method = method; |
65 request_info.load_flags = LOAD_NORMAL; | 70 request_info.load_flags = LOAD_NORMAL; |
66 | 71 |
67 std::unique_ptr<HttpTransaction> trans; | 72 std::unique_ptr<HttpTransaction> trans; |
68 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); | 73 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); |
69 EXPECT_EQ(OK, rv); | 74 EXPECT_THAT(rv, IsOk()); |
70 | 75 |
71 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); | 76 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); |
72 if (rv == ERR_IO_PENDING) | 77 if (rv == ERR_IO_PENDING) |
73 rv = callback.WaitForResult(); | 78 rv = callback.WaitForResult(); |
74 ASSERT_EQ(OK, rv); | 79 ASSERT_THAT(rv, IsOk()); |
75 | 80 |
76 std::string contents; | 81 std::string contents; |
77 rv = ReadTransaction(trans.get(), &contents); | 82 rv = ReadTransaction(trans.get(), &contents); |
78 EXPECT_EQ(OK, rv); | 83 EXPECT_THAT(rv, IsOk()); |
79 EXPECT_EQ(content, contents); | 84 EXPECT_EQ(content, contents); |
80 | 85 |
81 if (!header.empty()) { | 86 if (!header.empty()) { |
82 // We also have a server header here that isn't set by the proxy. | 87 // We also have a server header here that isn't set by the proxy. |
83 EXPECT_TRUE(trans->GetResponseInfo()->headers->HasHeaderValue( | 88 EXPECT_TRUE(trans->GetResponseInfo()->headers->HasHeaderValue( |
84 header, value)); | 89 header, value)); |
85 } | 90 } |
86 } | 91 } |
87 | 92 |
88 // Check that |proxy_count| proxies are in the retry list. | 93 // Check that |proxy_count| proxies are in the retry list. |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 std::unique_ptr<ProxyService> proxy_service_; | 271 std::unique_ptr<ProxyService> proxy_service_; |
267 const scoped_refptr<SSLConfigService> ssl_config_service_; | 272 const scoped_refptr<SSLConfigService> ssl_config_service_; |
268 std::unique_ptr<HttpNetworkSession> network_session_; | 273 std::unique_ptr<HttpNetworkSession> network_session_; |
269 std::unique_ptr<HttpNetworkLayer> factory_; | 274 std::unique_ptr<HttpNetworkLayer> factory_; |
270 HttpServerPropertiesImpl http_server_properties_; | 275 HttpServerPropertiesImpl http_server_properties_; |
271 }; | 276 }; |
272 | 277 |
273 TEST_F(HttpNetworkLayerTest, CreateAndDestroy) { | 278 TEST_F(HttpNetworkLayerTest, CreateAndDestroy) { |
274 std::unique_ptr<HttpTransaction> trans; | 279 std::unique_ptr<HttpTransaction> trans; |
275 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); | 280 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); |
276 EXPECT_EQ(OK, rv); | 281 EXPECT_THAT(rv, IsOk()); |
277 EXPECT_TRUE(trans.get() != NULL); | 282 EXPECT_TRUE(trans.get() != NULL); |
278 } | 283 } |
279 | 284 |
280 TEST_F(HttpNetworkLayerTest, Suspend) { | 285 TEST_F(HttpNetworkLayerTest, Suspend) { |
281 std::unique_ptr<HttpTransaction> trans; | 286 std::unique_ptr<HttpTransaction> trans; |
282 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); | 287 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); |
283 EXPECT_EQ(OK, rv); | 288 EXPECT_THAT(rv, IsOk()); |
284 | 289 |
285 trans.reset(); | 290 trans.reset(); |
286 | 291 |
287 factory_->OnSuspend(); | 292 factory_->OnSuspend(); |
288 | 293 |
289 rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); | 294 rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); |
290 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, rv); | 295 EXPECT_THAT(rv, IsError(ERR_NETWORK_IO_SUSPENDED)); |
291 | 296 |
292 ASSERT_TRUE(trans == NULL); | 297 ASSERT_TRUE(trans == NULL); |
293 | 298 |
294 factory_->OnResume(); | 299 factory_->OnResume(); |
295 | 300 |
296 rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); | 301 rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); |
297 EXPECT_EQ(OK, rv); | 302 EXPECT_THAT(rv, IsOk()); |
298 } | 303 } |
299 | 304 |
300 TEST_F(HttpNetworkLayerTest, GET) { | 305 TEST_F(HttpNetworkLayerTest, GET) { |
301 MockRead data_reads[] = { | 306 MockRead data_reads[] = { |
302 MockRead("HTTP/1.0 200 OK\r\n\r\n"), | 307 MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
303 MockRead("hello world"), | 308 MockRead("hello world"), |
304 MockRead(SYNCHRONOUS, OK), | 309 MockRead(SYNCHRONOUS, OK), |
305 }; | 310 }; |
306 MockWrite data_writes[] = { | 311 MockWrite data_writes[] = { |
307 MockWrite("GET / HTTP/1.1\r\n" | 312 MockWrite("GET / HTTP/1.1\r\n" |
308 "Host: www.google.com\r\n" | 313 "Host: www.google.com\r\n" |
309 "Connection: keep-alive\r\n" | 314 "Connection: keep-alive\r\n" |
310 "User-Agent: Foo/1.0\r\n\r\n"), | 315 "User-Agent: Foo/1.0\r\n\r\n"), |
311 }; | 316 }; |
312 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | 317 StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
313 data_writes, arraysize(data_writes)); | 318 data_writes, arraysize(data_writes)); |
314 mock_socket_factory_.AddSocketDataProvider(&data); | 319 mock_socket_factory_.AddSocketDataProvider(&data); |
315 | 320 |
316 TestCompletionCallback callback; | 321 TestCompletionCallback callback; |
317 | 322 |
318 HttpRequestInfo request_info; | 323 HttpRequestInfo request_info; |
319 request_info.url = GURL("http://www.google.com/"); | 324 request_info.url = GURL("http://www.google.com/"); |
320 request_info.method = "GET"; | 325 request_info.method = "GET"; |
321 request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, | 326 request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
322 "Foo/1.0"); | 327 "Foo/1.0"); |
323 request_info.load_flags = LOAD_NORMAL; | 328 request_info.load_flags = LOAD_NORMAL; |
324 | 329 |
325 std::unique_ptr<HttpTransaction> trans; | 330 std::unique_ptr<HttpTransaction> trans; |
326 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); | 331 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); |
327 EXPECT_EQ(OK, rv); | 332 EXPECT_THAT(rv, IsOk()); |
328 | 333 |
329 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); | 334 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); |
330 rv = callback.GetResult(rv); | 335 rv = callback.GetResult(rv); |
331 ASSERT_EQ(OK, rv); | 336 ASSERT_THAT(rv, IsOk()); |
332 | 337 |
333 std::string contents; | 338 std::string contents; |
334 rv = ReadTransaction(trans.get(), &contents); | 339 rv = ReadTransaction(trans.get(), &contents); |
335 EXPECT_EQ(OK, rv); | 340 EXPECT_THAT(rv, IsOk()); |
336 EXPECT_EQ("hello world", contents); | 341 EXPECT_EQ("hello world", contents); |
337 } | 342 } |
338 | 343 |
339 TEST_F(HttpNetworkLayerTest, NetworkVerified) { | 344 TEST_F(HttpNetworkLayerTest, NetworkVerified) { |
340 MockRead data_reads[] = { | 345 MockRead data_reads[] = { |
341 MockRead("HTTP/1.0 200 OK\r\n\r\n"), | 346 MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
342 MockRead("hello world"), | 347 MockRead("hello world"), |
343 MockRead(SYNCHRONOUS, OK), | 348 MockRead(SYNCHRONOUS, OK), |
344 }; | 349 }; |
345 MockWrite data_writes[] = { | 350 MockWrite data_writes[] = { |
(...skipping 10 matching lines...) Expand all Loading... |
356 | 361 |
357 HttpRequestInfo request_info; | 362 HttpRequestInfo request_info; |
358 request_info.url = GURL("http://www.google.com/"); | 363 request_info.url = GURL("http://www.google.com/"); |
359 request_info.method = "GET"; | 364 request_info.method = "GET"; |
360 request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, | 365 request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
361 "Foo/1.0"); | 366 "Foo/1.0"); |
362 request_info.load_flags = LOAD_NORMAL; | 367 request_info.load_flags = LOAD_NORMAL; |
363 | 368 |
364 std::unique_ptr<HttpTransaction> trans; | 369 std::unique_ptr<HttpTransaction> trans; |
365 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); | 370 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); |
366 EXPECT_EQ(OK, rv); | 371 EXPECT_THAT(rv, IsOk()); |
367 | 372 |
368 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); | 373 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); |
369 ASSERT_EQ(OK, callback.GetResult(rv)); | 374 ASSERT_THAT(callback.GetResult(rv), IsOk()); |
370 | 375 |
371 EXPECT_TRUE(trans->GetResponseInfo()->network_accessed); | 376 EXPECT_TRUE(trans->GetResponseInfo()->network_accessed); |
372 } | 377 } |
373 | 378 |
374 TEST_F(HttpNetworkLayerTest, NetworkUnVerified) { | 379 TEST_F(HttpNetworkLayerTest, NetworkUnVerified) { |
375 MockRead data_reads[] = { | 380 MockRead data_reads[] = { |
376 MockRead(ASYNC, ERR_CONNECTION_RESET), | 381 MockRead(ASYNC, ERR_CONNECTION_RESET), |
377 }; | 382 }; |
378 MockWrite data_writes[] = { | 383 MockWrite data_writes[] = { |
379 MockWrite("GET / HTTP/1.1\r\n" | 384 MockWrite("GET / HTTP/1.1\r\n" |
380 "Host: www.google.com\r\n" | 385 "Host: www.google.com\r\n" |
381 "Connection: keep-alive\r\n" | 386 "Connection: keep-alive\r\n" |
382 "User-Agent: Foo/1.0\r\n\r\n"), | 387 "User-Agent: Foo/1.0\r\n\r\n"), |
383 }; | 388 }; |
384 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | 389 StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
385 data_writes, arraysize(data_writes)); | 390 data_writes, arraysize(data_writes)); |
386 mock_socket_factory_.AddSocketDataProvider(&data); | 391 mock_socket_factory_.AddSocketDataProvider(&data); |
387 | 392 |
388 TestCompletionCallback callback; | 393 TestCompletionCallback callback; |
389 | 394 |
390 HttpRequestInfo request_info; | 395 HttpRequestInfo request_info; |
391 request_info.url = GURL("http://www.google.com/"); | 396 request_info.url = GURL("http://www.google.com/"); |
392 request_info.method = "GET"; | 397 request_info.method = "GET"; |
393 request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, | 398 request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
394 "Foo/1.0"); | 399 "Foo/1.0"); |
395 request_info.load_flags = LOAD_NORMAL; | 400 request_info.load_flags = LOAD_NORMAL; |
396 | 401 |
397 std::unique_ptr<HttpTransaction> trans; | 402 std::unique_ptr<HttpTransaction> trans; |
398 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); | 403 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans); |
399 EXPECT_EQ(OK, rv); | 404 EXPECT_THAT(rv, IsOk()); |
400 | 405 |
401 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); | 406 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); |
402 ASSERT_EQ(ERR_CONNECTION_RESET, callback.GetResult(rv)); | 407 ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CONNECTION_RESET)); |
403 | 408 |
404 // network_accessed is true; the HTTP stack did try to make a connection. | 409 // network_accessed is true; the HTTP stack did try to make a connection. |
405 EXPECT_TRUE(trans->GetResponseInfo()->network_accessed); | 410 EXPECT_TRUE(trans->GetResponseInfo()->network_accessed); |
406 } | 411 } |
407 | 412 |
408 } // namespace | 413 } // namespace |
409 | 414 |
410 } // namespace net | 415 } // namespace net |
OLD | NEW |