| 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_transaction_unittest.h" | 5 #include "net/http/http_transaction_unittest.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 14 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
| 15 #include "net/http/http_cache.h" | 15 #include "net/http/http_cache.h" |
| 16 #include "net/http/http_request_info.h" | 16 #include "net/http/http_request_info.h" |
| 17 #include "net/http/http_response_info.h" | 17 #include "net/http/http_response_info.h" |
| 18 #include "net/http/http_transaction.h" | 18 #include "net/http/http_transaction.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; | 22 typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; |
| 23 static MockTransactionMap mock_transactions; | 23 static MockTransactionMap mock_transactions; |
| 24 } // namespace | 24 } // namespace |
| 25 | 25 |
| 26 //----------------------------------------------------------------------------- | 26 //----------------------------------------------------------------------------- |
| 27 // mock transaction data | 27 // mock transaction data |
| 28 | 28 |
| 29 const MockTransaction kSimpleGET_Transaction = { | 29 const MockTransaction kSimpleGET_Transaction = { |
| 30 net::OK, | |
| 31 "http://www.google.com/", | 30 "http://www.google.com/", |
| 32 "GET", | 31 "GET", |
| 33 base::Time(), | 32 base::Time(), |
| 34 "", | 33 "", |
| 35 net::LOAD_NORMAL, | 34 net::LOAD_NORMAL, |
| 36 "HTTP/1.1 200 OK", | 35 "HTTP/1.1 200 OK", |
| 37 "Cache-Control: max-age=10000\n", | 36 "Cache-Control: max-age=10000\n", |
| 38 base::Time(), | 37 base::Time(), |
| 39 "<html><body>Google Blah Blah</body></html>", | 38 "<html><body>Google Blah Blah</body></html>", |
| 40 TEST_MODE_NORMAL, | 39 TEST_MODE_NORMAL, |
| 41 NULL, | 40 NULL, |
| 42 0 | 41 0 |
| 43 }; | 42 }; |
| 44 | 43 |
| 45 const MockTransaction kSimplePOST_Transaction = { | 44 const MockTransaction kSimplePOST_Transaction = { |
| 46 net::OK, | |
| 47 "http://bugdatabase.com/edit", | 45 "http://bugdatabase.com/edit", |
| 48 "POST", | 46 "POST", |
| 49 base::Time(), | 47 base::Time(), |
| 50 "", | 48 "", |
| 51 net::LOAD_NORMAL, | 49 net::LOAD_NORMAL, |
| 52 "HTTP/1.1 200 OK", | 50 "HTTP/1.1 200 OK", |
| 53 "", | 51 "", |
| 54 base::Time(), | 52 base::Time(), |
| 55 "<html><body>Google Blah Blah</body></html>", | 53 "<html><body>Google Blah Blah</body></html>", |
| 56 TEST_MODE_NORMAL, | 54 TEST_MODE_NORMAL, |
| 57 NULL, | 55 NULL, |
| 58 0 | 56 0 |
| 59 }; | 57 }; |
| 60 | 58 |
| 61 const MockTransaction kTypicalGET_Transaction = { | 59 const MockTransaction kTypicalGET_Transaction = { |
| 62 net::OK, | |
| 63 "http://www.example.com/~foo/bar.html", | 60 "http://www.example.com/~foo/bar.html", |
| 64 "GET", | 61 "GET", |
| 65 base::Time(), | 62 base::Time(), |
| 66 "", | 63 "", |
| 67 net::LOAD_NORMAL, | 64 net::LOAD_NORMAL, |
| 68 "HTTP/1.1 200 OK", | 65 "HTTP/1.1 200 OK", |
| 69 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | 66 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 70 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", | 67 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", |
| 71 base::Time(), | 68 base::Time(), |
| 72 "<html><body>Google Blah Blah</body></html>", | 69 "<html><body>Google Blah Blah</body></html>", |
| 73 TEST_MODE_NORMAL, | 70 TEST_MODE_NORMAL, |
| 74 NULL, | 71 NULL, |
| 75 0 | 72 0 |
| 76 }; | 73 }; |
| 77 | 74 |
| 78 const MockTransaction kETagGET_Transaction = { | 75 const MockTransaction kETagGET_Transaction = { |
| 79 net::OK, | |
| 80 "http://www.google.com/foopy", | 76 "http://www.google.com/foopy", |
| 81 "GET", | 77 "GET", |
| 82 base::Time(), | 78 base::Time(), |
| 83 "", | 79 "", |
| 84 net::LOAD_NORMAL, | 80 net::LOAD_NORMAL, |
| 85 "HTTP/1.1 200 OK", | 81 "HTTP/1.1 200 OK", |
| 86 "Cache-Control: max-age=10000\n" | 82 "Cache-Control: max-age=10000\n" |
| 87 "Etag: \"foopy\"\n", | 83 "Etag: \"foopy\"\n", |
| 88 base::Time(), | 84 base::Time(), |
| 89 "<html><body>Google Blah Blah</body></html>", | 85 "<html><body>Google Blah Blah</body></html>", |
| 90 TEST_MODE_NORMAL, | 86 TEST_MODE_NORMAL, |
| 91 NULL, | 87 NULL, |
| 92 0 | 88 0 |
| 93 }; | 89 }; |
| 94 | 90 |
| 95 const MockTransaction kRangeGET_Transaction = { | 91 const MockTransaction kRangeGET_Transaction = { |
| 96 net::OK, | |
| 97 "http://www.google.com/", | 92 "http://www.google.com/", |
| 98 "GET", | 93 "GET", |
| 99 base::Time(), | 94 base::Time(), |
| 100 "Range: 0-100\r\n", | 95 "Range: 0-100\r\n", |
| 101 net::LOAD_NORMAL, | 96 net::LOAD_NORMAL, |
| 102 "HTTP/1.1 200 OK", | 97 "HTTP/1.1 200 OK", |
| 103 "Cache-Control: max-age=10000\n", | 98 "Cache-Control: max-age=10000\n", |
| 104 base::Time(), | 99 base::Time(), |
| 105 "<html><body>Google Blah Blah</body></html>", | 100 "<html><body>Google Blah Blah</body></html>", |
| 106 TEST_MODE_NORMAL, | 101 TEST_MODE_NORMAL, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 124 | 119 |
| 125 // look for builtins: | 120 // look for builtins: |
| 126 for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { | 121 for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { |
| 127 if (url == GURL(kBuiltinMockTransactions[i]->url)) | 122 if (url == GURL(kBuiltinMockTransactions[i]->url)) |
| 128 return kBuiltinMockTransactions[i]; | 123 return kBuiltinMockTransactions[i]; |
| 129 } | 124 } |
| 130 return NULL; | 125 return NULL; |
| 131 } | 126 } |
| 132 | 127 |
| 133 void AddMockTransaction(const MockTransaction* trans) { | 128 void AddMockTransaction(const MockTransaction* trans) { |
| 134 // To return a result asynchronously, set the TEST_MODE_SYNC_NET_START bit | |
| 135 // of |test_mode|. | |
| 136 ASSERT_NE(net::ERR_IO_PENDING, trans->start_result); | |
| 137 mock_transactions[GURL(trans->url).spec()] = trans; | 129 mock_transactions[GURL(trans->url).spec()] = trans; |
| 138 } | 130 } |
| 139 | 131 |
| 140 void RemoveMockTransaction(const MockTransaction* trans) { | 132 void RemoveMockTransaction(const MockTransaction* trans) { |
| 141 mock_transactions.erase(GURL(trans->url).spec()); | 133 mock_transactions.erase(GURL(trans->url).spec()); |
| 142 } | 134 } |
| 143 | 135 |
| 144 MockHttpRequest::MockHttpRequest(const MockTransaction& t) { | 136 MockHttpRequest::MockHttpRequest(const MockTransaction& t) { |
| 145 url = GURL(t.url); | 137 url = GURL(t.url); |
| 146 method = t.method; | 138 method = t.method; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 223 |
| 232 MockNetworkTransaction::~MockNetworkTransaction() {} | 224 MockNetworkTransaction::~MockNetworkTransaction() {} |
| 233 | 225 |
| 234 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, | 226 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, |
| 235 const net::CompletionCallback& callback, | 227 const net::CompletionCallback& callback, |
| 236 const net::BoundNetLog& net_log) { | 228 const net::BoundNetLog& net_log) { |
| 237 const MockTransaction* t = FindMockTransaction(request->url); | 229 const MockTransaction* t = FindMockTransaction(request->url); |
| 238 if (!t) | 230 if (!t) |
| 239 return net::ERR_FAILED; | 231 return net::ERR_FAILED; |
| 240 | 232 |
| 241 net::Error result = t->start_result; | 233 std::string resp_status = t->status; |
| 234 std::string resp_headers = t->response_headers; |
| 235 std::string resp_data = t->data; |
| 236 if (t->handler) |
| 237 (t->handler)(request, &resp_status, &resp_headers, &resp_data); |
| 238 |
| 239 std::string header_data = base::StringPrintf( |
| 240 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); |
| 241 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); |
| 242 |
| 243 response_.request_time = base::Time::Now(); |
| 244 if (!t->request_time.is_null()) |
| 245 response_.request_time = t->request_time; |
| 246 |
| 247 response_.was_cached = false; |
| 248 |
| 249 response_.response_time = base::Time::Now(); |
| 250 if (!t->response_time.is_null()) |
| 251 response_.response_time = t->response_time; |
| 252 |
| 253 response_.headers = new net::HttpResponseHeaders(header_data); |
| 254 response_.ssl_info.cert_status = t->cert_status; |
| 255 data_ = resp_data; |
| 242 test_mode_ = t->test_mode; | 256 test_mode_ = t->test_mode; |
| 243 if (result == net::OK) { | |
| 244 std::string resp_status = t->status; | |
| 245 std::string resp_headers = t->response_headers; | |
| 246 std::string resp_data = t->data; | |
| 247 if (t->handler) | |
| 248 (t->handler)(request, &resp_status, &resp_headers, &resp_data); | |
| 249 | |
| 250 std::string header_data = base::StringPrintf( | |
| 251 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); | |
| 252 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); | |
| 253 | |
| 254 response_.request_time = base::Time::Now(); | |
| 255 if (!t->request_time.is_null()) | |
| 256 response_.request_time = t->request_time; | |
| 257 | |
| 258 response_.was_cached = false; | |
| 259 | |
| 260 response_.response_time = base::Time::Now(); | |
| 261 if (!t->response_time.is_null()) | |
| 262 response_.response_time = t->response_time; | |
| 263 | |
| 264 response_.headers = new net::HttpResponseHeaders(header_data); | |
| 265 response_.ssl_info.cert_status = t->cert_status; | |
| 266 data_ = resp_data; | |
| 267 } | |
| 268 | 257 |
| 269 if (test_mode_ & TEST_MODE_SYNC_NET_START) | 258 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
| 270 return result; | 259 return net::OK; |
| 271 | 260 |
| 272 CallbackLater(callback, result); | 261 CallbackLater(callback, net::OK); |
| 273 return net::ERR_IO_PENDING; | 262 return net::ERR_IO_PENDING; |
| 274 } | 263 } |
| 275 | 264 |
| 276 int MockNetworkTransaction::RestartIgnoringLastError( | 265 int MockNetworkTransaction::RestartIgnoringLastError( |
| 277 const net::CompletionCallback& callback) { | 266 const net::CompletionCallback& callback) { |
| 278 return net::ERR_FAILED; | 267 return net::ERR_FAILED; |
| 279 } | 268 } |
| 280 | 269 |
| 281 int MockNetworkTransaction::RestartWithCertificate( | 270 int MockNetworkTransaction::RestartWithCertificate( |
| 282 net::X509Certificate* client_cert, | 271 net::X509Certificate* client_cert, |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 | 373 |
| 385 if (rv > 0) | 374 if (rv > 0) |
| 386 content.append(buf->data(), rv); | 375 content.append(buf->data(), rv); |
| 387 else if (rv < 0) | 376 else if (rv < 0) |
| 388 return rv; | 377 return rv; |
| 389 } while (rv > 0); | 378 } while (rv > 0); |
| 390 | 379 |
| 391 result->swap(content); | 380 result->swap(content); |
| 392 return net::OK; | 381 return net::OK; |
| 393 } | 382 } |
| OLD | NEW |