| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 9 #include "content/public/test/browser_test.h" | 8 #include "content/public/test/browser_test.h" |
| 10 #include "headless/public/domains/types.h" | |
| 11 #include "headless/public/headless_browser.h" | 9 #include "headless/public/headless_browser.h" |
| 12 #include "headless/public/headless_web_contents.h" | 10 #include "headless/public/headless_web_contents.h" |
| 13 #include "headless/test/headless_browser_test.h" | 11 #include "headless/test/headless_browser_test.h" |
| 14 #include "net/base/io_buffer.h" | |
| 15 #include "net/http/http_response_headers.h" | |
| 16 #include "net/test/spawned_test_server/spawned_test_server.h" | 12 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 17 #include "net/url_request/url_request_job.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "ui/gfx/geometry/size.h" | 14 #include "ui/gfx/geometry/size.h" |
| 20 | 15 |
| 21 namespace headless { | 16 namespace headless { |
| 22 namespace { | |
| 23 | |
| 24 class TestURLRequestJob : public net::URLRequestJob { | |
| 25 public: | |
| 26 TestURLRequestJob(net::URLRequest* request, | |
| 27 net::NetworkDelegate* network_delegate, | |
| 28 const std::string& body); | |
| 29 ~TestURLRequestJob() override {} | |
| 30 | |
| 31 // net::URLRequestJob implementation: | |
| 32 void Start() override; | |
| 33 void GetResponseInfo(net::HttpResponseInfo* info) override; | |
| 34 int ReadRawData(net::IOBuffer* buf, int buf_size) override; | |
| 35 | |
| 36 private: | |
| 37 scoped_refptr<net::StringIOBuffer> body_; | |
| 38 scoped_refptr<net::DrainableIOBuffer> src_buf_; | |
| 39 | |
| 40 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJob); | |
| 41 }; | |
| 42 | |
| 43 TestURLRequestJob::TestURLRequestJob(net::URLRequest* request, | |
| 44 net::NetworkDelegate* network_delegate, | |
| 45 const std::string& body) | |
| 46 : net::URLRequestJob(request, network_delegate), | |
| 47 body_(new net::StringIOBuffer(body)), | |
| 48 src_buf_(new net::DrainableIOBuffer(body_.get(), body_->size())) {} | |
| 49 | |
| 50 void TestURLRequestJob::Start() { | |
| 51 NotifyHeadersComplete(); | |
| 52 } | |
| 53 | |
| 54 void TestURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { | |
| 55 info->headers = | |
| 56 new net::HttpResponseHeaders("Content-Type: text/html\r\n\r\n"); | |
| 57 } | |
| 58 | |
| 59 int TestURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { | |
| 60 scoped_refptr<net::DrainableIOBuffer> dest_buf( | |
| 61 new net::DrainableIOBuffer(buf, buf_size)); | |
| 62 while (src_buf_->BytesRemaining() > 0 && dest_buf->BytesRemaining() > 0) { | |
| 63 *dest_buf->data() = *src_buf_->data(); | |
| 64 src_buf_->DidConsume(1); | |
| 65 dest_buf->DidConsume(1); | |
| 66 } | |
| 67 return dest_buf->BytesConsumed(); | |
| 68 } | |
| 69 | |
| 70 class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { | |
| 71 public: | |
| 72 TestProtocolHandler(const std::string& body); | |
| 73 ~TestProtocolHandler() override {} | |
| 74 | |
| 75 net::URLRequestJob* MaybeCreateJob( | |
| 76 net::URLRequest* request, | |
| 77 net::NetworkDelegate* network_delegate) const override; | |
| 78 | |
| 79 private: | |
| 80 std::string body_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(TestProtocolHandler); | |
| 83 }; | |
| 84 | |
| 85 TestProtocolHandler::TestProtocolHandler(const std::string& body) | |
| 86 : body_(body) {} | |
| 87 | |
| 88 net::URLRequestJob* TestProtocolHandler::MaybeCreateJob( | |
| 89 net::URLRequest* request, | |
| 90 net::NetworkDelegate* network_delegate) const { | |
| 91 return new TestURLRequestJob(request, network_delegate, body_); | |
| 92 } | |
| 93 | |
| 94 } // namespace | |
| 95 | 17 |
| 96 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { | 18 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { |
| 97 HeadlessWebContents* web_contents = | 19 HeadlessWebContents* web_contents = |
| 98 browser()->CreateWebContents(GURL("about:blank"), gfx::Size(800, 600)); | 20 browser()->CreateWebContents(GURL("about:blank"), gfx::Size(800, 600)); |
| 99 EXPECT_TRUE(web_contents); | 21 EXPECT_TRUE(web_contents); |
| 100 | 22 |
| 101 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size()); | 23 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size()); |
| 102 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]); | 24 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]); |
| 103 // TODO(skyostil): Verify viewport dimensions once we can. | 25 // TODO(skyostil): Verify viewport dimensions once we can. |
| 104 web_contents->Close(); | 26 web_contents->Close(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 embedded_test_server()->host_port_pair().port())); | 87 embedded_test_server()->host_port_pair().port())); |
| 166 SetBrowserOptions(builder.Build()); | 88 SetBrowserOptions(builder.Build()); |
| 167 | 89 |
| 168 // Load a page which doesn't actually exist, but which is turned into a valid | 90 // Load a page which doesn't actually exist, but which is turned into a valid |
| 169 // address by our host resolver rules. | 91 // address by our host resolver rules. |
| 170 HeadlessWebContents* web_contents = browser()->CreateWebContents( | 92 HeadlessWebContents* web_contents = browser()->CreateWebContents( |
| 171 GURL("http://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600)); | 93 GURL("http://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600)); |
| 172 EXPECT_TRUE(WaitForLoad(web_contents)); | 94 EXPECT_TRUE(WaitForLoad(web_contents)); |
| 173 } | 95 } |
| 174 | 96 |
| 175 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, HttpProtocolHandler) { | |
| 176 const std::string kResponseBody = "<p>HTTP response body</p>"; | |
| 177 ProtocolHandlerMap protocol_handlers; | |
| 178 protocol_handlers[url::kHttpScheme] = | |
| 179 base::WrapUnique(new TestProtocolHandler(kResponseBody)); | |
| 180 | |
| 181 HeadlessBrowser::Options::Builder builder; | |
| 182 builder.SetProtocolHandlers(std::move(protocol_handlers)); | |
| 183 SetBrowserOptions(builder.Build()); | |
| 184 | |
| 185 // Load a page which doesn't actually exist, but which is fetched by our | |
| 186 // custom protocol handler. | |
| 187 HeadlessWebContents* web_contents = browser()->CreateWebContents( | |
| 188 GURL("http://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600)); | |
| 189 EXPECT_TRUE(WaitForLoad(web_contents)); | |
| 190 | |
| 191 std::string inner_html; | |
| 192 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") | |
| 193 ->GetResult() | |
| 194 ->GetValue() | |
| 195 ->GetAsString(&inner_html)); | |
| 196 EXPECT_EQ(kResponseBody, inner_html); | |
| 197 } | |
| 198 | |
| 199 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, HttpsProtocolHandler) { | |
| 200 const std::string kResponseBody = "<p>HTTPS response body</p>"; | |
| 201 ProtocolHandlerMap protocol_handlers; | |
| 202 protocol_handlers[url::kHttpsScheme] = | |
| 203 base::WrapUnique(new TestProtocolHandler(kResponseBody)); | |
| 204 | |
| 205 HeadlessBrowser::Options::Builder builder; | |
| 206 builder.SetProtocolHandlers(std::move(protocol_handlers)); | |
| 207 SetBrowserOptions(builder.Build()); | |
| 208 | |
| 209 // Load a page which doesn't actually exist, but which is fetched by our | |
| 210 // custom protocol handler. | |
| 211 HeadlessWebContents* web_contents = browser()->CreateWebContents( | |
| 212 GURL("https://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600)); | |
| 213 EXPECT_TRUE(WaitForLoad(web_contents)); | |
| 214 | |
| 215 std::string inner_html; | |
| 216 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML") | |
| 217 ->GetResult() | |
| 218 ->GetValue() | |
| 219 ->GetAsString(&inner_html)); | |
| 220 EXPECT_EQ(kResponseBody, inner_html); | |
| 221 } | |
| 222 | |
| 223 } // namespace headless | 97 } // namespace headless |
| OLD | NEW |