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 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 } | 235 } |
236 | 236 |
237 MockNetworkTransaction::MockNetworkTransaction(RequestPriority priority, | 237 MockNetworkTransaction::MockNetworkTransaction(RequestPriority priority, |
238 MockNetworkLayer* factory) | 238 MockNetworkLayer* factory) |
239 : request_(NULL), | 239 : request_(NULL), |
240 data_cursor_(0), | 240 data_cursor_(0), |
241 priority_(priority), | 241 priority_(priority), |
242 websocket_handshake_stream_create_helper_(NULL), | 242 websocket_handshake_stream_create_helper_(NULL), |
243 transaction_factory_(factory->AsWeakPtr()), | 243 transaction_factory_(factory->AsWeakPtr()), |
244 received_bytes_(0), | 244 received_bytes_(0), |
| 245 sent_bytes_(0), |
245 socket_log_id_(NetLog::Source::kInvalidId), | 246 socket_log_id_(NetLog::Source::kInvalidId), |
246 weak_factory_(this) { | 247 weak_factory_(this) {} |
247 } | |
248 | 248 |
249 MockNetworkTransaction::~MockNetworkTransaction() {} | 249 MockNetworkTransaction::~MockNetworkTransaction() {} |
250 | 250 |
251 int MockNetworkTransaction::Start(const HttpRequestInfo* request, | 251 int MockNetworkTransaction::Start(const HttpRequestInfo* request, |
252 const CompletionCallback& callback, | 252 const CompletionCallback& callback, |
253 const BoundNetLog& net_log) { | 253 const BoundNetLog& net_log) { |
254 if (request_) | 254 if (request_) |
255 return ERR_FAILED; | 255 return ERR_FAILED; |
256 | 256 |
257 request_ = request; | 257 request_ = request; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 | 322 |
323 bool MockNetworkTransaction::GetFullRequestHeaders( | 323 bool MockNetworkTransaction::GetFullRequestHeaders( |
324 HttpRequestHeaders* headers) const { | 324 HttpRequestHeaders* headers) const { |
325 return false; | 325 return false; |
326 } | 326 } |
327 | 327 |
328 int64 MockNetworkTransaction::GetTotalReceivedBytes() const { | 328 int64 MockNetworkTransaction::GetTotalReceivedBytes() const { |
329 return received_bytes_; | 329 return received_bytes_; |
330 } | 330 } |
331 | 331 |
| 332 int64_t MockNetworkTransaction::GetTotalSentBytes() const { |
| 333 return sent_bytes_; |
| 334 } |
| 335 |
332 void MockNetworkTransaction::DoneReading() { | 336 void MockNetworkTransaction::DoneReading() { |
333 if (transaction_factory_.get()) | 337 if (transaction_factory_.get()) |
334 transaction_factory_->TransactionDoneReading(); | 338 transaction_factory_->TransactionDoneReading(); |
335 } | 339 } |
336 | 340 |
337 const HttpResponseInfo* MockNetworkTransaction::GetResponseInfo() const { | 341 const HttpResponseInfo* MockNetworkTransaction::GetResponseInfo() const { |
338 return &response_; | 342 return &response_; |
339 } | 343 } |
340 | 344 |
341 LoadState MockNetworkTransaction::GetLoadState() const { | 345 LoadState MockNetworkTransaction::GetLoadState() const { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 380 |
377 void MockNetworkTransaction::SetPriority(RequestPriority priority) { | 381 void MockNetworkTransaction::SetPriority(RequestPriority priority) { |
378 priority_ = priority; | 382 priority_ = priority; |
379 } | 383 } |
380 | 384 |
381 void MockNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper( | 385 void MockNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper( |
382 WebSocketHandshakeStreamBase::CreateHelper* create_helper) { | 386 WebSocketHandshakeStreamBase::CreateHelper* create_helper) { |
383 websocket_handshake_stream_create_helper_ = create_helper; | 387 websocket_handshake_stream_create_helper_ = create_helper; |
384 } | 388 } |
385 | 389 |
| 390 // static |
| 391 const int64_t MockNetworkTransaction::kTotalReceivedBytes = 1000; |
| 392 |
| 393 // static |
| 394 const int64_t MockNetworkTransaction::kTotalSentBytes = 100; |
| 395 |
386 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request, | 396 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request, |
387 const CompletionCallback& callback, | 397 const CompletionCallback& callback, |
388 const BoundNetLog& net_log) { | 398 const BoundNetLog& net_log) { |
389 const MockTransaction* t = FindMockTransaction(request->url); | 399 const MockTransaction* t = FindMockTransaction(request->url); |
390 if (!t) | 400 if (!t) |
391 return ERR_FAILED; | 401 return ERR_FAILED; |
392 | 402 |
393 test_mode_ = t->test_mode; | 403 test_mode_ = t->test_mode; |
394 | 404 |
395 // Return immediately if we're returning an error. | 405 // Return immediately if we're returning an error. |
396 if (OK != t->return_code) { | 406 if (OK != t->return_code) { |
397 if (test_mode_ & TEST_MODE_SYNC_NET_START) | 407 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
398 return t->return_code; | 408 return t->return_code; |
399 CallbackLater(callback, t->return_code); | 409 CallbackLater(callback, t->return_code); |
400 return ERR_IO_PENDING; | 410 return ERR_IO_PENDING; |
401 } | 411 } |
402 | 412 |
| 413 sent_bytes_ = kTotalSentBytes; |
| 414 received_bytes_ = kTotalReceivedBytes; |
| 415 |
403 std::string resp_status = t->status; | 416 std::string resp_status = t->status; |
404 std::string resp_headers = t->response_headers; | 417 std::string resp_headers = t->response_headers; |
405 std::string resp_data = t->data; | 418 std::string resp_data = t->data; |
406 received_bytes_ = resp_status.size() + resp_headers.size() + resp_data.size(); | |
407 if (t->handler) | 419 if (t->handler) |
408 (t->handler)(request, &resp_status, &resp_headers, &resp_data); | 420 (t->handler)(request, &resp_status, &resp_headers, &resp_data); |
409 | 421 |
410 std::string header_data = base::StringPrintf( | 422 std::string header_data = base::StringPrintf( |
411 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); | 423 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); |
412 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); | 424 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); |
413 | 425 |
414 response_.request_time = transaction_factory_->Now(); | 426 response_.request_time = transaction_factory_->Now(); |
415 if (!t->request_time.is_null()) | 427 if (!t->request_time.is_null()) |
416 response_.request_time = t->request_time; | 428 response_.request_time = t->request_time; |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 content.append(buf->data(), rv); | 551 content.append(buf->data(), rv); |
540 else if (rv < 0) | 552 else if (rv < 0) |
541 return rv; | 553 return rv; |
542 } while (rv > 0); | 554 } while (rv > 0); |
543 | 555 |
544 result->swap(content); | 556 result->swap(content); |
545 return OK; | 557 return OK; |
546 } | 558 } |
547 | 559 |
548 } // namespace net | 560 } // namespace net |
OLD | NEW |