OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_test_util.h" | 5 #include "net/http/http_transaction_test_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <unordered_map> | 8 #include <unordered_map> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 "HTTP/1.1 200 OK", | 51 "HTTP/1.1 200 OK", |
52 "Cache-Control: max-age=10000\n", | 52 "Cache-Control: max-age=10000\n", |
53 base::Time(), | 53 base::Time(), |
54 "<html><body>Google Blah Blah</body></html>", | 54 "<html><body>Google Blah Blah</body></html>", |
55 TEST_MODE_NORMAL, | 55 TEST_MODE_NORMAL, |
56 nullptr, | 56 nullptr, |
57 nullptr, | 57 nullptr, |
58 nullptr, | 58 nullptr, |
59 0, | 59 0, |
60 0, | 60 0, |
| 61 OK, |
61 OK}; | 62 OK}; |
62 | 63 |
63 const MockTransaction kSimplePOST_Transaction = { | 64 const MockTransaction kSimplePOST_Transaction = { |
64 "http://bugdatabase.com/edit", | 65 "http://bugdatabase.com/edit", |
65 "POST", | 66 "POST", |
66 base::Time(), | 67 base::Time(), |
67 "", | 68 "", |
68 LOAD_NORMAL, | 69 LOAD_NORMAL, |
69 "HTTP/1.1 200 OK", | 70 "HTTP/1.1 200 OK", |
70 "", | 71 "", |
71 base::Time(), | 72 base::Time(), |
72 "<html><body>Google Blah Blah</body></html>", | 73 "<html><body>Google Blah Blah</body></html>", |
73 TEST_MODE_NORMAL, | 74 TEST_MODE_NORMAL, |
74 nullptr, | 75 nullptr, |
75 nullptr, | 76 nullptr, |
76 nullptr, | 77 nullptr, |
77 0, | 78 0, |
78 0, | 79 0, |
| 80 OK, |
79 OK}; | 81 OK}; |
80 | 82 |
81 const MockTransaction kTypicalGET_Transaction = { | 83 const MockTransaction kTypicalGET_Transaction = { |
82 "http://www.example.com/~foo/bar.html", "GET", base::Time(), "", | 84 "http://www.example.com/~foo/bar.html", |
83 LOAD_NORMAL, "HTTP/1.1 200 OK", | 85 "GET", |
| 86 base::Time(), |
| 87 "", |
| 88 LOAD_NORMAL, |
| 89 "HTTP/1.1 200 OK", |
84 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | 90 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
85 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", | 91 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", |
86 base::Time(), "<html><body>Google Blah Blah</body></html>", | 92 base::Time(), |
87 TEST_MODE_NORMAL, nullptr, nullptr, nullptr, 0, 0, OK}; | 93 "<html><body>Google Blah Blah</body></html>", |
| 94 TEST_MODE_NORMAL, |
| 95 nullptr, |
| 96 nullptr, |
| 97 nullptr, |
| 98 0, |
| 99 0, |
| 100 OK, |
| 101 OK}; |
88 | 102 |
89 const MockTransaction kETagGET_Transaction = { | 103 const MockTransaction kETagGET_Transaction = { |
90 "http://www.google.com/foopy", "GET", base::Time(), "", LOAD_NORMAL, | 104 "http://www.google.com/foopy", |
| 105 "GET", |
| 106 base::Time(), |
| 107 "", |
| 108 LOAD_NORMAL, |
91 "HTTP/1.1 200 OK", | 109 "HTTP/1.1 200 OK", |
92 "Cache-Control: max-age=10000\n" | 110 "Cache-Control: max-age=10000\n" |
93 "Etag: \"foopy\"\n", | 111 "Etag: \"foopy\"\n", |
94 base::Time(), "<html><body>Google Blah Blah</body></html>", | 112 base::Time(), |
95 TEST_MODE_NORMAL, nullptr, nullptr, nullptr, 0, 0, OK}; | 113 "<html><body>Google Blah Blah</body></html>", |
| 114 TEST_MODE_NORMAL, |
| 115 nullptr, |
| 116 nullptr, |
| 117 nullptr, |
| 118 0, |
| 119 0, |
| 120 OK, |
| 121 OK}; |
96 | 122 |
97 const MockTransaction kRangeGET_Transaction = { | 123 const MockTransaction kRangeGET_Transaction = { |
98 "http://www.google.com/", | 124 "http://www.google.com/", |
99 "GET", | 125 "GET", |
100 base::Time(), | 126 base::Time(), |
101 "Range: 0-100\r\n", | 127 "Range: 0-100\r\n", |
102 LOAD_NORMAL, | 128 LOAD_NORMAL, |
103 "HTTP/1.1 200 OK", | 129 "HTTP/1.1 200 OK", |
104 "Cache-Control: max-age=10000\n", | 130 "Cache-Control: max-age=10000\n", |
105 base::Time(), | 131 base::Time(), |
106 "<html><body>Google Blah Blah</body></html>", | 132 "<html><body>Google Blah Blah</body></html>", |
107 TEST_MODE_NORMAL, | 133 TEST_MODE_NORMAL, |
108 nullptr, | 134 nullptr, |
109 nullptr, | 135 nullptr, |
110 nullptr, | 136 nullptr, |
111 0, | 137 0, |
112 0, | 138 0, |
| 139 OK, |
113 OK}; | 140 OK}; |
114 | 141 |
115 static const MockTransaction* const kBuiltinMockTransactions[] = { | 142 static const MockTransaction* const kBuiltinMockTransactions[] = { |
116 &kSimpleGET_Transaction, | 143 &kSimpleGET_Transaction, |
117 &kSimplePOST_Transaction, | 144 &kSimplePOST_Transaction, |
118 &kTypicalGET_Transaction, | 145 &kTypicalGET_Transaction, |
119 &kETagGET_Transaction, | 146 &kETagGET_Transaction, |
120 &kRangeGET_Transaction | 147 &kRangeGET_Transaction |
121 }; | 148 }; |
122 | 149 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 | 324 |
298 // Allow the mock server to decide whether authentication is required or not. | 325 // Allow the mock server to decide whether authentication is required or not. |
299 std::string status_line = response_.headers->GetStatusLine(); | 326 std::string status_line = response_.headers->GetStatusLine(); |
300 return status_line.find(" 401 ") != std::string::npos || | 327 return status_line.find(" 401 ") != std::string::npos || |
301 status_line.find(" 407 ") != std::string::npos; | 328 status_line.find(" 407 ") != std::string::npos; |
302 } | 329 } |
303 | 330 |
304 int MockNetworkTransaction::Read(net::IOBuffer* buf, | 331 int MockNetworkTransaction::Read(net::IOBuffer* buf, |
305 int buf_len, | 332 int buf_len, |
306 const CompletionCallback& callback) { | 333 const CompletionCallback& callback) { |
| 334 const MockTransaction* t = FindMockTransaction(request_->url); |
| 335 DCHECK(t); |
| 336 |
307 CHECK(!done_reading_called_); | 337 CHECK(!done_reading_called_); |
308 int num = 0; | 338 |
309 if (read_handler_) { | 339 int num = t->read_return_code; |
310 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len); | 340 |
311 data_cursor_ += num; | 341 if (OK == num) { |
312 } else { | 342 if (read_handler_) { |
313 int data_len = static_cast<int>(data_.size()); | 343 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len); |
314 num = std::min(static_cast<int64_t>(buf_len), data_len - data_cursor_); | |
315 if (test_mode_ & TEST_MODE_SLOW_READ) | |
316 num = std::min(num, 1); | |
317 if (num) { | |
318 memcpy(buf->data(), data_.data() + data_cursor_, num); | |
319 data_cursor_ += num; | 344 data_cursor_ += num; |
| 345 } else { |
| 346 int data_len = static_cast<int>(data_.size()); |
| 347 num = std::min(static_cast<int64_t>(buf_len), data_len - data_cursor_); |
| 348 if (test_mode_ & TEST_MODE_SLOW_READ) |
| 349 num = std::min(num, 1); |
| 350 if (num) { |
| 351 memcpy(buf->data(), data_.data() + data_cursor_, num); |
| 352 data_cursor_ += num; |
| 353 } |
320 } | 354 } |
321 } | 355 } |
| 356 |
322 if (test_mode_ & TEST_MODE_SYNC_NET_READ) | 357 if (test_mode_ & TEST_MODE_SYNC_NET_READ) |
323 return num; | 358 return num; |
324 | 359 |
325 CallbackLater(callback, num); | 360 CallbackLater(callback, num); |
326 return ERR_IO_PENDING; | 361 return ERR_IO_PENDING; |
327 } | 362 } |
328 | 363 |
329 void MockNetworkTransaction::StopCaching() { | 364 void MockNetworkTransaction::StopCaching() { |
330 if (transaction_factory_.get()) | 365 if (transaction_factory_.get()) |
331 transaction_factory_->TransactionStopCaching(); | 366 transaction_factory_->TransactionStopCaching(); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request, | 445 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request, |
411 const CompletionCallback& callback, | 446 const CompletionCallback& callback, |
412 const NetLogWithSource& net_log) { | 447 const NetLogWithSource& net_log) { |
413 const MockTransaction* t = FindMockTransaction(request->url); | 448 const MockTransaction* t = FindMockTransaction(request->url); |
414 if (!t) | 449 if (!t) |
415 return ERR_FAILED; | 450 return ERR_FAILED; |
416 | 451 |
417 test_mode_ = t->test_mode; | 452 test_mode_ = t->test_mode; |
418 | 453 |
419 // Return immediately if we're returning an error. | 454 // Return immediately if we're returning an error. |
420 if (OK != t->return_code) { | 455 if (OK != t->start_return_code) { |
421 if (test_mode_ & TEST_MODE_SYNC_NET_START) | 456 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
422 return t->return_code; | 457 return t->start_return_code; |
423 CallbackLater(callback, t->return_code); | 458 CallbackLater(callback, t->start_return_code); |
424 return ERR_IO_PENDING; | 459 return ERR_IO_PENDING; |
425 } | 460 } |
426 | 461 |
427 sent_bytes_ = kTotalSentBytes; | 462 sent_bytes_ = kTotalSentBytes; |
428 received_bytes_ = kTotalReceivedBytes; | 463 received_bytes_ = kTotalReceivedBytes; |
429 | 464 |
430 std::string resp_status = t->status; | 465 std::string resp_status = t->status; |
431 std::string resp_headers = t->response_headers; | 466 std::string resp_headers = t->response_headers; |
432 std::string resp_data = t->data; | 467 std::string resp_data = t->data; |
433 if (t->handler) | 468 if (t->handler) |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 content.append(buf->data(), rv); | 622 content.append(buf->data(), rv); |
588 else if (rv < 0) | 623 else if (rv < 0) |
589 return rv; | 624 return rv; |
590 } while (rv > 0); | 625 } while (rv > 0); |
591 | 626 |
592 result->swap(content); | 627 result->swap(content); |
593 return OK; | 628 return OK; |
594 } | 629 } |
595 | 630 |
596 } // namespace net | 631 } // namespace net |
OLD | NEW |