Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: net/http/http_transaction_test_util.cc

Issue 2886483002: Adds a new class HttpCache::Writers for multiple cache transactions reading from the network. (Closed)
Patch Set: Feedback addressed. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 // Return immediately if we're returning an error.
jkarlin 2017/07/12 15:05:26 This comment isn't quite right, as there is no ear
shivanisha 2017/07/13 18:35:51 removed
310 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len); 340 int num = t->read_return_code;
311 data_cursor_ += num; 341
312 } else { 342 if (OK == num) {
313 int data_len = static_cast<int>(data_.size()); 343 if (read_handler_) {
314 num = std::min(static_cast<int64_t>(buf_len), data_len - data_cursor_); 344 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len);
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; 345 data_cursor_ += num;
346 } else {
347 int data_len = static_cast<int>(data_.size());
348 num = std::min(static_cast<int64_t>(buf_len), data_len - data_cursor_);
349 if (test_mode_ & TEST_MODE_SLOW_READ)
350 num = std::min(num, 1);
351 if (num) {
352 memcpy(buf->data(), data_.data() + data_cursor_, num);
353 data_cursor_ += num;
354 }
320 } 355 }
321 } 356 }
357
322 if (test_mode_ & TEST_MODE_SYNC_NET_READ) 358 if (test_mode_ & TEST_MODE_SYNC_NET_READ)
323 return num; 359 return num;
324 360
325 CallbackLater(callback, num); 361 CallbackLater(callback, num);
326 return ERR_IO_PENDING; 362 return ERR_IO_PENDING;
327 } 363 }
328 364
329 void MockNetworkTransaction::StopCaching() { 365 void MockNetworkTransaction::StopCaching() {
330 if (transaction_factory_.get()) 366 if (transaction_factory_.get())
331 transaction_factory_->TransactionStopCaching(); 367 transaction_factory_->TransactionStopCaching();
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request, 446 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request,
411 const CompletionCallback& callback, 447 const CompletionCallback& callback,
412 const NetLogWithSource& net_log) { 448 const NetLogWithSource& net_log) {
413 const MockTransaction* t = FindMockTransaction(request->url); 449 const MockTransaction* t = FindMockTransaction(request->url);
414 if (!t) 450 if (!t)
415 return ERR_FAILED; 451 return ERR_FAILED;
416 452
417 test_mode_ = t->test_mode; 453 test_mode_ = t->test_mode;
418 454
419 // Return immediately if we're returning an error. 455 // Return immediately if we're returning an error.
420 if (OK != t->return_code) { 456 if (OK != t->start_return_code) {
421 if (test_mode_ & TEST_MODE_SYNC_NET_START) 457 if (test_mode_ & TEST_MODE_SYNC_NET_START)
422 return t->return_code; 458 return t->start_return_code;
423 CallbackLater(callback, t->return_code); 459 CallbackLater(callback, t->start_return_code);
424 return ERR_IO_PENDING; 460 return ERR_IO_PENDING;
425 } 461 }
426 462
427 sent_bytes_ = kTotalSentBytes; 463 sent_bytes_ = kTotalSentBytes;
428 received_bytes_ = kTotalReceivedBytes; 464 received_bytes_ = kTotalReceivedBytes;
429 465
430 std::string resp_status = t->status; 466 std::string resp_status = t->status;
431 std::string resp_headers = t->response_headers; 467 std::string resp_headers = t->response_headers;
432 std::string resp_data = t->data; 468 std::string resp_data = t->data;
433 if (t->handler) 469 if (t->handler)
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 content.append(buf->data(), rv); 623 content.append(buf->data(), rv);
588 else if (rv < 0) 624 else if (rv < 0)
589 return rv; 625 return rv;
590 } while (rv > 0); 626 } while (rv > 0);
591 627
592 result->swap(content); 628 result->swap(content);
593 return OK; 629 return OK;
594 } 630 }
595 631
596 } // namespace net 632 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698