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/url_request/url_request_job.h" | 5 #include "net/url_request/url_request_job.h" |
6 | 6 |
| 7 #include "base/basictypes.h" |
7 #include "net/http/http_transaction_unittest.h" | 8 #include "net/http/http_transaction_unittest.h" |
8 #include "net/url_request/url_request_test_util.h" | 9 #include "net/url_request/url_request_test_util.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 // This is a header that signals the end of the data. | 14 // This is a header that signals the end of the data. |
14 const char kGzipGata[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; | 15 const char kGzipGata[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; |
15 | 16 |
16 void GZipServer(const net::HttpRequestInfo* request, | 17 void GZipServer(const net::HttpRequestInfo* request, |
17 std::string* response_status, std::string* response_headers, | 18 std::string* response_status, std::string* response_headers, |
18 std::string* response_data) { | 19 std::string* response_data) { |
19 response_data->assign(kGzipGata, sizeof(kGzipGata)); | 20 response_data->assign(kGzipGata, sizeof(kGzipGata)); |
20 } | 21 } |
21 | 22 |
| 23 void NullServer(const net::HttpRequestInfo* request, |
| 24 std::string* response_status, |
| 25 std::string* response_headers, |
| 26 std::string* response_data) { |
| 27 FAIL(); |
| 28 } |
| 29 |
22 const MockTransaction kGZip_Transaction = { | 30 const MockTransaction kGZip_Transaction = { |
| 31 net::OK, |
23 "http://www.google.com/gzyp", | 32 "http://www.google.com/gzyp", |
24 "GET", | 33 "GET", |
25 base::Time(), | 34 base::Time(), |
26 "", | 35 "", |
27 net::LOAD_NORMAL, | 36 net::LOAD_NORMAL, |
28 "HTTP/1.1 200 OK", | 37 "HTTP/1.1 200 OK", |
29 "Cache-Control: max-age=10000\n" | 38 "Cache-Control: max-age=10000\n" |
30 "Content-Encoding: gzip\n" | 39 "Content-Encoding: gzip\n" |
31 "Content-Length: 30\n", // Intentionally wrong. | 40 "Content-Length: 30\n", // Intentionally wrong. |
32 base::Time(), | 41 base::Time(), |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 79 |
71 req.set_method("GET"); | 80 req.set_method("GET"); |
72 req.Start(); | 81 req.Start(); |
73 | 82 |
74 MessageLoop::current()->Run(); | 83 MessageLoop::current()->Run(); |
75 | 84 |
76 EXPECT_TRUE(network_layer.done_reading_called()); | 85 EXPECT_TRUE(network_layer.done_reading_called()); |
77 | 86 |
78 RemoveMockTransaction(&transaction); | 87 RemoveMockTransaction(&transaction); |
79 } | 88 } |
| 89 |
| 90 // Tests that automatic retry triggers for certain errors on GETs, and is not |
| 91 // triggered for other errors or with other methods. |
| 92 TEST(URLRequestJob, RetryRequests) { |
| 93 const struct { |
| 94 net::Error transaction_result; |
| 95 const char* method; |
| 96 int expected_transaction_count; |
| 97 } tests[] = { |
| 98 {net::ERR_EMPTY_RESPONSE, "GET", 2}, |
| 99 {net::ERR_CONNECTION_REFUSED, "GET", 2}, |
| 100 {net::ERR_CONNECTION_REFUSED, "POST", 1}, |
| 101 {net::ERR_CONNECTION_REFUSED, "PUT", 1}, |
| 102 {net::ERR_ACCESS_DENIED, "GET", 1}, |
| 103 {net::ERR_CONTENT_DECODING_FAILED, "GET", 1}, |
| 104 }; |
| 105 |
| 106 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 107 const MockTransaction dead_transaction = { |
| 108 tests[i].transaction_result, |
| 109 "http://www.dead.com/", |
| 110 tests[i].method, |
| 111 base::Time(), |
| 112 "", |
| 113 net::LOAD_NORMAL, |
| 114 "", |
| 115 "", |
| 116 base::Time(), |
| 117 "", |
| 118 TEST_MODE_NORMAL, |
| 119 &NullServer, |
| 120 0 |
| 121 }; |
| 122 |
| 123 MockNetworkLayer network_layer; |
| 124 TestURLRequestContext context; |
| 125 context.set_http_transaction_factory(&network_layer); |
| 126 |
| 127 TestDelegate d; |
| 128 TestURLRequest req(GURL(dead_transaction.url), &d, &context); |
| 129 MockTransaction transaction(dead_transaction); |
| 130 transaction.test_mode = TEST_MODE_SYNC_ALL; |
| 131 AddMockTransaction(&transaction); |
| 132 |
| 133 req.set_method(tests[i].method); |
| 134 req.Start(); |
| 135 |
| 136 MessageLoop::current()->Run(); |
| 137 |
| 138 EXPECT_EQ(tests[i].expected_transaction_count, |
| 139 network_layer.transaction_count()); |
| 140 |
| 141 RemoveMockTransaction(&transaction); |
| 142 } |
| 143 } |
OLD | NEW |