| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/guest_view/web_view/web_view_apitest.h" | 5 #include "extensions/browser/guest_view/web_view/web_view_apitest.h" |
| 6 | 6 |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "content/public/browser/render_process_host.h" | 10 #include "content/public/browser/render_process_host.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include "net/base/filename_util.h" | 27 #include "net/base/filename_util.h" |
| 28 #include "net/test/embedded_test_server/embedded_test_server.h" | 28 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 29 #include "net/test/embedded_test_server/http_request.h" | 29 #include "net/test/embedded_test_server/http_request.h" |
| 30 #include "net/test/embedded_test_server/http_response.h" | 30 #include "net/test/embedded_test_server/http_response.h" |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 const char kEmptyResponsePath[] = "/close-socket"; | 34 const char kEmptyResponsePath[] = "/close-socket"; |
| 35 const char kRedirectResponsePath[] = "/server-redirect"; | 35 const char kRedirectResponsePath[] = "/server-redirect"; |
| 36 const char kRedirectResponseFullPath[] = "/guest_redirect.html"; | 36 const char kRedirectResponseFullPath[] = "/guest_redirect.html"; |
| 37 const char kUserAgentRedirectResponsePath[] = "/detect-user-agent"; |
| 37 const char kTestDataDirectory[] = "testDataDirectory"; | 38 const char kTestDataDirectory[] = "testDataDirectory"; |
| 38 const char kTestServerPort[] = "testServer.port"; | 39 const char kTestServerPort[] = "testServer.port"; |
| 39 const char kTestWebSocketPort[] = "testWebSocketPort"; | 40 const char kTestWebSocketPort[] = "testWebSocketPort"; |
| 40 | 41 |
| 41 class EmptyHttpResponse : public net::test_server::HttpResponse { | 42 class EmptyHttpResponse : public net::test_server::HttpResponse { |
| 42 public: | 43 public: |
| 43 virtual std::string ToResponseString() const override { | 44 virtual std::string ToResponseString() const override { |
| 44 return std::string(); | 45 return std::string(); |
| 45 } | 46 } |
| 46 }; | 47 }; |
| 47 | 48 |
| 49 // Handles |request| by serving a redirect response if the |User-Agent| is |
| 50 // foobar. |
| 51 static scoped_ptr<net::test_server::HttpResponse> UserAgentResponseHandler( |
| 52 const std::string& path, |
| 53 const GURL& redirect_target, |
| 54 const net::test_server::HttpRequest& request) { |
| 55 if (!StartsWithASCII(path, request.relative_url, true)) |
| 56 return scoped_ptr<net::test_server::HttpResponse>(); |
| 57 |
| 58 std::map<std::string, std::string>::const_iterator it = |
| 59 request.headers.find("User-Agent"); |
| 60 EXPECT_TRUE(it != request.headers.end()); |
| 61 if (!StartsWithASCII("foobar", it->second, true)) |
| 62 return scoped_ptr<net::test_server::HttpResponse>(); |
| 63 |
| 64 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
| 65 new net::test_server::BasicHttpResponse); |
| 66 http_response->set_code(net::HTTP_MOVED_PERMANENTLY); |
| 67 http_response->AddCustomHeader("Location", redirect_target.spec()); |
| 68 return http_response.PassAs<net::test_server::HttpResponse>(); |
| 69 } |
| 70 |
| 48 // Handles |request| by serving a redirect response. | 71 // Handles |request| by serving a redirect response. |
| 49 scoped_ptr<net::test_server::HttpResponse> RedirectResponseHandler( | 72 scoped_ptr<net::test_server::HttpResponse> RedirectResponseHandler( |
| 50 const std::string& path, | 73 const std::string& path, |
| 51 const GURL& redirect_target, | 74 const GURL& redirect_target, |
| 52 const net::test_server::HttpRequest& request) { | 75 const net::test_server::HttpRequest& request) { |
| 53 if (!StartsWithASCII(path, request.relative_url, true)) | 76 if (!StartsWithASCII(path, request.relative_url, true)) |
| 54 return scoped_ptr<net::test_server::HttpResponse>(); | 77 return scoped_ptr<net::test_server::HttpResponse>(); |
| 55 | 78 |
| 56 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 79 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
| 57 new net::test_server::BasicHttpResponse); | 80 new net::test_server::BasicHttpResponse); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 161 |
| 139 test_config_.SetInteger(kTestServerPort, embedded_test_server()->port()); | 162 test_config_.SetInteger(kTestServerPort, embedded_test_server()->port()); |
| 140 | 163 |
| 141 embedded_test_server()->RegisterRequestHandler( | 164 embedded_test_server()->RegisterRequestHandler( |
| 142 base::Bind(&RedirectResponseHandler, | 165 base::Bind(&RedirectResponseHandler, |
| 143 kRedirectResponsePath, | 166 kRedirectResponsePath, |
| 144 embedded_test_server()->GetURL(kRedirectResponseFullPath))); | 167 embedded_test_server()->GetURL(kRedirectResponseFullPath))); |
| 145 | 168 |
| 146 embedded_test_server()->RegisterRequestHandler( | 169 embedded_test_server()->RegisterRequestHandler( |
| 147 base::Bind(&EmptyResponseHandler, kEmptyResponsePath)); | 170 base::Bind(&EmptyResponseHandler, kEmptyResponsePath)); |
| 171 |
| 172 embedded_test_server()->RegisterRequestHandler( |
| 173 base::Bind( |
| 174 &UserAgentResponseHandler, |
| 175 kUserAgentRedirectResponsePath, |
| 176 embedded_test_server()->GetURL(kRedirectResponseFullPath))); |
| 148 } | 177 } |
| 149 | 178 |
| 150 void WebViewAPITest::StopTestServer() { | 179 void WebViewAPITest::StopTestServer() { |
| 151 if (!embedded_test_server()->ShutdownAndWaitUntilComplete()) { | 180 if (!embedded_test_server()->ShutdownAndWaitUntilComplete()) { |
| 152 LOG(ERROR) << "Failed to shutdown test server."; | 181 LOG(ERROR) << "Failed to shutdown test server."; |
| 153 } | 182 } |
| 154 } | 183 } |
| 155 | 184 |
| 156 void WebViewAPITest::TearDownOnMainThread() { | 185 void WebViewAPITest::TearDownOnMainThread() { |
| 157 TestGetConfigFunction::set_test_config_state(nullptr); | 186 TestGetConfigFunction::set_test_config_state(nullptr); |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestTerminateAfterExit) { | 470 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestTerminateAfterExit) { |
| 442 RunTest("testTerminateAfterExit", "web_view/apitest"); | 471 RunTest("testTerminateAfterExit", "web_view/apitest"); |
| 443 } | 472 } |
| 444 | 473 |
| 445 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPI) { | 474 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPI) { |
| 446 StartTestServer(); | 475 StartTestServer(); |
| 447 RunTest("testWebRequestAPI", "web_view/apitest"); | 476 RunTest("testWebRequestAPI", "web_view/apitest"); |
| 448 StopTestServer(); | 477 StopTestServer(); |
| 449 } | 478 } |
| 450 | 479 |
| 480 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIWithHeaders) { |
| 481 StartTestServer(); |
| 482 RunTest("testWebRequestAPIWithHeaders", "web_view/apitest"); |
| 483 StopTestServer(); |
| 484 } |
| 485 |
| 451 // Tests the existence of WebRequest API event objects on the request | 486 // Tests the existence of WebRequest API event objects on the request |
| 452 // object, on the webview element, and hanging directly off webview. | 487 // object, on the webview element, and hanging directly off webview. |
| 453 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIExistence) { | 488 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIExistence) { |
| 454 StartTestServer(); | 489 StartTestServer(); |
| 455 RunTest("testWebRequestAPIExistence", "web_view/apitest"); | 490 RunTest("testWebRequestAPIExistence", "web_view/apitest"); |
| 456 StopTestServer(); | 491 StopTestServer(); |
| 457 } | 492 } |
| 458 | 493 |
| 459 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIGoogleProperty) { | 494 IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestWebRequestAPIGoogleProperty) { |
| 460 StartTestServer(); | 495 StartTestServer(); |
| 461 RunTest("testWebRequestAPIGoogleProperty", "web_view/apitest"); | 496 RunTest("testWebRequestAPIGoogleProperty", "web_view/apitest"); |
| 462 StopTestServer(); | 497 StopTestServer(); |
| 463 } | 498 } |
| 464 | 499 |
| 465 } // namespace extensions | 500 } // namespace extensions |
| OLD | NEW |