Chromium Code Reviews| Index: net/test/url_request/url_request_mock_http_job.cc |
| diff --git a/net/test/url_request/url_request_mock_http_job.cc b/net/test/url_request/url_request_mock_http_job.cc |
| index c611f36530bab8e49b2ad93397fbfe1ad65fe5a4..c3e3c660ea44e88eb5dc1f71c51aaea802a8305b 100644 |
| --- a/net/test/url_request/url_request_mock_http_job.cc |
| +++ b/net/test/url_request/url_request_mock_http_job.cc |
| @@ -6,23 +6,33 @@ |
| #include "base/files/file_util.h" |
| #include "base/message_loop/message_loop.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/task_runner_util.h" |
| #include "base/threading/sequenced_worker_pool.h" |
| #include "base/threading/thread_restrictions.h" |
| #include "net/base/filename_util.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/url_util.h" |
| #include "net/http/http_response_headers.h" |
| #include "net/url_request/url_request_filter.h" |
| #include "net/url_request/url_request_interceptor.h" |
| +namespace net { |
| + |
| +namespace { |
| + |
| const char kMockHostname[] = "mock.http"; |
| const base::FilePath::CharType kMockHeaderFileSuffix[] = |
| FILE_PATH_LITERAL(".mock-http-headers"); |
| -namespace net { |
| - |
| -namespace { |
| +// String names of failure phases matching FailurePhase enum. |
| +const char* kFailurePhase[]{ |
|
mmenke
2014/10/20 21:07:30
nit: Space before "{"
mef
2014/10/20 22:30:09
Done.
|
| + "start", // START |
| + "readasync", // READ_ASYNC |
| + "readsync", // READ_SYNC |
|
mmenke
2014/10/20 21:07:29
nit: -2 indent.
mef
2014/10/20 22:30:09
Done.
|
| +}; |
| class MockJobInterceptor : public net::URLRequestInterceptor { |
| public: |
| @@ -119,6 +129,18 @@ GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { |
| } |
| // static |
| +GURL URLRequestMockHTTPJob::GetMockUrlWithFailure(const base::FilePath& path, |
| + FailurePhase phase, |
| + int net_error) { |
|
mmenke
2014/10/20 21:07:30
Maybe DCHECK phase is in the correct range?
mef
2014/10/20 22:30:09
Done.
|
| + std::string url(GetMockUrl(path).spec()); |
| + url.append("?"); |
| + url.append(kFailurePhase[phase]); |
| + url.append("="); |
| + url.append(base::IntToString(net_error)); |
| + return GURL(url); |
| +} |
| + |
| +// static |
| scoped_ptr<net::URLRequestInterceptor> URLRequestMockHTTPJob::CreateInterceptor( |
| const base::FilePath& base_path, |
| const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
| @@ -163,23 +185,72 @@ bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
| // Public virtual version. |
| void URLRequestMockHTTPJob::Start() { |
| + if (MaybeReportErrorOnPhase(START)) |
| + return; |
| base::PostTaskAndReplyWithResult( |
| task_runner_.get(), |
| FROM_HERE, |
| base::Bind(&DoFileIO, file_path_), |
| - base::Bind(&URLRequestMockHTTPJob::GetRawHeaders, |
| + base::Bind(&URLRequestMockHTTPJob::SetHeadersAndStart, |
| weak_ptr_factory_.GetWeakPtr())); |
| } |
| -void URLRequestMockHTTPJob::GetRawHeaders(std::string raw_headers) { |
| +// Public virtual version. |
| +bool URLRequestMockHTTPJob::ReadRawData(IOBuffer* buf, |
| + int buf_size, |
| + int* bytes_read) { |
| + if (MaybeReportErrorOnPhase(READ_SYNC)) |
| + return false; |
| + if (MaybeReportErrorOnPhase(READ_ASYNC)) |
| + return false; |
| + return URLRequestFileJob::ReadRawData(buf, buf_size, bytes_read); |
| +} |
| + |
| +void URLRequestMockHTTPJob::SetHeadersAndStart(const std::string& raw_headers) { |
| + if (MaybeReportErrorOnPhase(START)) |
| + return; |
| + raw_headers_ = raw_headers; |
| // Handle CRLF line-endings. |
| - ReplaceSubstringsAfterOffset(&raw_headers, 0, "\r\n", "\n"); |
| + ReplaceSubstringsAfterOffset(&raw_headers_, 0, "\r\n", "\n"); |
| // ParseRawHeaders expects \0 to end each header line. |
| - ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); |
| - raw_headers_ = raw_headers; |
| + ReplaceSubstringsAfterOffset(&raw_headers_, 0, "\n", std::string("\0", 1)); |
| URLRequestFileJob::Start(); |
| } |
| +bool URLRequestMockHTTPJob::MaybeReportErrorOnPhase(FailurePhase phase) { |
|
mmenke
2014/10/20 21:07:30
nit: phase -> current_phase (To try and make it c
mef
2014/10/20 22:30:09
Done.
|
| + DCHECK(phase >= START && phase <= READ_SYNC); |
|
mmenke
2014/10/20 21:07:30
Suggest a DCHECK_GE and DCHECK_LE
mef
2014/10/20 22:30:09
Done.
|
| + std::string phase_key(kFailurePhase[phase]); |
| + |
| + std::string value; |
| + if (!GetValueForKeyInQuery(request_->url(), phase_key, &value)) |
| + return false; |
| + |
| + int net_error; |
| + if (!base::StringToInt(value, &net_error)) |
| + return false; |
| + |
| + if (net_error == net::ERR_IO_PENDING) { |
| + DLOG(ERROR) << "Report Mock ERR_IO_PENDING for phase " << phase_key; |
| + SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| + } else { |
| + DLOG(ERROR) << "Report Mock Error " << net_error << " for phase " |
| + << phase_key; |
| + if (phase == READ_ASYNC) { |
| + SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| + base::MessageLoopProxy::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + &URLRequestMockHTTPJob::NotifyDone, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + net::URLRequestStatus(net::URLRequestStatus::FAILED, net_error))); |
| + } else { |
| + NotifyDone( |
| + net::URLRequestStatus(net::URLRequestStatus::FAILED, net_error)); |
| + } |
| + } |
|
mmenke
2014/10/20 21:07:30
Think this could be a bit simpler as:
if (phase =
mef
2014/10/20 22:30:09
Done.
|
| + return true; |
| +} |
| + |
| // Private const version. |
| void URLRequestMockHTTPJob::GetResponseInfoConst( |
| net::HttpResponseInfo* info) const { |