OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/public/test/browser_test.h" | |
6 #include "headless/public/devtools/domains/network.h" | |
7 #include "headless/public/devtools/domains/page.h" | |
8 #include "headless/public/headless_devtools_client.h" | |
9 #include "headless/public/util/expedited_dispatcher.h" | |
10 #include "headless/public/util/generic_url_request_job.h" | |
11 #include "headless/public/util/url_fetcher.h" | |
12 #include "headless/test/headless_browser_test.h" | |
13 #include "net/url_request/url_request_job_factory.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "url/gurl.h" | |
17 | |
18 using testing::ContainerEq; | |
19 | |
20 namespace headless { | |
21 | |
22 namespace { | |
23 class RequestIdCorrelationProtocolHandler | |
24 : public net::URLRequestJobFactory::ProtocolHandler { | |
25 public: | |
26 explicit RequestIdCorrelationProtocolHandler( | |
27 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner) | |
28 : test_delegate_(new TestDelegate(this)), | |
29 dispatcher_(new ExpeditedDispatcher(io_thread_task_runner)) {} | |
30 | |
31 ~RequestIdCorrelationProtocolHandler() override {} | |
32 | |
33 struct Response { | |
34 Response() {} | |
35 Response(const std::string& body, const std::string& mime_type) | |
36 : data("HTTP/1.1 200 OK\r\nContent-Type: " + mime_type + "\r\n\r\n" + | |
37 body) {} | |
38 | |
39 std::string data; | |
40 }; | |
41 | |
42 void InsertResponse(const std::string& url, const Response& response) { | |
43 response_map_[url] = response; | |
44 } | |
45 | |
46 const Response* GetResponse(const std::string& url) const { | |
47 std::map<std::string, Response>::const_iterator find_it = | |
48 response_map_.find(url); | |
49 if (find_it == response_map_.end()) | |
50 return nullptr; | |
51 return &find_it->second; | |
52 } | |
53 | |
54 class MockURLFetcher : public URLFetcher { | |
55 public: | |
56 explicit MockURLFetcher( | |
57 const RequestIdCorrelationProtocolHandler* protocol_handler) | |
58 : protocol_handler_(protocol_handler) {} | |
59 ~MockURLFetcher() override {} | |
60 | |
61 // MockURLFetcher implementation: | |
Sami
2017/02/27 12:39:07
nit: s/Mock//
alex clarke (OOO till 29th)
2017/02/27 13:51:16
Done.
| |
62 void StartFetch(const GURL& url, | |
63 const std::string& method, | |
64 const net::HttpRequestHeaders& request_headers, | |
Sami
2017/02/27 12:39:07
Should we check that the magic header isn't in req
alex clarke (OOO till 29th)
2017/02/27 13:51:16
Done.
| |
65 ResultListener* result_listener) override { | |
66 const Response* response = protocol_handler_->GetResponse(url.spec()); | |
67 if (!response) | |
68 result_listener->OnFetchStartError(net::ERR_FILE_NOT_FOUND); | |
69 | |
70 result_listener->OnFetchCompleteExtractHeaders( | |
71 url, 200, response->data.c_str(), response->data.size()); | |
72 } | |
73 | |
74 private: | |
75 const RequestIdCorrelationProtocolHandler* protocol_handler_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(MockURLFetcher); | |
78 }; | |
79 | |
80 class TestDelegate : public GenericURLRequestJob::Delegate { | |
81 public: | |
82 explicit TestDelegate(RequestIdCorrelationProtocolHandler* protocol_handler) | |
83 : protocol_handler_(protocol_handler) {} | |
84 ~TestDelegate() override {} | |
85 | |
86 // GenericURLRequestJob::Delegate implementation: | |
87 bool BlockOrRewriteRequest( | |
88 const GURL& url, | |
89 const std::string& devtools_id, | |
90 const std::string& method, | |
91 const std::string& referrer, | |
92 GenericURLRequestJob::RewriteCallback callback) override { | |
93 protocol_handler_->url_to_devtools_id_[url.spec()] = devtools_id; | |
94 return false; | |
95 } | |
96 | |
97 const GenericURLRequestJob::HttpResponse* MaybeMatchResource( | |
98 const GURL& url, | |
99 const std::string& devtools_id, | |
100 const std::string& method, | |
101 const net::HttpRequestHeaders& request_headers) override { | |
102 return nullptr; | |
103 } | |
104 | |
105 void OnResourceLoadComplete(const GURL& final_url, | |
106 const std::string& devtools_id, | |
107 const std::string& mime_type, | |
108 int http_response_code) override {} | |
109 | |
110 private: | |
111 RequestIdCorrelationProtocolHandler* protocol_handler_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
114 }; | |
115 | |
116 // net::URLRequestJobFactory::ProtocolHandler implementation:: | |
117 net::URLRequestJob* MaybeCreateJob( | |
118 net::URLRequest* request, | |
119 net::NetworkDelegate* network_delegate) const override { | |
120 return new GenericURLRequestJob( | |
121 request, network_delegate, dispatcher_.get(), | |
122 base::MakeUnique<MockURLFetcher>(this), test_delegate_.get()); | |
123 } | |
124 | |
125 std::map<std::string, std::string> url_to_devtools_id_; | |
126 | |
127 private: | |
128 std::unique_ptr<TestDelegate> test_delegate_; | |
129 std::unique_ptr<ExpeditedDispatcher> dispatcher_; | |
130 std::map<std::string, Response> response_map_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(RequestIdCorrelationProtocolHandler); | |
133 }; | |
134 | |
135 const char* kIndexHtml = R"( | |
136 <html> | |
137 <head> | |
138 <link rel="stylesheet" type="text/css" href="style1.css"> | |
139 <link rel="stylesheet" type="text/css" href="style2.css"> | |
140 </head> | |
141 <body>Hello. | |
142 </body> | |
143 </html>)"; | |
144 | |
145 const char* kStyle1 = R"( | |
146 .border { | |
147 border: 1px solid #000; | |
148 })"; | |
149 | |
150 const char* kStyle2 = R"( | |
151 .border { | |
152 border: 2px solid #fff; | |
153 })"; | |
154 | |
155 } // namespace | |
156 | |
157 class ProtocolHandlerRequestIdCorrelationTest | |
158 : public HeadlessAsyncDevTooledBrowserTest, | |
159 public network::Observer, | |
160 public page::Observer { | |
161 public: | |
162 void RunDevTooledTest() override { | |
163 EXPECT_TRUE(embedded_test_server()->Start()); | |
164 devtools_client_->GetPage()->AddObserver(this); | |
165 devtools_client_->GetPage()->Enable(); | |
166 devtools_client_->GetNetwork()->AddObserver(this); | |
167 devtools_client_->GetNetwork()->Enable(); | |
168 devtools_client_->GetPage()->Navigate("http://foo.com/index.html"); | |
169 } | |
170 | |
171 ProtocolHandlerMap GetProtocolHandlers() override { | |
172 ProtocolHandlerMap protocol_handlers; | |
173 std::unique_ptr<RequestIdCorrelationProtocolHandler> http_handler( | |
174 new RequestIdCorrelationProtocolHandler(browser()->BrowserIOThread())); | |
175 http_handler_ = http_handler.get(); | |
176 http_handler_->InsertResponse("http://foo.com/index.html", | |
177 {kIndexHtml, "text/html"}); | |
178 http_handler_->InsertResponse("http://foo.com/style1.css", | |
179 {kStyle1, "text/css"}); | |
180 http_handler_->InsertResponse("http://foo.com/style2.css", | |
181 {kStyle2, "text/css"}); | |
182 protocol_handlers[url::kHttpScheme] = std::move(http_handler); | |
183 return protocol_handlers; | |
184 } | |
185 | |
186 // network::Observer implementation: | |
187 void OnRequestWillBeSent( | |
188 const network::RequestWillBeSentParams& params) override { | |
189 url_to_devtools_id_[params.GetRequest()->GetUrl()] = params.GetRequestId(); | |
Sami
2017/02/27 12:39:07
Maybe also check that the magic header isn't inclu
alex clarke (OOO till 29th)
2017/02/27 13:51:16
Done.
| |
190 } | |
191 | |
192 // page::Observer implementation: | |
193 void OnLoadEventFired(const page::LoadEventFiredParams& params) override { | |
194 // Make sure that our protocol handler saw the same url : devtools ids as | |
195 // our OnRequestWillBeSent event listener did. | |
196 EXPECT_THAT(url_to_devtools_id_, | |
197 ContainerEq(http_handler_->url_to_devtools_id_)); | |
198 EXPECT_EQ(3u, url_to_devtools_id_.size()); | |
199 FinishAsynchronousTest(); | |
200 } | |
201 | |
202 private: | |
203 std::map<std::string, std::string> url_to_devtools_id_; | |
204 RequestIdCorrelationProtocolHandler* http_handler_; // NOT OWNED | |
205 }; | |
206 | |
207 HEADLESS_ASYNC_DEVTOOLED_TEST_F(ProtocolHandlerRequestIdCorrelationTest); | |
208 | |
209 } // namespace headless | |
OLD | NEW |