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

Unified Diff: components/cronet/android/test/cronet_tests_jni.cc

Issue 558333007: Setup initial mock url request job tests for Cronet (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added one assert on request url Created 6 years, 3 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 side-by-side diff with in-line comments
Download patch
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..dbcb0f8f8b3911794ac683a4c9a3b853aa6e55b9 100644
--- a/components/cronet/android/test/cronet_tests_jni.cc
+++ b/components/cronet/android/test/cronet_tests_jni.cc
@@ -7,14 +7,77 @@
#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/files/file_util.h"
+#include "base/files/scoped_file.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/url_request/url_request_filter.h"
+#include "net/test/url_request/url_request_failed_job.h"
+#include "net/test/url_request/url_request_mock_http_job.h"
namespace {
+// A mock URL that will return a 200 response.
+const char* const kMockCronetTestUrl = "http://mock.cronet.success/";
+
+// A mock URL that will result in a failed url request.
+const char* const kMockCronetTestFailedUrl = "http://mock.cronet.failed/";
+
const base::android::RegistrationMethod kCronetTestsRegisteredMethods[] = {
{"BaseAndroid", base::android::RegisterJni},
};
+// static
+net::URLRequestJob* CronetURLRequestJobFactory(
mmenke 2014/09/17 21:46:11 Job factories, though heavily used, are semi-sort-
xunjieli 2014/09/17 22:16:00 Acknowledged. Will do it in next patch.
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate,
+ const std::string& scheme) {
+ base::FilePath test_files_root;
+ DCHECK(PathService::Get(base::DIR_SOURCE_ROOT, &test_files_root));
+
+ if (request->url() == GURL(kMockCronetTestUrl)) {
+ 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);
+}
+
+void PrepareTestFiles() {
+ base::FilePath test_files_root;
+ DCHECK(PathService::Get(base::DIR_SOURCE_ROOT, &test_files_root));
+
+ base::FilePath success_file_path =
+ test_files_root.AppendASCII("success.html");
+ base::ScopedFILE success_file(base::OpenFile(success_file_path, "w"));
+ DCHECK(success_file);
+ fprintf(success_file.get(), "<html></html>");
+ base::FilePath success_headers_file_path =
+ test_files_root.AppendASCII("success.html.mock-http-headers");
+ base::ScopedFILE success_headers_file(
+ base::OpenFile(success_headers_file_path, "w"));
+ DCHECK(success_headers_file);
+ fprintf(success_headers_file.get(), "HTTP/1.1 200 OK");
mmenke 2014/09/17 21:46:11 Is there an alternative way to do this? Embed a d
xunjieli 2014/09/17 22:16:00 I think this dumping mechanism might work. I will
+
+ base::CloseFile(success_file.get());
+ base::CloseFile(success_headers_file.get());
+}
+
+void AddUrlHandlers() {
+ PrepareTestFiles();
+ net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
+ DCHECK(filter->AddUrlHandler(GURL(kMockCronetTestUrl),
+ &CronetURLRequestJobFactory));
+ DCHECK(filter->AddUrlHandler(GURL(kMockCronetTestFailedUrl),
+ &CronetURLRequestJobFactory));
+}
+
} // namespace
// This is called by the VM when the shared library is first loaded.
@@ -35,6 +98,8 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) {
arraysize(kCronetTestsRegisteredMethods))) {
return -1;
}
+
+ AddUrlHandlers();
return cronet_onload;
}

Powered by Google App Engine
This is Rietveld 408576698