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

Side by Side Diff: headless/lib/headless_browser_browsertest.cc

Issue 2024973002: headless: Allow protocol handler customization (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix typo Created 4 years, 6 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 unified diff | Download patch
OLDNEW
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"
7 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
8 #include "content/public/test/browser_test.h" 9 #include "content/public/test/browser_test.h"
10 #include "headless/public/domains/types.h"
9 #include "headless/public/headless_browser.h" 11 #include "headless/public/headless_browser.h"
10 #include "headless/public/headless_web_contents.h" 12 #include "headless/public/headless_web_contents.h"
11 #include "headless/test/headless_browser_test.h" 13 #include "headless/test/headless_browser_test.h"
14 #include "net/base/io_buffer.h"
15 #include "net/http/http_response_headers.h"
12 #include "net/test/spawned_test_server/spawned_test_server.h" 16 #include "net/test/spawned_test_server/spawned_test_server.h"
17 #include "net/url_request/url_request_job.h"
13 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/geometry/size.h" 19 #include "ui/gfx/geometry/size.h"
15 20
16 namespace headless { 21 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 std::string body_;
38 size_t read_offset_ = 0;
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), body_(body) {}
47
48 void TestURLRequestJob::Start() {
49 NotifyHeadersComplete();
50 }
51
52 void TestURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) {
53 info->headers =
54 new net::HttpResponseHeaders("Content-Type: text/html\r\n\r\n");
55 }
56
57 int TestURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) {
58 size_t bytes_read = 0;
59 for (int i = 0; i < buf_size; i++) {
alex clarke (OOO till 29th) 2016/05/31 16:55:50 Nit: this class is small enough it probably doesn'
Sami 2016/05/31 18:01:38 Good point, replaced with some convenience classes
60 if (read_offset_ >= body_.size())
61 return bytes_read;
62 buf->data()[i] = body_[read_offset_++];
63 bytes_read++;
64 }
65 return bytes_read;
66 }
67
68 class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
69 public:
70 TestProtocolHandler(const std::string& body);
71 ~TestProtocolHandler() override {}
72
73 net::URLRequestJob* MaybeCreateJob(
74 net::URLRequest* request,
75 net::NetworkDelegate* network_delegate) const override;
76
77 private:
78 std::string body_;
79
80 DISALLOW_COPY_AND_ASSIGN(TestProtocolHandler);
81 };
82
83 TestProtocolHandler::TestProtocolHandler(const std::string& body)
84 : body_(body) {}
85
86 net::URLRequestJob* TestProtocolHandler::MaybeCreateJob(
87 net::URLRequest* request,
88 net::NetworkDelegate* network_delegate) const {
89 return new TestURLRequestJob(request, network_delegate, body_);
90 }
91
92 } // namespace
17 93
18 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { 94 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) {
19 HeadlessWebContents* web_contents = 95 HeadlessWebContents* web_contents =
20 browser()->CreateWebContents(GURL("about:blank"), gfx::Size(800, 600)); 96 browser()->CreateWebContents(GURL("about:blank"), gfx::Size(800, 600));
21 EXPECT_TRUE(web_contents); 97 EXPECT_TRUE(web_contents);
22 98
23 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size()); 99 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size());
24 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]); 100 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]);
25 // TODO(skyostil): Verify viewport dimensions once we can. 101 // TODO(skyostil): Verify viewport dimensions once we can.
26 web_contents->Close(); 102 web_contents->Close();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 embedded_test_server()->host_port_pair().port())); 163 embedded_test_server()->host_port_pair().port()));
88 SetBrowserOptions(builder.Build()); 164 SetBrowserOptions(builder.Build());
89 165
90 // Load a page which doesn't actually exist, but which is turned into a valid 166 // Load a page which doesn't actually exist, but which is turned into a valid
91 // address by our host resolver rules. 167 // address by our host resolver rules.
92 HeadlessWebContents* web_contents = browser()->CreateWebContents( 168 HeadlessWebContents* web_contents = browser()->CreateWebContents(
93 GURL("http://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600)); 169 GURL("http://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600));
94 EXPECT_TRUE(WaitForLoad(web_contents)); 170 EXPECT_TRUE(WaitForLoad(web_contents));
95 } 171 }
96 172
173 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, HttpProtocolHandler) {
174 const std::string kResponseBody = "<p>HTTP response body</p>";
175 ProtocolHandlerMap protocol_handlers;
176 protocol_handlers[url::kHttpScheme] =
177 base::WrapUnique(new TestProtocolHandler(kResponseBody));
178
179 HeadlessBrowser::Options::Builder builder;
180 builder.SetProtocolHandlers(std::move(protocol_handlers));
181 SetBrowserOptions(builder.Build());
182
183 // Load a page which doesn't actually exist, but which is fetched by our
184 // custom protocol handler.
185 HeadlessWebContents* web_contents = browser()->CreateWebContents(
186 GURL("http://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600));
187 EXPECT_TRUE(WaitForLoad(web_contents));
188
189 std::string inner_html;
190 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML")
191 ->GetResult()
192 ->GetValue()
193 ->GetAsString(&inner_html));
194 EXPECT_EQ(kResponseBody, inner_html);
195 }
196
197 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, HttpsProtocolHandler) {
198 const std::string kResponseBody = "<p>HTTPS response body</p>";
199 ProtocolHandlerMap protocol_handlers;
200 protocol_handlers[url::kHttpsScheme] =
201 base::WrapUnique(new TestProtocolHandler(kResponseBody));
202
203 HeadlessBrowser::Options::Builder builder;
204 builder.SetProtocolHandlers(std::move(protocol_handlers));
205 SetBrowserOptions(builder.Build());
206
207 // Load a page which doesn't actually exist, but which is fetched by our
208 // custom protocol handler.
209 HeadlessWebContents* web_contents = browser()->CreateWebContents(
210 GURL("https://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600));
211 EXPECT_TRUE(WaitForLoad(web_contents));
212
213 std::string inner_html;
214 EXPECT_TRUE(EvaluateScript(web_contents, "document.body.innerHTML")
215 ->GetResult()
216 ->GetValue()
217 ->GetAsString(&inner_html));
218 EXPECT_EQ(kResponseBody, inner_html);
219 }
220
97 } // namespace headless 221 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698