Chromium Code Reviews| Index: components/cronet/android/test/cronet_tests_jni.cc |
| diff --git a/components/cronet/android/test/cronet_tests_jni.cc b/components/cronet/android/test/cronet_tests_jni.cc |
| index 88b2b8cf62041c896430ff1f6fec29f811d68fd1..da9fa136a60c293f31ab1c3f5784a79d1386802b 100644 |
| --- a/components/cronet/android/test/cronet_tests_jni.cc |
| +++ b/components/cronet/android/test/cronet_tests_jni.cc |
| @@ -7,14 +7,79 @@ |
| #include "base/android/base_jni_registrar.h" |
| #include "base/android/jni_android.h" |
| #include "base/android/jni_registrar.h" |
| +#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/path_service.h" |
| #include "components/cronet/android/cronet_loader.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 { |
| +// 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/"; |
| + |
| const base::android::RegistrationMethod kCronetTestsRegisteredMethods[] = { |
| {"BaseAndroid", base::android::RegisterJni}, |
| }; |
| +class CronetMockJobInterceptor : public net::URLRequestInterceptor { |
|
mef
2014/09/19 14:12:00
Suggest: move CronetMockJobInterceptor into its ow
xunjieli
2014/09/19 15:00:04
Done.
|
| + 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)) { |
|
mef
2014/09/19 14:12:00
Would it make sense to make some kind of map betwe
xunjieli
2014/09/19 15:00:04
Acknowledged. I think this is a great idea. But ri
|
| + 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); |
| +}; |
| + |
| +bool AddUrlInterceptors() { |
| + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| + return filter->AddUrlInterceptor(GURL(kMockCronetTestSuccessUrl), |
| + scoped_ptr<net::URLRequestInterceptor>( |
| + new CronetMockJobInterceptor())) && |
| + filter->AddUrlInterceptor(GURL(kMockCronetTestRedirectUrl), |
|
mef
2014/09/19 14:12:00
Again, having a map / list and a loop would avoid
xunjieli
2014/09/19 15:00:04
Acknowledged. Will tackle this in the next CL when
|
| + scoped_ptr<net::URLRequestInterceptor>( |
| + new CronetMockJobInterceptor())) && |
| + filter->AddUrlInterceptor(GURL(kMockCronetTestFailedUrl), |
| + scoped_ptr<net::URLRequestInterceptor>( |
| + new CronetMockJobInterceptor())); |
| +} |
| + |
| } // namespace |
| // This is called by the VM when the shared library is first loaded. |
| @@ -35,10 +100,14 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
| arraysize(kCronetTestsRegisteredMethods))) { |
| return -1; |
| } |
| + |
| + if (!AddUrlInterceptors()) |
| + return -1; |
| return cronet_onload; |
| } |
| extern "C" void JNI_OnUnLoad(JavaVM* vm, void* reserved) { |
| + net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| cronet::CronetOnUnLoad(vm, reserved); |
| } |