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 "headless/public/util/generic_url_request_job.h" | 5 #include "headless/public/util/generic_url_request_job.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 MockFetcher(base::DictionaryValue* fetch_request, | 46 MockFetcher(base::DictionaryValue* fetch_request, |
47 const std::string& json_reply) | 47 const std::string& json_reply) |
48 : fetch_reply_(base::JSONReader::Read(json_reply, base::JSON_PARSE_RFC)), | 48 : fetch_reply_(base::JSONReader::Read(json_reply, base::JSON_PARSE_RFC)), |
49 fetch_request_(fetch_request) { | 49 fetch_request_(fetch_request) { |
50 CHECK(fetch_reply_) << "Invalid json: " << json_reply; | 50 CHECK(fetch_reply_) << "Invalid json: " << json_reply; |
51 } | 51 } |
52 | 52 |
53 ~MockFetcher() override {} | 53 ~MockFetcher() override {} |
54 | 54 |
55 void StartFetch(const GURL& url, | 55 void StartFetch(const GURL& url, |
| 56 const std::string& method, |
56 const net::HttpRequestHeaders& request_headers, | 57 const net::HttpRequestHeaders& request_headers, |
57 ResultListener* result_listener) override { | 58 ResultListener* result_listener) override { |
58 // Record the request. | 59 // Record the request. |
59 fetch_request_->SetString("url", url.spec()); | 60 fetch_request_->SetString("url", url.spec()); |
| 61 fetch_request_->SetString("method", method); |
60 std::unique_ptr<base::DictionaryValue> headers(new base::DictionaryValue); | 62 std::unique_ptr<base::DictionaryValue> headers(new base::DictionaryValue); |
61 for (net::HttpRequestHeaders::Iterator it(request_headers); it.GetNext();) { | 63 for (net::HttpRequestHeaders::Iterator it(request_headers); it.GetNext();) { |
62 headers->SetString(it.name(), it.value()); | 64 headers->SetString(it.name(), it.value()); |
63 } | 65 } |
64 fetch_request_->Set("headers", std::move(headers)); | 66 fetch_request_->Set("headers", std::move(headers)); |
65 | 67 |
66 // Return the canned response. | 68 // Return the canned response. |
67 base::DictionaryValue* reply_dictionary; | 69 base::DictionaryValue* reply_dictionary; |
68 ASSERT_TRUE(fetch_reply_->GetAsDictionary(&reply_dictionary)); | 70 ASSERT_TRUE(fetch_reply_->GetAsDictionary(&reply_dictionary)); |
69 std::string final_url; | 71 std::string final_url; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 GURL("https://example.com"), net::DEFAULT_PRIORITY, &request_delegate_)); | 178 GURL("https://example.com"), net::DEFAULT_PRIORITY, &request_delegate_)); |
177 request->SetReferrer("https://referrer.example.com"); | 179 request->SetReferrer("https://referrer.example.com"); |
178 request->SetExtraRequestHeaderByName("Extra-Header", "Value", true); | 180 request->SetExtraRequestHeaderByName("Extra-Header", "Value", true); |
179 request->SetExtraRequestHeaderByName("User-Agent", "TestBrowser", true); | 181 request->SetExtraRequestHeaderByName("User-Agent", "TestBrowser", true); |
180 request->SetExtraRequestHeaderByName("Accept", "text/plain", true); | 182 request->SetExtraRequestHeaderByName("Accept", "text/plain", true); |
181 request->Start(); | 183 request->Start(); |
182 base::RunLoop().RunUntilIdle(); | 184 base::RunLoop().RunUntilIdle(); |
183 | 185 |
184 std::string expected_request_json = | 186 std::string expected_request_json = |
185 "{\"url\": \"https://example.com/\"," | 187 "{\"url\": \"https://example.com/\"," |
| 188 " \"method\": \"GET\"," |
186 " \"headers\": {" | 189 " \"headers\": {" |
187 " \"Accept\": \"text/plain\"," | 190 " \"Accept\": \"text/plain\"," |
188 " \"Cookie\": \"\"," | 191 " \"Cookie\": \"\"," |
189 " \"Extra-Header\": \"Value\"," | 192 " \"Extra-Header\": \"Value\"," |
190 " \"Referer\": \"https://referrer.example.com/\"," | 193 " \"Referer\": \"https://referrer.example.com/\"," |
191 " \"User-Agent\": \"TestBrowser\"" | 194 " \"User-Agent\": \"TestBrowser\"" |
192 " }" | 195 " }" |
193 "}"; | 196 "}"; |
194 | 197 |
195 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); | 198 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 "{\"url\":\"https://example.com\"," | 330 "{\"url\":\"https://example.com\"," |
328 " \"http_response_code\":200," | 331 " \"http_response_code\":200," |
329 " \"data\":\"Reply\"," | 332 " \"data\":\"Reply\"," |
330 " \"headers\":{\"Content-Type\":\"text/html; charset=UTF-8\"}}"; | 333 " \"headers\":{\"Content-Type\":\"text/html; charset=UTF-8\"}}"; |
331 | 334 |
332 std::unique_ptr<net::URLRequest> request( | 335 std::unique_ptr<net::URLRequest> request( |
333 CreateAndCompleteJob(GURL("https://example.com"), reply)); | 336 CreateAndCompleteJob(GURL("https://example.com"), reply)); |
334 | 337 |
335 std::string expected_request_json = | 338 std::string expected_request_json = |
336 "{\"url\": \"https://example.com/\"," | 339 "{\"url\": \"https://example.com/\"," |
| 340 " \"method\": \"GET\"," |
337 " \"headers\": {" | 341 " \"headers\": {" |
338 " \"Cookie\": \"basic_cookie=1; secure_cookie=2; http_only_cookie=3\"," | 342 " \"Cookie\": \"basic_cookie=1; secure_cookie=2; http_only_cookie=3\"," |
339 " \"Referer\": \"\"" | 343 " \"Referer\": \"\"" |
340 " }" | 344 " }" |
341 "}"; | 345 "}"; |
342 | 346 |
343 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); | 347 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); |
344 } | 348 } |
345 | 349 |
346 TEST_F(GenericURLRequestJobTest, DelegateBlocksLoading) { | 350 TEST_F(GenericURLRequestJobTest, DelegateBlocksLoading) { |
347 std::string reply = | 351 std::string reply = |
348 "{\"url\":\"https://example.com\"," | 352 "{\"url\":\"https://example.com\"," |
349 " \"http_response_code\":200," | 353 " \"http_response_code\":200," |
350 " \"data\":\"Reply\"," | 354 " \"data\":\"Reply\"," |
351 " \"headers\":{\"Content-Type\":\"text/html; charset=UTF-8\"}}"; | 355 " \"headers\":{\"Content-Type\":\"text/html; charset=UTF-8\"}}"; |
352 | 356 |
353 job_delegate_.SetShouldBlock(true); | 357 job_delegate_.SetShouldBlock(true); |
354 | 358 |
355 std::unique_ptr<net::URLRequest> request( | 359 std::unique_ptr<net::URLRequest> request( |
356 CreateAndCompleteJob(GURL("https://example.com"), reply)); | 360 CreateAndCompleteJob(GURL("https://example.com"), reply)); |
357 | 361 |
358 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 362 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
359 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()); | 363 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()); |
360 } | 364 } |
361 | 365 |
362 } // namespace headless | 366 } // namespace headless |
OLD | NEW |