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 "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "net/base/net_log.h" | 8 #include "net/base/net_log.h" |
9 #include "net/cert/mock_cert_verifier.h" | 9 #include "net/cert/mock_cert_verifier.h" |
10 #include "net/dns/mock_host_resolver.h" | 10 #include "net/dns/mock_host_resolver.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 return HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN)).ToString(); | 53 return HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN)).ToString(); |
54 } | 54 } |
55 #endif | 55 #endif |
56 | 56 |
57 #if defined(SPDY_PROXY_AUTH_ORIGIN) && defined(DATA_REDUCTION_FALLBACK_HOST) | 57 #if defined(SPDY_PROXY_AUTH_ORIGIN) && defined(DATA_REDUCTION_FALLBACK_HOST) |
58 std::string GetChromeFallbackProxy() { | 58 std::string GetChromeFallbackProxy() { |
59 return HostPortPair::FromURL(GURL(DATA_REDUCTION_FALLBACK_HOST)).ToString(); | 59 return HostPortPair::FromURL(GURL(DATA_REDUCTION_FALLBACK_HOST)).ToString(); |
60 } | 60 } |
61 #endif | 61 #endif |
62 | 62 |
63 void ExecuteRequestExpectingContentAndHeader(const std::string& content, | 63 void ExecuteRequestExpectingContentAndHeader(const std::string& method, |
64 const std::string& content, | |
64 const std::string& header, | 65 const std::string& header, |
65 const std::string& value) { | 66 const std::string& value) { |
66 TestCompletionCallback callback; | 67 TestCompletionCallback callback; |
67 | 68 |
68 HttpRequestInfo request_info; | 69 HttpRequestInfo request_info; |
69 request_info.url = GURL("http://www.google.com/"); | 70 request_info.url = GURL("http://www.google.com/"); |
70 request_info.method = "GET"; | 71 request_info.method = method; |
71 request_info.load_flags = LOAD_NORMAL; | 72 request_info.load_flags = LOAD_NORMAL; |
72 | 73 |
73 scoped_ptr<HttpTransaction> trans; | 74 scoped_ptr<HttpTransaction> trans; |
74 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans, NULL); | 75 int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans, NULL); |
75 EXPECT_EQ(OK, rv); | 76 EXPECT_EQ(OK, rv); |
76 | 77 |
77 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); | 78 rv = trans->Start(&request_info, callback.callback(), BoundNetLog()); |
78 if (rv == ERR_IO_PENDING) | 79 if (rv == ERR_IO_PENDING) |
79 rv = callback.WaitForResult(); | 80 rv = callback.WaitForResult(); |
80 ASSERT_EQ(OK, rv); | 81 ASSERT_EQ(OK, rv); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 }; | 116 }; |
116 TestProxyFallbackWithMockReads(bad_proxy, "", data_reads, | 117 TestProxyFallbackWithMockReads(bad_proxy, "", data_reads, |
117 arraysize(data_reads), 1u); | 118 arraysize(data_reads), 1u); |
118 } | 119 } |
119 | 120 |
120 void TestProxyFallbackWithMockReads(const std::string& bad_proxy, | 121 void TestProxyFallbackWithMockReads(const std::string& bad_proxy, |
121 const std::string& bad_proxy2, | 122 const std::string& bad_proxy2, |
122 MockRead data_reads[], | 123 MockRead data_reads[], |
123 int data_reads_size, | 124 int data_reads_size, |
124 unsigned int expected_retry_info_size) { | 125 unsigned int expected_retry_info_size) { |
126 TestProxyFallbackByMethodWithMockReads(bad_proxy, bad_proxy2, data_reads, | |
127 data_reads_size, "GET", "content", | |
128 true, expected_retry_info_size); | |
129 | |
130 } | |
131 | |
132 void TestProxyFallbackByMethodWithMockReads( | |
133 const std::string& bad_proxy, | |
134 const std::string& bad_proxy2, | |
135 MockRead data_reads[], | |
136 int data_reads_size, | |
137 std::string method, | |
138 std::string content, | |
139 bool retry_expected, | |
140 unsigned int expected_retry_info_size) { | |
141 | |
142 std::string trailer = | |
143 (method == "HEAD" || method == "PUT" || method == "POST") ? | |
144 "Content-Length: 0\r\n\r\n" : "\r\n"; | |
145 std::string request = | |
146 base::StringPrintf("%s http://www.google.com/ HTTP/1.1\r\n" | |
147 "Host: www.google.com\r\n" | |
148 "Proxy-Connection: keep-alive\r\n" | |
149 "%s", method.c_str(), trailer.c_str()); | |
150 | |
125 MockWrite data_writes[] = { | 151 MockWrite data_writes[] = { |
126 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | 152 MockWrite(request.c_str()), |
127 "Host: www.google.com\r\n" | |
128 "Proxy-Connection: keep-alive\r\n\r\n"), | |
129 }; | 153 }; |
130 | 154 |
131 StaticSocketDataProvider data1(data_reads, data_reads_size, | 155 StaticSocketDataProvider data1(data_reads, data_reads_size, |
132 data_writes, arraysize(data_writes)); | 156 data_writes, arraysize(data_writes)); |
133 mock_socket_factory_.AddSocketDataProvider(&data1); | 157 mock_socket_factory_.AddSocketDataProvider(&data1); |
134 | 158 |
135 // Second data provider returns the expected content. | 159 // Second data provider returns the expected content. |
136 MockRead data_reads2[] = { | 160 MockRead data_reads2[3]; |
137 MockRead("HTTP/1.0 200 OK\r\n" | 161 size_t data_reads2_index = 0; |
138 "Server: not-proxy\r\n\r\n"), | 162 data_reads2[data_reads2_index++] = MockRead("HTTP/1.0 200 OK\r\n" |
139 MockRead("content"), | 163 "Server: not-proxy\r\n\r\n"); |
140 MockRead(SYNCHRONOUS, OK), | 164 if (!content.empty()) |
165 data_reads2[data_reads2_index++] = MockRead(content.c_str()); | |
166 data_reads2[data_reads2_index++] = MockRead(SYNCHRONOUS, OK); | |
167 | |
mef
2013/12/18 20:36:23
nit: blank line
bengr
2013/12/18 21:05:10
Done.
| |
168 | |
169 MockWrite data_writes2[] = { | |
170 MockWrite(request.c_str()), | |
141 }; | 171 }; |
142 MockWrite data_writes2[] = { | 172 StaticSocketDataProvider data2(data_reads2, data_reads2_index, |
143 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | |
144 "Host: www.google.com\r\n" | |
145 "Proxy-Connection: keep-alive\r\n\r\n"), | |
146 }; | |
147 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
148 data_writes2, arraysize(data_writes2)); | 173 data_writes2, arraysize(data_writes2)); |
149 mock_socket_factory_.AddSocketDataProvider(&data2); | 174 mock_socket_factory_.AddSocketDataProvider(&data2); |
150 | 175 |
151 // Expect that we get "content" and not "Bypass message", and that there's | 176 // Expect that we get "content" and not "Bypass message", and that there's |
152 // a "not-proxy" "Server:" header in the final response. | 177 // a "not-proxy" "Server:" header in the final response. |
153 ExecuteRequestExpectingContentAndHeader("content", "server", "not-proxy"); | 178 if (retry_expected) { |
179 ExecuteRequestExpectingContentAndHeader(method, content, | |
180 "server", "not-proxy"); | |
181 } else { | |
182 ExecuteRequestExpectingContentAndHeader(method, "Bypass message", "", ""); | |
183 } | |
154 | 184 |
155 // We should also observe the bad proxy in the retry list. | 185 // We should also observe the bad proxy in the retry list. |
156 TestBadProxies(expected_retry_info_size, bad_proxy, bad_proxy2); | 186 TestBadProxies(expected_retry_info_size, bad_proxy, bad_proxy2); |
157 } | 187 } |
158 | 188 |
159 // Simulates a request through a proxy which returns a bypass, which is then | 189 // Simulates a request through a proxy which returns a bypass, which is then |
160 // retried through a direct connection to the origin site. | 190 // retried through a direct connection to the origin site. |
161 // Checks that the expected requests were issued, the expected content was | 191 // Checks that the expected requests were issued, the expected content was |
162 // received, and the proxy |bad_proxy| was marked as bad. | 192 // received, and the proxy |bad_proxy| was marked as bad. |
163 void TestProxyFallbackToDirect(const std::string& bad_proxy) { | 193 void TestProxyFallbackToDirect(const std::string& bad_proxy) { |
(...skipping 23 matching lines...) Expand all Loading... | |
187 MockWrite("GET / HTTP/1.1\r\n" | 217 MockWrite("GET / HTTP/1.1\r\n" |
188 "Host: www.google.com\r\n" | 218 "Host: www.google.com\r\n" |
189 "Connection: keep-alive\r\n\r\n"), | 219 "Connection: keep-alive\r\n\r\n"), |
190 }; | 220 }; |
191 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | 221 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
192 data_writes2, arraysize(data_writes2)); | 222 data_writes2, arraysize(data_writes2)); |
193 mock_socket_factory_.AddSocketDataProvider(&data2); | 223 mock_socket_factory_.AddSocketDataProvider(&data2); |
194 | 224 |
195 // Expect that we get "content" and not "Bypass message", and that there's | 225 // Expect that we get "content" and not "Bypass message", and that there's |
196 // a "not-proxy" "Server:" header in the final response. | 226 // a "not-proxy" "Server:" header in the final response. |
197 ExecuteRequestExpectingContentAndHeader("content", "server", "not-proxy"); | 227 ExecuteRequestExpectingContentAndHeader("GET", "content", |
228 "server", "not-proxy"); | |
198 | 229 |
199 // We should also observe the bad proxy in the retry list. | 230 // We should also observe the bad proxy in the retry list. |
200 TestBadProxies(1u, bad_proxy, ""); | 231 TestBadProxies(1u, bad_proxy, ""); |
201 } | 232 } |
202 | 233 |
203 // Simulates a request through a proxy which returns a bypass, under a | 234 // Simulates a request through a proxy which returns a bypass, under a |
204 // configuration where there is no valid bypass. |proxy_count| proxies | 235 // configuration where there is no valid bypass. |proxy_count| proxies |
205 // are expected to be configured. | 236 // are expected to be configured. |
206 // Checks that the expected requests were issued, the bypass message was the | 237 // Checks that the expected requests were issued, the bypass message was the |
207 // final received content, and all proxies were marked as bad. | 238 // final received content, and all proxies were marked as bad. |
(...skipping 14 matching lines...) Expand all Loading... | |
222 StaticSocketDataProvider data1(data_reads, arraysize(data_reads), | 253 StaticSocketDataProvider data1(data_reads, arraysize(data_reads), |
223 data_writes, arraysize(data_writes)); | 254 data_writes, arraysize(data_writes)); |
224 StaticSocketDataProvider data2(data_reads, arraysize(data_reads), | 255 StaticSocketDataProvider data2(data_reads, arraysize(data_reads), |
225 data_writes, arraysize(data_writes)); | 256 data_writes, arraysize(data_writes)); |
226 | 257 |
227 mock_socket_factory_.AddSocketDataProvider(&data1); | 258 mock_socket_factory_.AddSocketDataProvider(&data1); |
228 if (proxy_count > 1) | 259 if (proxy_count > 1) |
229 mock_socket_factory_.AddSocketDataProvider(&data2); | 260 mock_socket_factory_.AddSocketDataProvider(&data2); |
230 | 261 |
231 // Expect that we get "Bypass message", and not "content".. | 262 // Expect that we get "Bypass message", and not "content".. |
232 ExecuteRequestExpectingContentAndHeader("Bypass message", "", ""); | 263 ExecuteRequestExpectingContentAndHeader("GET", "Bypass message", "", ""); |
233 | 264 |
234 // We should also observe the bad proxy or proxies in the retry list. | 265 // We should also observe the bad proxy or proxies in the retry list. |
235 TestBadProxies(proxy_count, bad_proxy, bad_proxy2); | 266 TestBadProxies(proxy_count, bad_proxy, bad_proxy2); |
236 } | 267 } |
237 | 268 |
238 MockClientSocketFactory mock_socket_factory_; | 269 MockClientSocketFactory mock_socket_factory_; |
239 MockHostResolver host_resolver_; | 270 MockHostResolver host_resolver_; |
240 scoped_ptr<CertVerifier> cert_verifier_; | 271 scoped_ptr<CertVerifier> cert_verifier_; |
241 scoped_ptr<TransportSecurityState> transport_security_state_; | 272 scoped_ptr<TransportSecurityState> transport_security_state_; |
242 scoped_ptr<ProxyService> proxy_service_; | 273 scoped_ptr<ProxyService> proxy_service_; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 TestProxyFallback(bad_proxy); | 362 TestProxyFallback(bad_proxy); |
332 } | 363 } |
333 | 364 |
334 TEST_F(HttpNetworkLayerTest, ServerTwoProxyBypassFixed) { | 365 TEST_F(HttpNetworkLayerTest, ServerTwoProxyBypassFixed) { |
335 std::string bad_proxy = GetChromeProxy(); | 366 std::string bad_proxy = GetChromeProxy(); |
336 ConfigureTestDependencies( | 367 ConfigureTestDependencies( |
337 ProxyService::CreateFixed(bad_proxy +", good:8080")); | 368 ProxyService::CreateFixed(bad_proxy +", good:8080")); |
338 TestProxyFallback(bad_proxy); | 369 TestProxyFallback(bad_proxy); |
339 } | 370 } |
340 | 371 |
372 TEST_F(HttpNetworkLayerTest, BypassAndRetryIdempotentMethods) { | |
373 std::string bad_proxy = GetChromeProxy(); | |
374 const struct { | |
375 std::string method; | |
376 std::string content; | |
377 bool expected_to_retry; | |
378 } tests[] = { | |
379 { | |
380 "GET", | |
381 "content", | |
382 true, | |
383 }, | |
384 { | |
385 "OPTIONS", | |
386 "content", | |
387 true, | |
388 }, | |
389 { | |
390 "HEAD", | |
391 "", | |
392 true, | |
393 }, | |
394 { | |
395 "PUT", | |
396 "", | |
397 true, | |
398 }, | |
399 { | |
400 "DELETE", | |
401 "content", | |
402 true, | |
403 }, | |
404 { | |
405 "TRACE", | |
406 "content", | |
407 true, | |
408 }, | |
409 { | |
410 "POST", | |
411 "Bypass message", | |
412 false, | |
413 }, | |
414 }; | |
415 | |
416 for (size_t i = 0; i < arraysize(tests); ++i) { | |
417 ConfigureTestDependencies( | |
418 ProxyService::CreateFixed(bad_proxy +", good:8080")); | |
419 MockRead data_reads[] = { | |
420 MockRead("HTTP/1.1 200 OK\r\n" | |
421 "Chrome-Proxy: bypass=0\r\n\r\n"), | |
422 MockRead("Bypass message"), | |
423 MockRead(SYNCHRONOUS, OK), | |
424 }; | |
425 TestProxyFallbackByMethodWithMockReads(bad_proxy, "", data_reads, | |
426 arraysize(data_reads), | |
427 tests[i].method, | |
428 tests[i].content, | |
429 tests[i].expected_to_retry, 1u); | |
430 } | |
431 } | |
432 | |
341 TEST_F(HttpNetworkLayerTest, ServerOneProxyWithDirectBypassPac) { | 433 TEST_F(HttpNetworkLayerTest, ServerOneProxyWithDirectBypassPac) { |
342 std::string bad_proxy = GetChromeProxy(); | 434 std::string bad_proxy = GetChromeProxy(); |
343 ConfigureTestDependencies(ProxyService::CreateFixedFromPacResult( | 435 ConfigureTestDependencies(ProxyService::CreateFixedFromPacResult( |
344 "PROXY " + bad_proxy + "; DIRECT")); | 436 "PROXY " + bad_proxy + "; DIRECT")); |
345 TestProxyFallbackToDirect(bad_proxy); | 437 TestProxyFallbackToDirect(bad_proxy); |
346 } | 438 } |
347 | 439 |
348 TEST_F(HttpNetworkLayerTest, ServerOneProxyWithDirectBypassFixed) { | 440 TEST_F(HttpNetworkLayerTest, ServerOneProxyWithDirectBypassFixed) { |
349 std::string bad_proxy = GetChromeProxy(); | 441 std::string bad_proxy = GetChromeProxy(); |
350 ConfigureTestDependencies( | 442 ConfigureTestDependencies( |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 ASSERT_EQ(ERR_CONNECTION_RESET, callback.GetResult(rv)); | 727 ASSERT_EQ(ERR_CONNECTION_RESET, callback.GetResult(rv)); |
636 | 728 |
637 // If the response info is null, that means that any consumer won't | 729 // If the response info is null, that means that any consumer won't |
638 // see the network accessed bit set. | 730 // see the network accessed bit set. |
639 EXPECT_EQ(NULL, trans->GetResponseInfo()); | 731 EXPECT_EQ(NULL, trans->GetResponseInfo()); |
640 } | 732 } |
641 | 733 |
642 } // namespace | 734 } // namespace |
643 | 735 |
644 } // namespace net | 736 } // namespace net |
OLD | NEW |