Commit 33438ab4 authored by Eugene But's avatar Eugene But Committed by Commit Bot

Add testing::HandleDownload embedded test server handler.

This handler allows to control the size of the download response, and
test larger downloads. The handler was adopted in inttests and egtests.

Bug: 854232
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I6569cc43081d03ffb065e64c5f9d80da833f6541
Reviewed-on: https://chromium-review.googlesource.com/1110599
Commit-Queue: Eugene But <eugenebut@chromium.org>
Reviewed-by: default avatarDanyao Wang <danyao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#573506}
parent 88c5836a
......@@ -111,12 +111,14 @@ source_set("eg_tests") {
"//base",
"//ios/chrome/app:app_internal",
"//ios/chrome/app/strings",
"//ios/chrome/browser:browser",
"//ios/chrome/browser/download",
"//ios/chrome/browser/download:test_support",
"//ios/chrome/browser/ui:ui_internal",
"//ios/chrome/browser/ui:ui_util",
"//ios/chrome/test/app:test_support",
"//ios/chrome/test/earl_grey:test_support",
"//ios/testing:embedded_test_server_support",
"//ios/testing:ios_test_support",
"//ios/third_party/earl_grey:earl_grey+link",
"//ios/web/public",
......
......@@ -10,10 +10,12 @@
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h"
#include "ios/testing/embedded_test_server_handlers.h"
#import "ios/testing/wait_util.h"
#include "ios/web/public/features.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"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -33,20 +35,12 @@ id<GREYMatcher> OpenInButton() {
return ButtonWithAccessibilityLabelId(IDS_IOS_OPEN_IN);
}
// Provides downloads landing page and download response.
// Provides downloads landing page with download link.
std::unique_ptr<net::test_server::HttpResponse> GetResponse(
const net::test_server::HttpRequest& request) {
auto result = std::make_unique<net::test_server::BasicHttpResponse>();
result->set_code(net::HTTP_OK);
if (request.GetURL().path() == "/") {
// Landing page with download links.
result->set_content("<a id='download' href='/download'>Download</a>");
} else if (request.GetURL().path() == "/download") {
// Sucessfully provide download response.
result->AddCustomHeader("Content-Type", "application/vnd.test");
}
result->set_content("<a id='download' href='/download?50000'>Download</a>");
return result;
}
......@@ -65,7 +59,14 @@ std::unique_ptr<net::test_server::HttpResponse> GetResponse(
_featureList.InitAndEnableFeature(web::features::kNewFileDownload);
self.testServer->RegisterRequestHandler(base::BindRepeating(&GetResponse));
self.testServer->RegisterRequestHandler(
base::BindRepeating(&net::test_server::HandlePrefixedRequest, "/",
base::BindRepeating(&GetResponse)));
self.testServer->RegisterRequestHandler(
base::BindRepeating(&net::test_server::HandlePrefixedRequest, "/download",
base::BindRepeating(&testing::HandleDownload)));
GREYAssertTrue(self.testServer->Start(), @"Test server failed to start.");
}
......
......@@ -6,12 +6,21 @@
#include <string>
#include "base/bind.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_task_runner_handle.h"
#include "net/base/escape.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "url/gurl.h"
namespace testing {
const char kTestFormPage[] = "ios.testing.HandleForm";
const char kTestFormFieldValue[] = "test-value";
const char kTestDownloadMimeType[] = "application/vnd.test";
namespace {
// Extracts and escapes url spec from the query.
std::string ExtractUlrSpecFromQuery(
......@@ -23,12 +32,46 @@ std::string ExtractUlrSpecFromQuery(
GURL url(spec);
return url.is_valid() ? net::EscapeForHTML(url.spec()) : spec;
}
} // namespace
namespace testing {
// A HttpResponse that responds with |length| zeroes and kTestDownloadMimeType
// MIME Type.
class DownloadResponse : public net::test_server::BasicHttpResponse {
public:
DownloadResponse(int length) : length_(length) {}
const char kTestFormPage[] = "ios.testing.HandleForm";
const char kTestFormFieldValue[] = "test-value";
void SendResponse(
const net::test_server::SendBytesCallback& send,
const net::test_server::SendCompleteCallback& done) override {
send.Run(base::StringPrintf("HTTP/1.1 200 OK\r\n"
"Content-Type:%s\r\n\r\n"
"Content-Length:%d\r\n\r\n",
kTestDownloadMimeType, length_),
base::BindRepeating(&DownloadResponse::Send, send, done, length_));
}
private:
// Sends "0" |count| times.
static void Send(const net::test_server::SendBytesCallback& send,
const net::test_server::SendCompleteCallback& done,
int count) {
if (!count) {
done.Run();
return;
}
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindRepeating(send, "0",
base::BindRepeating(&DownloadResponse::Send, send,
done, count - 1)));
}
int length_ = 0;
DISALLOW_COPY_AND_ASSIGN(DownloadResponse);
};
} // namespace
std::unique_ptr<net::test_server::HttpResponse> HandleIFrame(
const net::test_server::HttpRequest& request) {
......@@ -68,4 +111,14 @@ std::unique_ptr<net::test_server::HttpResponse> HandleForm(
return std::move(response);
}
std::unique_ptr<net::test_server::HttpResponse> HandleDownload(
const net::test_server::HttpRequest& request) {
int length = 0;
if (!base::StringToInt(request.GetURL().query(), &length)) {
length = 1;
}
return std::make_unique<DownloadResponse>(length);
}
} // namespace testing
......@@ -20,6 +20,7 @@ namespace testing {
extern const char kTestFormPage[];
// Field value for form returned from HandleForm handler.
extern const char kTestFormFieldValue[];
extern const char kTestDownloadMimeType[];
// Returns a page with iframe which uses URL from the query as src.
std::unique_ptr<net::test_server::HttpResponse> HandleIFrame(
......@@ -37,6 +38,12 @@ std::unique_ptr<net::test_server::HttpResponse> HandleEchoQueryOrCloseSocket(
std::unique_ptr<net::test_server::HttpResponse> HandleForm(
const net::test_server::HttpRequest& request);
// Returns a download response with kTestDownloadMimeType MIME type. The length
// of the response is taken from the number passed as URL query (1 byte if the
// length is not provided).
std::unique_ptr<net::test_server::HttpResponse> HandleDownload(
const net::test_server::HttpRequest& request);
} // namespace testing
#endif // IOS_TESTING_EMBEDDED_TEST_SERVER_HANDLERS_H_
......@@ -48,11 +48,8 @@ namespace web {
namespace {
const char kTestPageText[] = "landing!";
const char kExpectedMimeType[] = "text/html";
const char kDownloadMimeType[] = "application/vnd.test";
// NavigationAndLoadCallbacksTest is parameterized on this enum to test both
// LegacyNavigationManagerImpl and WKBasedNavigationManagerImpl.
enum NavigationManagerChoice {
......@@ -518,18 +515,6 @@ class PolicyDeciderMock : public WebStatePolicyDecider {
MOCK_METHOD2(ShouldAllowResponse, bool(NSURLResponse*, bool for_main_frame));
};
// Responds with a download.
std::unique_ptr<net::test_server::HttpResponse> HandleDownloadPage(
const net::test_server::HttpRequest& request) {
if (request.GetURL().path() == "/download") {
auto result = std::make_unique<net::test_server::BasicHttpResponse>();
result->set_content_type(kDownloadMimeType);
result->set_content(kTestPageText);
return std::move(result);
}
return nullptr;
}
} // namespace
using testing::Return;
......@@ -571,8 +556,9 @@ class NavigationAndLoadCallbacksTest
test_server_->RegisterRequestHandler(
base::BindRepeating(&net::test_server::HandlePrefixedRequest, "/form",
base::BindRepeating(&testing::HandleForm)));
test_server_->RegisterDefaultHandler(
base::BindRepeating(&HandleDownloadPage));
test_server_->RegisterRequestHandler(base::BindRepeating(
&net::test_server::HandlePrefixedRequest, "/download",
base::BindRepeating(&testing::HandleDownload)));
RegisterDefaultHandlers(test_server_.get());
test_server_->ServeFilesFromSourceDirectory(
base::FilePath("ios/testing/data/http_server_files/"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment