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

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

Issue 2164863002: Fix CronetHttpURLConnectionTest#testServerHangsUp flake (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comment Created 4 years, 5 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/native_test_server.cc
diff --git a/components/cronet/android/test/native_test_server.cc b/components/cronet/android/test/native_test_server.cc
index 60bd3a0070f05ece18867b1c09476d101c118382..34e868bd46794c8f96e342e883efeed9dcc70d20 100644
--- a/components/cronet/android/test/native_test_server.cc
+++ b/components/cronet/android/test/native_test_server.cc
@@ -15,10 +15,12 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/macros.h"
+#include "base/memory/ptr_util.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/test_support_android.h"
+#include "base/threading/thread_task_runner_handle.h"
#include "components/cronet/android/test/cronet_test_util.h"
#include "jni/NativeTestServer_jni.h"
#include "net/base/host_port_pair.h"
@@ -27,6 +29,7 @@
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
+#include "net/test/embedded_test_server/request_handler_util.h"
#include "url/gurl.h"
namespace cronet {
@@ -38,6 +41,7 @@ const char kEchoHeaderPath[] = "/echo_header";
const char kEchoAllHeadersPath[] = "/echo_all_headers";
const char kEchoMethodPath[] = "/echo_method";
const char kRedirectToEchoBodyPath[] = "/redirect_to_echo_body";
+const char kExabyteResponsePath[] = "/exabyte_response";
// Path that advertises the dictionary passed in query params if client
// supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server
// responds with "Get-Dictionary: /sdch/dict/LeQxM80O".
@@ -49,6 +53,33 @@ const char kSdchDictPath[] = "/sdch/dict/";
net::EmbeddedTestServer* g_test_server = nullptr;
+// A HttpResponse that is almost never ending (with an Extabyte content-length).
+class ExabyteResponse : public net::test_server::BasicHttpResponse {
+ public:
+ ExabyteResponse() {}
+
+ void SendResponse(
+ const net::test_server::SendBytesCallback& send,
+ const net::test_server::SendCompleteCallback& done) override {
+ // Use 10^18 bytes (exabyte) as the content length so that the client will
+ // be expecting data.
+ send.Run("HTTP/1.1 200 OK\r\nContent-Length:1000000000000000000\r\n\r\n",
+ base::Bind(&ExabyteResponse::SendExabyte, send));
+ }
+
+ private:
+ // Keeps sending the word "echo" over and over again. It can go further to
+ // limit the response to exactly an exabyte, but it shouldn't be necessary
+ // for the purpose of testing.
+ static void SendExabyte(const net::test_server::SendBytesCallback& send) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(send, "echo",
+ base::Bind(&ExabyteResponse::SendExabyte, send)));
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(ExabyteResponse);
+};
+
std::unique_ptr<net::test_server::RawHttpResponse> ConstructResponseBasedOnFile(
const base::FilePath& file_path) {
std::string file_contents;
@@ -161,6 +192,11 @@ std::unique_ptr<net::test_server::HttpResponse> SdchRequestHandler(
return std::unique_ptr<net::test_server::BasicHttpResponse>();
}
+std::unique_ptr<net::test_server::HttpResponse> HandleExabyteRequest(
+ const net::test_server::HttpRequest& request) {
+ return base::WrapUnique(new ExabyteResponse);
+}
+
} // namespace
jboolean StartNativeTestServer(JNIEnv* env,
@@ -178,6 +214,9 @@ jboolean StartNativeTestServer(JNIEnv* env,
g_test_server = new net::EmbeddedTestServer();
g_test_server->RegisterRequestHandler(
base::Bind(&NativeTestServerRequestHandler));
+ g_test_server->RegisterDefaultHandler(
+ base::Bind(&net::test_server::HandlePrefixedRequest, kExabyteResponsePath,
+ base::Bind(&HandleExabyteRequest)));
g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler));
base::FilePath test_files_root(
base::android::ConvertJavaStringToUTF8(env, jtest_files_root));
@@ -259,6 +298,14 @@ ScopedJavaLocalRef<jstring> GetSdchURL(JNIEnv* env,
return base::android::ConvertUTF8ToJavaString(env, url);
}
+ScopedJavaLocalRef<jstring> GetExabyteResponseURL(
+ JNIEnv* env,
+ const JavaParamRef<jclass>& jcaller) {
+ DCHECK(g_test_server);
+ GURL url = g_test_server->GetURL(kExabyteResponsePath);
+ return base::android::ConvertUTF8ToJavaString(env, url.spec());
+}
+
ScopedJavaLocalRef<jstring> GetHostPort(JNIEnv* env,
const JavaParamRef<jclass>& jcaller) {
DCHECK(g_test_server);

Powered by Google App Engine
This is Rietveld 408576698