Commit 453d85b0 authored by Eugene But's avatar Eugene But Committed by Commit Bot

Add HandleForm handler to use with EmbeddedTestServer.

Use this handler in NavigationAndLoadCallbacksTest. HandleForm will be
used in ErrorPageTest for testing POST errors (the change will be landed
in a separate CL).

Bug: None
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I79bd13ff20be38fa80ca4ebcccbed4532fa93a2d
Reviewed-on: https://chromium-review.googlesource.com/1053064Reviewed-by: default avatarDanyao Wang <danyao@chromium.org>
Commit-Queue: Eugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558832}
parent dd496fe6
...@@ -12,24 +12,31 @@ ...@@ -12,24 +12,31 @@
#include "net/test/embedded_test_server/http_response.h" #include "net/test/embedded_test_server/http_response.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace testing { namespace {
// Extracts and escapes url spec from the query.
std::unique_ptr<net::test_server::HttpResponse> HandleIFrame( std::string ExtractUlrSpecFromQuery(
const net::test_server::HttpRequest& request) { const net::test_server::HttpRequest& request) {
GURL request_url = request.GetURL(); GURL request_url = request.GetURL();
std::string iframe_src = net::UnescapeBinaryURLComponent(request_url.query()); std::string spec = net::UnescapeBinaryURLComponent(request_url.query());
// Escape iframe src. // Escape the URL spec.
GURL iframe_url(iframe_src); GURL url(spec);
if (iframe_url.is_valid()) { return url.is_valid() ? net::EscapeForHTML(url.spec()) : spec;
iframe_src = net::EscapeForHTML(iframe_url.spec()); }
} } // namespace
namespace testing {
const char kTestFormPage[] = "ios.testing.HandleForm";
const char kTestFormFieldValue[] = "test-value";
std::unique_ptr<net::test_server::HttpResponse> HandleIFrame(
const net::test_server::HttpRequest& request) {
auto http_response = std::make_unique<net::test_server::BasicHttpResponse>(); auto http_response = std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_content_type("text/html"); http_response->set_content_type("text/html");
http_response->set_content(base::StringPrintf( http_response->set_content(base::StringPrintf(
"<html><head></head><body><iframe src='%s'></iframe></body></html>", "<html><head></head><body><iframe src='%s'></iframe></body></html>",
iframe_src.c_str())); ExtractUlrSpecFromQuery(request).c_str()));
return std::move(http_response); return std::move(http_response);
} }
...@@ -46,4 +53,19 @@ std::unique_ptr<net::test_server::HttpResponse> HandleEchoQueryOrCloseSocket( ...@@ -46,4 +53,19 @@ std::unique_ptr<net::test_server::HttpResponse> HandleEchoQueryOrCloseSocket(
return std::move(response); return std::move(response);
} }
std::unique_ptr<net::test_server::HttpResponse> HandleForm(
const net::test_server::HttpRequest& request) {
std::string form_action = ExtractUlrSpecFromQuery(request);
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_content_type("text/html");
response->set_content(base::StringPrintf(
"<form method='post' id='form' action='%s'>"
" <input type='text' name='test-name' value='%s'>"
"</form>"
"%s",
form_action.c_str(), kTestFormFieldValue, kTestFormPage));
return std::move(response);
}
} // namespace testing } // namespace testing
...@@ -16,6 +16,11 @@ class HttpResponse; ...@@ -16,6 +16,11 @@ class HttpResponse;
namespace testing { namespace testing {
// Text returned from HandleForm handler.
extern const char kTestFormPage[];
// Field value for form returned from HandleForm handler.
extern const char kTestFormFieldValue[];
// Returns a page with iframe which uses URL from the query as src. // Returns a page with iframe which uses URL from the query as src.
std::unique_ptr<net::test_server::HttpResponse> HandleIFrame( std::unique_ptr<net::test_server::HttpResponse> HandleIFrame(
const net::test_server::HttpRequest& request); const net::test_server::HttpRequest& request);
...@@ -27,6 +32,11 @@ std::unique_ptr<net::test_server::HttpResponse> HandleEchoQueryOrCloseSocket( ...@@ -27,6 +32,11 @@ std::unique_ptr<net::test_server::HttpResponse> HandleEchoQueryOrCloseSocket(
const bool& responds_with_content, const bool& responds_with_content,
const net::test_server::HttpRequest& request); const net::test_server::HttpRequest& request);
// Returns a page with html form and kTestFormPage text. The form contains one
// text field with kTestFormFieldValue value.
std::unique_ptr<net::test_server::HttpResponse> HandleForm(
const net::test_server::HttpRequest& request);
} // namespace testing } // namespace testing
#endif // IOS_TESTING_EMBEDDED_TEST_SERVER_HANDLERS_H_ #endif // IOS_TESTING_EMBEDDED_TEST_SERVER_HANDLERS_H_
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "ios/testing/embedded_test_server_handlers.h"
#import "ios/testing/wait_util.h" #import "ios/testing/wait_util.h"
#import "ios/web/public/navigation_item.h" #import "ios/web/public/navigation_item.h"
#import "ios/web/public/navigation_manager.h" #import "ios/web/public/navigation_manager.h"
...@@ -28,6 +29,7 @@ ...@@ -28,6 +29,7 @@
#include "net/test/embedded_test_server/embedded_test_server.h" #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_request.h"
#include "net/test/embedded_test_server/http_response.h" #include "net/test/embedded_test_server/http_response.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h" #import "testing/gtest_mac.h"
...@@ -43,7 +45,6 @@ namespace web { ...@@ -43,7 +45,6 @@ namespace web {
namespace { namespace {
const char kTestFormValue[] = "inttestvalue";
const char kTestPageText[] = "landing!"; const char kTestPageText[] = "landing!";
const char kExpectedMimeType[] = "text/html"; const char kExpectedMimeType[] = "text/html";
...@@ -473,24 +474,6 @@ class PolicyDeciderMock : public WebStatePolicyDecider { ...@@ -473,24 +474,6 @@ class PolicyDeciderMock : public WebStatePolicyDecider {
MOCK_METHOD2(ShouldAllowResponse, bool(NSURLResponse*, bool for_main_frame)); MOCK_METHOD2(ShouldAllowResponse, bool(NSURLResponse*, bool for_main_frame));
}; };
// Responds with a page that contains an html form.
std::unique_ptr<net::test_server::HttpResponse> HandleFormPage(
const net::test_server::HttpRequest& request) {
if (request.GetURL().path() == "/form") {
auto result = std::make_unique<net::test_server::BasicHttpResponse>();
result->set_content_type("text/html");
result->set_content(base::StringPrintf(
"<form method='post' id='form' action='echo'>"
" <input type=''text' name='inttestname' value='%s'>"
"</form>"
"%s",
kTestFormValue, kTestPageText));
return std::move(result);
}
return nullptr;
}
// Responds with a download. // Responds with a download.
std::unique_ptr<net::test_server::HttpResponse> HandleDownloadPage( std::unique_ptr<net::test_server::HttpResponse> HandleDownloadPage(
const net::test_server::HttpRequest& request) { const net::test_server::HttpRequest& request) {
...@@ -532,7 +515,9 @@ class NavigationAndLoadCallbacksTest : public WebIntTest { ...@@ -532,7 +515,9 @@ class NavigationAndLoadCallbacksTest : public WebIntTest {
web_state_impl->GetWebController().nativeProvider = provider_; web_state_impl->GetWebController().nativeProvider = provider_;
test_server_ = std::make_unique<net::test_server::EmbeddedTestServer>(); test_server_ = std::make_unique<net::test_server::EmbeddedTestServer>();
test_server_->RegisterDefaultHandler(base::BindRepeating(&HandleFormPage)); test_server_->RegisterRequestHandler(
base::BindRepeating(&net::test_server::HandlePrefixedRequest, "/form",
base::BindRepeating(&testing::HandleForm)));
test_server_->RegisterDefaultHandler( test_server_->RegisterDefaultHandler(
base::BindRepeating(&HandleDownloadPage)); base::BindRepeating(&HandleDownloadPage));
RegisterDefaultHandlers(test_server_.get()); RegisterDefaultHandlers(test_server_.get());
...@@ -986,7 +971,7 @@ TEST_F(NavigationAndLoadCallbacksTest, UserInitiatedPostNavigation) { ...@@ -986,7 +971,7 @@ TEST_F(NavigationAndLoadCallbacksTest, UserInitiatedPostNavigation) {
// Tests successful navigation to a new page with post HTTP method. // Tests successful navigation to a new page with post HTTP method.
TEST_F(NavigationAndLoadCallbacksTest, RendererInitiatedPostNavigation) { TEST_F(NavigationAndLoadCallbacksTest, RendererInitiatedPostNavigation) {
const GURL url = test_server_->GetURL("/form"); const GURL url = test_server_->GetURL("/form?echo");
const GURL action = test_server_->GetURL("/echo"); const GURL action = test_server_->GetURL("/echo");
// Perform new page navigation. // Perform new page navigation.
...@@ -1001,7 +986,8 @@ TEST_F(NavigationAndLoadCallbacksTest, RendererInitiatedPostNavigation) { ...@@ -1001,7 +986,8 @@ TEST_F(NavigationAndLoadCallbacksTest, RendererInitiatedPostNavigation) {
EXPECT_CALL(observer_, EXPECT_CALL(observer_,
PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS)); PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS));
ASSERT_TRUE(LoadUrl(url)); ASSERT_TRUE(LoadUrl(url));
ASSERT_TRUE(WaitForWebViewContainingText(web_state(), kTestPageText)); ASSERT_TRUE(
WaitForWebViewContainingText(web_state(), testing::kTestFormPage));
// Submit the form using JavaScript. // Submit the form using JavaScript.
NavigationContext* context = nullptr; NavigationContext* context = nullptr;
...@@ -1025,12 +1011,13 @@ TEST_F(NavigationAndLoadCallbacksTest, RendererInitiatedPostNavigation) { ...@@ -1025,12 +1011,13 @@ TEST_F(NavigationAndLoadCallbacksTest, RendererInitiatedPostNavigation) {
EXPECT_CALL(observer_, EXPECT_CALL(observer_,
PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS)); PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS));
ExecuteJavaScript(@"document.getElementById('form').submit();"); ExecuteJavaScript(@"document.getElementById('form').submit();");
ASSERT_TRUE(WaitForWebViewContainingText(web_state(), kTestFormValue)); ASSERT_TRUE(
WaitForWebViewContainingText(web_state(), testing::kTestFormFieldValue));
} }
// Tests successful reload of a page returned for post request. // Tests successful reload of a page returned for post request.
TEST_F(NavigationAndLoadCallbacksTest, ReloadPostNavigation) { TEST_F(NavigationAndLoadCallbacksTest, ReloadPostNavigation) {
const GURL url = test_server_->GetURL("/form"); const GURL url = test_server_->GetURL("/form?echo");
const GURL action = test_server_->GetURL("/echo"); const GURL action = test_server_->GetURL("/echo");
// Perform new page navigation. // Perform new page navigation.
...@@ -1045,7 +1032,8 @@ TEST_F(NavigationAndLoadCallbacksTest, ReloadPostNavigation) { ...@@ -1045,7 +1032,8 @@ TEST_F(NavigationAndLoadCallbacksTest, ReloadPostNavigation) {
EXPECT_CALL(observer_, EXPECT_CALL(observer_,
PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS)); PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS));
ASSERT_TRUE(LoadUrl(url)); ASSERT_TRUE(LoadUrl(url));
ASSERT_TRUE(WaitForWebViewContainingText(web_state(), kTestPageText)); ASSERT_TRUE(
WaitForWebViewContainingText(web_state(), testing::kTestFormPage));
// Submit the form using JavaScript. // Submit the form using JavaScript.
EXPECT_CALL(*decider_, ShouldAllowRequest(_, _, /*from_main_frame=*/true)) EXPECT_CALL(*decider_, ShouldAllowRequest(_, _, /*from_main_frame=*/true))
...@@ -1062,7 +1050,8 @@ TEST_F(NavigationAndLoadCallbacksTest, ReloadPostNavigation) { ...@@ -1062,7 +1050,8 @@ TEST_F(NavigationAndLoadCallbacksTest, ReloadPostNavigation) {
EXPECT_CALL(observer_, EXPECT_CALL(observer_,
PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS)); PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS));
ExecuteJavaScript(@"window.document.getElementById('form').submit();"); ExecuteJavaScript(@"window.document.getElementById('form').submit();");
ASSERT_TRUE(WaitForWebViewContainingText(web_state(), kTestFormValue)); ASSERT_TRUE(
WaitForWebViewContainingText(web_state(), testing::kTestFormFieldValue));
// Reload the page. // Reload the page.
NavigationContext* context = nullptr; NavigationContext* context = nullptr;
...@@ -1108,7 +1097,7 @@ TEST_F(NavigationAndLoadCallbacksTest, ReloadPostNavigation) { ...@@ -1108,7 +1097,7 @@ TEST_F(NavigationAndLoadCallbacksTest, ReloadPostNavigation) {
// Tests going forward to a page rendered from post response. // Tests going forward to a page rendered from post response.
TEST_F(NavigationAndLoadCallbacksTest, ForwardPostNavigation) { TEST_F(NavigationAndLoadCallbacksTest, ForwardPostNavigation) {
const GURL url = test_server_->GetURL("/form"); const GURL url = test_server_->GetURL("/form?echo");
const GURL action = test_server_->GetURL("/echo"); const GURL action = test_server_->GetURL("/echo");
// Perform new page navigation. // Perform new page navigation.
...@@ -1123,7 +1112,8 @@ TEST_F(NavigationAndLoadCallbacksTest, ForwardPostNavigation) { ...@@ -1123,7 +1112,8 @@ TEST_F(NavigationAndLoadCallbacksTest, ForwardPostNavigation) {
EXPECT_CALL(observer_, EXPECT_CALL(observer_,
PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS)); PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS));
ASSERT_TRUE(LoadUrl(url)); ASSERT_TRUE(LoadUrl(url));
ASSERT_TRUE(WaitForWebViewContainingText(web_state(), kTestPageText)); ASSERT_TRUE(
WaitForWebViewContainingText(web_state(), testing::kTestFormPage));
// Submit the form using JavaScript. // Submit the form using JavaScript.
EXPECT_CALL(*decider_, ShouldAllowRequest(_, _, /*from_main_frame=*/true)) EXPECT_CALL(*decider_, ShouldAllowRequest(_, _, /*from_main_frame=*/true))
...@@ -1140,7 +1130,8 @@ TEST_F(NavigationAndLoadCallbacksTest, ForwardPostNavigation) { ...@@ -1140,7 +1130,8 @@ TEST_F(NavigationAndLoadCallbacksTest, ForwardPostNavigation) {
EXPECT_CALL(observer_, EXPECT_CALL(observer_,
PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS)); PageLoaded(web_state(), PageLoadCompletionStatus::SUCCESS));
ExecuteJavaScript(@"window.document.getElementById('form').submit();"); ExecuteJavaScript(@"window.document.getElementById('form').submit();");
ASSERT_TRUE(WaitForWebViewContainingText(web_state(), kTestFormValue)); ASSERT_TRUE(
WaitForWebViewContainingText(web_state(), testing::kTestFormFieldValue));
// Go Back. // Go Back.
if (web::GetWebClient()->IsSlimNavigationManagerEnabled()) { if (web::GetWebClient()->IsSlimNavigationManagerEnabled()) {
......
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