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