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

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: Review comments 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 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
17 95
18 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { 96 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) {
19 HeadlessWebContents* web_contents = 97 HeadlessWebContents* web_contents =
20 browser()->CreateWebContents(GURL("about:blank"), gfx::Size(800, 600)); 98 browser()->CreateWebContents(GURL("about:blank"), gfx::Size(800, 600));
21 EXPECT_TRUE(web_contents); 99 EXPECT_TRUE(web_contents);
22 100
23 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size()); 101 EXPECT_EQ(static_cast<size_t>(1), browser()->GetAllWebContents().size());
24 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]); 102 EXPECT_EQ(web_contents, browser()->GetAllWebContents()[0]);
25 // TODO(skyostil): Verify viewport dimensions once we can. 103 // TODO(skyostil): Verify viewport dimensions once we can.
26 web_contents->Close(); 104 web_contents->Close();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 embedded_test_server()->host_port_pair().port())); 165 embedded_test_server()->host_port_pair().port()));
88 SetBrowserOptions(builder.Build()); 166 SetBrowserOptions(builder.Build());
89 167
90 // Load a page which doesn't actually exist, but which is turned into a valid 168 // Load a page which doesn't actually exist, but which is turned into a valid
91 // address by our host resolver rules. 169 // address by our host resolver rules.
92 HeadlessWebContents* web_contents = browser()->CreateWebContents( 170 HeadlessWebContents* web_contents = browser()->CreateWebContents(
93 GURL("http://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600)); 171 GURL("http://not-an-actual-domain.tld/hello.html"), gfx::Size(800, 600));
94 EXPECT_TRUE(WaitForLoad(web_contents)); 172 EXPECT_TRUE(WaitForLoad(web_contents));
95 } 173 }
96 174
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
97 } // namespace headless 223 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698