Chromium Code Reviews| Index: components/cronet/android/test/cronet_mock_job_interceptor.cc |
| diff --git a/components/cronet/android/test/cronet_mock_job_interceptor.cc b/components/cronet/android/test/cronet_mock_job_interceptor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fac8bf2486a18001403fe5b3b3b1b77979d8aefd |
| --- /dev/null |
| +++ b/components/cronet/android/test/cronet_mock_job_interceptor.cc |
| @@ -0,0 +1,80 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
|
mmenke
2014/09/19 17:15:55
nit: Don't use (c) in new files.
xunjieli
2014/09/19 20:39:15
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "cronet_mock_job_interceptor.h" |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/macros.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/path_service.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/test/url_request/url_request_failed_job.h" |
| +#include "net/test/url_request/url_request_mock_http_job.h" |
| +#include "net/url_request/url_request_filter.h" |
| +#include "net/url_request/url_request_interceptor.h" |
| + |
| +namespace cronet { |
| + |
| +namespace { |
| + |
| +// A mock URL that will produces a redirect. |
| +const char kMockCronetTestRedirectUrl[] = "http://mock.cronet.redirect/"; |
| + |
| +// A mock URL that will return a 404 response. |
| +const char kMockCronetTestSuccessUrl[] = "http://mock.cronet.success/"; |
| + |
| +// A mock URL that will result in a failed url request. |
| +const char kMockCronetTestFailedUrl[] = "http://mock.cronet.failed/"; |
| + |
| +class CronetMockJobInterceptor : public net::URLRequestInterceptor { |
| + public: |
| + CronetMockJobInterceptor() {} |
| + virtual ~CronetMockJobInterceptor() {} |
| + |
| + // net::URLRequestInterceptor implementation |
| + virtual net::URLRequestJob* MaybeInterceptRequest( |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) const OVERRIDE { |
| + base::FilePath test_files_root; |
| + PathService::Get(base::DIR_ANDROID_APP_DATA, &test_files_root); |
| + if (request->url() == GURL(kMockCronetTestRedirectUrl)) { |
| + return new net::URLRequestMockHTTPJob( |
| + request, |
| + network_delegate, |
| + test_files_root.Append("redirect.html"), |
| + base::MessageLoop::current()->message_loop_proxy()); |
| + } |
| + if (request->url() == GURL(kMockCronetTestSuccessUrl)) { |
| + return new net::URLRequestMockHTTPJob( |
| + request, |
| + network_delegate, |
| + test_files_root.Append("success.html"), |
| + base::MessageLoop::current()->message_loop_proxy()); |
| + } |
| + return new net::URLRequestFailedJob( |
| + request, network_delegate, net::ERR_FAILED); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(CronetMockJobInterceptor); |
| +}; |
| + |
| +} // namespace |
| + |
| +// static |
| +bool AddUrlInterceptors() { |
| + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| + // TODO(xunjieli): use a map to avoid code duplication. |
| + return filter->AddUrlInterceptor(GURL(kMockCronetTestSuccessUrl), |
| + scoped_ptr<net::URLRequestInterceptor>( |
| + new CronetMockJobInterceptor())) && |
| + filter->AddUrlInterceptor(GURL(kMockCronetTestRedirectUrl), |
| + scoped_ptr<net::URLRequestInterceptor>( |
| + new CronetMockJobInterceptor())) && |
|
mmenke
2014/09/19 17:15:55
As Misha said, we really shouldn't be listing thes
xunjieli
2014/09/19 20:39:15
Done. I see! That's very smart! But I am having on
|
| + filter->AddUrlInterceptor(GURL(kMockCronetTestFailedUrl), |
| + scoped_ptr<net::URLRequestInterceptor>( |
| + new CronetMockJobInterceptor())); |
| +} |
| + |
| +} // namespace cronet |