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 // Keep in sync with X_DevTools_Request_Id defined in HTTPNames.json5. |
| 24 const char kDevtoolsRequestId[] = "X-DevTools-Request-Id"; |
| 25 } // namespace |
| 26 |
| 27 namespace { |
| 28 class RequestIdCorrelationProtocolHandler |
| 29 : public net::URLRequestJobFactory::ProtocolHandler { |
| 30 public: |
| 31 explicit RequestIdCorrelationProtocolHandler( |
| 32 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner) |
| 33 : test_delegate_(new TestDelegate(this)), |
| 34 dispatcher_(new ExpeditedDispatcher(io_thread_task_runner)) {} |
| 35 |
| 36 ~RequestIdCorrelationProtocolHandler() override {} |
| 37 |
| 38 struct Response { |
| 39 Response() {} |
| 40 Response(const std::string& body, const std::string& mime_type) |
| 41 : data("HTTP/1.1 200 OK\r\nContent-Type: " + mime_type + "\r\n\r\n" + |
| 42 body) {} |
| 43 |
| 44 std::string data; |
| 45 }; |
| 46 |
| 47 void InsertResponse(const std::string& url, const Response& response) { |
| 48 response_map_[url] = response; |
| 49 } |
| 50 |
| 51 const Response* GetResponse(const std::string& url) const { |
| 52 std::map<std::string, Response>::const_iterator find_it = |
| 53 response_map_.find(url); |
| 54 if (find_it == response_map_.end()) |
| 55 return nullptr; |
| 56 return &find_it->second; |
| 57 } |
| 58 |
| 59 class MockURLFetcher : public URLFetcher { |
| 60 public: |
| 61 explicit MockURLFetcher( |
| 62 const RequestIdCorrelationProtocolHandler* protocol_handler) |
| 63 : protocol_handler_(protocol_handler) {} |
| 64 ~MockURLFetcher() override {} |
| 65 |
| 66 // URLFetcher implementation: |
| 67 void StartFetch(const GURL& url, |
| 68 const std::string& method, |
| 69 const net::HttpRequestHeaders& request_headers, |
| 70 ResultListener* result_listener) override { |
| 71 const Response* response = protocol_handler_->GetResponse(url.spec()); |
| 72 if (!response) |
| 73 result_listener->OnFetchStartError(net::ERR_FILE_NOT_FOUND); |
| 74 |
| 75 // The header used for correlation should not be sent to the fetcher. |
| 76 EXPECT_FALSE(request_headers.HasHeader(kDevtoolsRequestId)); |
| 77 |
| 78 result_listener->OnFetchCompleteExtractHeaders( |
| 79 url, 200, response->data.c_str(), response->data.size()); |
| 80 } |
| 81 |
| 82 private: |
| 83 const RequestIdCorrelationProtocolHandler* protocol_handler_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(MockURLFetcher); |
| 86 }; |
| 87 |
| 88 class TestDelegate : public GenericURLRequestJob::Delegate { |
| 89 public: |
| 90 explicit TestDelegate(RequestIdCorrelationProtocolHandler* protocol_handler) |
| 91 : protocol_handler_(protocol_handler) {} |
| 92 ~TestDelegate() override {} |
| 93 |
| 94 // GenericURLRequestJob::Delegate implementation: |
| 95 bool BlockOrRewriteRequest( |
| 96 const GURL& url, |
| 97 const std::string& devtools_id, |
| 98 const std::string& method, |
| 99 const std::string& referrer, |
| 100 GenericURLRequestJob::RewriteCallback callback) override { |
| 101 protocol_handler_->url_to_devtools_id_[url.spec()] = devtools_id; |
| 102 return false; |
| 103 } |
| 104 |
| 105 const GenericURLRequestJob::HttpResponse* MaybeMatchResource( |
| 106 const GURL& url, |
| 107 const std::string& devtools_id, |
| 108 const std::string& method, |
| 109 const net::HttpRequestHeaders& request_headers) override { |
| 110 return nullptr; |
| 111 } |
| 112 |
| 113 void OnResourceLoadComplete(const GURL& final_url, |
| 114 const std::string& devtools_id, |
| 115 const std::string& mime_type, |
| 116 int http_response_code) override {} |
| 117 |
| 118 private: |
| 119 RequestIdCorrelationProtocolHandler* protocol_handler_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 122 }; |
| 123 |
| 124 // net::URLRequestJobFactory::ProtocolHandler implementation:: |
| 125 net::URLRequestJob* MaybeCreateJob( |
| 126 net::URLRequest* request, |
| 127 net::NetworkDelegate* network_delegate) const override { |
| 128 return new GenericURLRequestJob( |
| 129 request, network_delegate, dispatcher_.get(), |
| 130 base::MakeUnique<MockURLFetcher>(this), test_delegate_.get()); |
| 131 } |
| 132 |
| 133 std::map<std::string, std::string> url_to_devtools_id_; |
| 134 |
| 135 private: |
| 136 std::unique_ptr<TestDelegate> test_delegate_; |
| 137 std::unique_ptr<ExpeditedDispatcher> dispatcher_; |
| 138 std::map<std::string, Response> response_map_; |
| 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(RequestIdCorrelationProtocolHandler); |
| 141 }; |
| 142 |
| 143 const char* kIndexHtml = R"( |
| 144 <html> |
| 145 <head> |
| 146 <link rel="stylesheet" type="text/css" href="style1.css"> |
| 147 <link rel="stylesheet" type="text/css" href="style2.css"> |
| 148 </head> |
| 149 <body>Hello. |
| 150 </body> |
| 151 </html>)"; |
| 152 |
| 153 const char* kStyle1 = R"( |
| 154 .border { |
| 155 border: 1px solid #000; |
| 156 })"; |
| 157 |
| 158 const char* kStyle2 = R"( |
| 159 .border { |
| 160 border: 2px solid #fff; |
| 161 })"; |
| 162 |
| 163 } // namespace |
| 164 |
| 165 class ProtocolHandlerRequestIdCorrelationTest |
| 166 : public HeadlessAsyncDevTooledBrowserTest, |
| 167 public network::Observer, |
| 168 public page::Observer { |
| 169 public: |
| 170 void RunDevTooledTest() override { |
| 171 EXPECT_TRUE(embedded_test_server()->Start()); |
| 172 devtools_client_->GetPage()->AddObserver(this); |
| 173 devtools_client_->GetPage()->Enable(); |
| 174 devtools_client_->GetNetwork()->AddObserver(this); |
| 175 devtools_client_->GetNetwork()->Enable(); |
| 176 devtools_client_->GetPage()->Navigate("http://foo.com/index.html"); |
| 177 } |
| 178 |
| 179 ProtocolHandlerMap GetProtocolHandlers() override { |
| 180 ProtocolHandlerMap protocol_handlers; |
| 181 std::unique_ptr<RequestIdCorrelationProtocolHandler> http_handler( |
| 182 new RequestIdCorrelationProtocolHandler(browser()->BrowserIOThread())); |
| 183 http_handler_ = http_handler.get(); |
| 184 http_handler_->InsertResponse("http://foo.com/index.html", |
| 185 {kIndexHtml, "text/html"}); |
| 186 http_handler_->InsertResponse("http://foo.com/style1.css", |
| 187 {kStyle1, "text/css"}); |
| 188 http_handler_->InsertResponse("http://foo.com/style2.css", |
| 189 {kStyle2, "text/css"}); |
| 190 protocol_handlers[url::kHttpScheme] = std::move(http_handler); |
| 191 return protocol_handlers; |
| 192 } |
| 193 |
| 194 // network::Observer implementation: |
| 195 void OnRequestWillBeSent( |
| 196 const network::RequestWillBeSentParams& params) override { |
| 197 url_to_devtools_id_[params.GetRequest()->GetUrl()] = params.GetRequestId(); |
| 198 EXPECT_FALSE(params.GetRequest()->GetHeaders()->HasKey(kDevtoolsRequestId)); |
| 199 } |
| 200 |
| 201 // page::Observer implementation: |
| 202 void OnLoadEventFired(const page::LoadEventFiredParams& params) override { |
| 203 // Make sure that our protocol handler saw the same url : devtools ids as |
| 204 // our OnRequestWillBeSent event listener did. |
| 205 EXPECT_THAT(url_to_devtools_id_, |
| 206 ContainerEq(http_handler_->url_to_devtools_id_)); |
| 207 EXPECT_EQ(3u, url_to_devtools_id_.size()); |
| 208 FinishAsynchronousTest(); |
| 209 } |
| 210 |
| 211 private: |
| 212 std::map<std::string, std::string> url_to_devtools_id_; |
| 213 RequestIdCorrelationProtocolHandler* http_handler_; // NOT OWNED |
| 214 }; |
| 215 |
| 216 HEADLESS_ASYNC_DEVTOOLED_TEST_F(ProtocolHandlerRequestIdCorrelationTest); |
| 217 |
| 218 } // namespace headless |
OLD | NEW |