| 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" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 "GET", | 31 "GET", |
| 32 base::Time(), | 32 base::Time(), |
| 33 "", | 33 "", |
| 34 net::LOAD_NORMAL, | 34 net::LOAD_NORMAL, |
| 35 "HTTP/1.1 200 OK", | 35 "HTTP/1.1 200 OK", |
| 36 "Cache-Control: max-age=10000\n", | 36 "Cache-Control: max-age=10000\n", |
| 37 base::Time(), | 37 base::Time(), |
| 38 "<html><body>Google Blah Blah</body></html>", | 38 "<html><body>Google Blah Blah</body></html>", |
| 39 TEST_MODE_NORMAL, | 39 TEST_MODE_NORMAL, |
| 40 NULL, | 40 NULL, |
| 41 0 | 41 0, |
| 42 net::OK |
| 42 }; | 43 }; |
| 43 | 44 |
| 44 const MockTransaction kSimplePOST_Transaction = { | 45 const MockTransaction kSimplePOST_Transaction = { |
| 45 "http://bugdatabase.com/edit", | 46 "http://bugdatabase.com/edit", |
| 46 "POST", | 47 "POST", |
| 47 base::Time(), | 48 base::Time(), |
| 48 "", | 49 "", |
| 49 net::LOAD_NORMAL, | 50 net::LOAD_NORMAL, |
| 50 "HTTP/1.1 200 OK", | 51 "HTTP/1.1 200 OK", |
| 51 "", | 52 "", |
| 52 base::Time(), | 53 base::Time(), |
| 53 "<html><body>Google Blah Blah</body></html>", | 54 "<html><body>Google Blah Blah</body></html>", |
| 54 TEST_MODE_NORMAL, | 55 TEST_MODE_NORMAL, |
| 55 NULL, | 56 NULL, |
| 56 0 | 57 0, |
| 58 net::OK |
| 57 }; | 59 }; |
| 58 | 60 |
| 59 const MockTransaction kTypicalGET_Transaction = { | 61 const MockTransaction kTypicalGET_Transaction = { |
| 60 "http://www.example.com/~foo/bar.html", | 62 "http://www.example.com/~foo/bar.html", |
| 61 "GET", | 63 "GET", |
| 62 base::Time(), | 64 base::Time(), |
| 63 "", | 65 "", |
| 64 net::LOAD_NORMAL, | 66 net::LOAD_NORMAL, |
| 65 "HTTP/1.1 200 OK", | 67 "HTTP/1.1 200 OK", |
| 66 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | 68 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 67 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", | 69 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", |
| 68 base::Time(), | 70 base::Time(), |
| 69 "<html><body>Google Blah Blah</body></html>", | 71 "<html><body>Google Blah Blah</body></html>", |
| 70 TEST_MODE_NORMAL, | 72 TEST_MODE_NORMAL, |
| 71 NULL, | 73 NULL, |
| 72 0 | 74 0, |
| 75 net::OK |
| 73 }; | 76 }; |
| 74 | 77 |
| 75 const MockTransaction kETagGET_Transaction = { | 78 const MockTransaction kETagGET_Transaction = { |
| 76 "http://www.google.com/foopy", | 79 "http://www.google.com/foopy", |
| 77 "GET", | 80 "GET", |
| 78 base::Time(), | 81 base::Time(), |
| 79 "", | 82 "", |
| 80 net::LOAD_NORMAL, | 83 net::LOAD_NORMAL, |
| 81 "HTTP/1.1 200 OK", | 84 "HTTP/1.1 200 OK", |
| 82 "Cache-Control: max-age=10000\n" | 85 "Cache-Control: max-age=10000\n" |
| 83 "Etag: \"foopy\"\n", | 86 "Etag: \"foopy\"\n", |
| 84 base::Time(), | 87 base::Time(), |
| 85 "<html><body>Google Blah Blah</body></html>", | 88 "<html><body>Google Blah Blah</body></html>", |
| 86 TEST_MODE_NORMAL, | 89 TEST_MODE_NORMAL, |
| 87 NULL, | 90 NULL, |
| 88 0 | 91 0, |
| 92 net::OK |
| 89 }; | 93 }; |
| 90 | 94 |
| 91 const MockTransaction kRangeGET_Transaction = { | 95 const MockTransaction kRangeGET_Transaction = { |
| 92 "http://www.google.com/", | 96 "http://www.google.com/", |
| 93 "GET", | 97 "GET", |
| 94 base::Time(), | 98 base::Time(), |
| 95 "Range: 0-100\r\n", | 99 "Range: 0-100\r\n", |
| 96 net::LOAD_NORMAL, | 100 net::LOAD_NORMAL, |
| 97 "HTTP/1.1 200 OK", | 101 "HTTP/1.1 200 OK", |
| 98 "Cache-Control: max-age=10000\n", | 102 "Cache-Control: max-age=10000\n", |
| 99 base::Time(), | 103 base::Time(), |
| 100 "<html><body>Google Blah Blah</body></html>", | 104 "<html><body>Google Blah Blah</body></html>", |
| 101 TEST_MODE_NORMAL, | 105 TEST_MODE_NORMAL, |
| 102 NULL, | 106 NULL, |
| 103 0 | 107 0, |
| 108 net::OK |
| 104 }; | 109 }; |
| 105 | 110 |
| 106 static const MockTransaction* const kBuiltinMockTransactions[] = { | 111 static const MockTransaction* const kBuiltinMockTransactions[] = { |
| 107 &kSimpleGET_Transaction, | 112 &kSimpleGET_Transaction, |
| 108 &kSimplePOST_Transaction, | 113 &kSimplePOST_Transaction, |
| 109 &kTypicalGET_Transaction, | 114 &kTypicalGET_Transaction, |
| 110 &kETagGET_Transaction, | 115 &kETagGET_Transaction, |
| 111 &kRangeGET_Transaction | 116 &kRangeGET_Transaction |
| 112 }; | 117 }; |
| 113 | 118 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 228 |
| 224 MockNetworkTransaction::~MockNetworkTransaction() {} | 229 MockNetworkTransaction::~MockNetworkTransaction() {} |
| 225 | 230 |
| 226 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, | 231 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, |
| 227 const net::CompletionCallback& callback, | 232 const net::CompletionCallback& callback, |
| 228 const net::BoundNetLog& net_log) { | 233 const net::BoundNetLog& net_log) { |
| 229 const MockTransaction* t = FindMockTransaction(request->url); | 234 const MockTransaction* t = FindMockTransaction(request->url); |
| 230 if (!t) | 235 if (!t) |
| 231 return net::ERR_FAILED; | 236 return net::ERR_FAILED; |
| 232 | 237 |
| 238 // Return immediately if we're returning in error. |
| 239 if (net::OK != t->return_code) { |
| 240 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
| 241 return t->return_code; |
| 242 CallbackLater(callback, t->return_code); |
| 243 return net::ERR_IO_PENDING; |
| 244 } |
| 245 |
| 233 std::string resp_status = t->status; | 246 std::string resp_status = t->status; |
| 234 std::string resp_headers = t->response_headers; | 247 std::string resp_headers = t->response_headers; |
| 235 std::string resp_data = t->data; | 248 std::string resp_data = t->data; |
| 236 if (t->handler) | 249 if (t->handler) |
| 237 (t->handler)(request, &resp_status, &resp_headers, &resp_data); | 250 (t->handler)(request, &resp_status, &resp_headers, &resp_data); |
| 238 | 251 |
| 239 std::string header_data = base::StringPrintf( | 252 std::string header_data = base::StringPrintf( |
| 240 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); | 253 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); |
| 241 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); | 254 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); |
| 242 | 255 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 | 392 |
| 380 if (rv > 0) | 393 if (rv > 0) |
| 381 content.append(buf->data(), rv); | 394 content.append(buf->data(), rv); |
| 382 else if (rv < 0) | 395 else if (rv < 0) |
| 383 return rv; | 396 return rv; |
| 384 } while (rv > 0); | 397 } while (rv > 0); |
| 385 | 398 |
| 386 result->swap(content); | 399 result->swap(content); |
| 387 return net::OK; | 400 return net::OK; |
| 388 } | 401 } |
| OLD | NEW |