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

Side by Side Diff: content/browser/background_fetch/background_fetch_service_unittest.cc

Issue 2814683003: Pass through the response code and headers in Background Fetch (Closed)
Patch Set: Created 3 years, 8 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 2017 The Chromium Authors. All rights reserved. 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 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 <map> 5 #include <map>
6 #include <memory> 6 #include <memory>
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 BackgroundFetchRegistrationId registration_id; 323 BackgroundFetchRegistrationId registration_id;
324 ASSERT_TRUE(CreateRegistrationId(kExampleTag, &registration_id)); 324 ASSERT_TRUE(CreateRegistrationId(kExampleTag, &registration_id));
325 325
326 // base::RunLoop that we'll run until the event has been dispatched. If this 326 // base::RunLoop that we'll run until the event has been dispatched. If this
327 // test times out, it means that the event could not be dispatched. 327 // test times out, it means that the event could not be dispatched.
328 base::RunLoop event_dispatched_loop; 328 base::RunLoop event_dispatched_loop;
329 embedded_worker_test_helper()->set_fetched_event_closure( 329 embedded_worker_test_helper()->set_fetched_event_closure(
330 event_dispatched_loop.QuitClosure()); 330 event_dispatched_loop.QuitClosure());
331 331
332 std::vector<ServiceWorkerFetchRequest> requests; 332 std::vector<ServiceWorkerFetchRequest> requests;
333
334 constexpr int kFirstResponseCode = 200;
335 constexpr int kSecondResponseCode = 201;
336
333 requests.push_back(CreateRequestWithProvidedResponse( 337 requests.push_back(CreateRequestWithProvidedResponse(
334 "GET", "https://example.com/funny_cat.txt", 200 /* status_code */, 338 "GET", "https://example.com/funny_cat.txt",
335 "This text describes a scenario involving a funny cat.")); 339 TestResponseBuilder(kFirstResponseCode)
340 .SetResponseData(
341 "This text describes a scenario involving a funny cat.")
342 .AddResponseHeader("Content-Type", "text/plain")
343 .AddResponseHeader("X-Cat", "yes")
344 .Build()));
345
336 requests.push_back(CreateRequestWithProvidedResponse( 346 requests.push_back(CreateRequestWithProvidedResponse(
337 "GET", "https://example.com/crazy_cat.txt", 200 /* status_code */, 347 "GET", "https://example.com/crazy_cat.txt",
338 "This text describes another scenario that involves a crazy cat.")); 348 TestResponseBuilder(kSecondResponseCode)
349 .SetResponseData(
350 "This text describes another scenario that involves a crazy cat.")
351 .AddResponseHeader("Content-Type", "text/plain")
352 .Build()));
339 353
340 // Create the registration with the given |requests|. 354 // Create the registration with the given |requests|.
341 { 355 {
342 BackgroundFetchOptions options; 356 BackgroundFetchOptions options;
343 357
344 blink::mojom::BackgroundFetchError error; 358 blink::mojom::BackgroundFetchError error;
345 BackgroundFetchRegistration registration; 359 BackgroundFetchRegistration registration;
346 360
347 // Create the first registration. This must succeed. 361 // Create the first registration. This must succeed.
348 ASSERT_NO_FATAL_FAILURE( 362 ASSERT_NO_FATAL_FAILURE(
(...skipping 10 matching lines...) Expand all
359 ASSERT_TRUE(embedded_worker_test_helper()->last_fetches().has_value()); 373 ASSERT_TRUE(embedded_worker_test_helper()->last_fetches().has_value());
360 374
361 std::vector<BackgroundFetchSettledFetch> fetches = 375 std::vector<BackgroundFetchSettledFetch> fetches =
362 embedded_worker_test_helper()->last_fetches().value(); 376 embedded_worker_test_helper()->last_fetches().value();
363 ASSERT_EQ(fetches.size(), requests.size()); 377 ASSERT_EQ(fetches.size(), requests.size());
364 378
365 for (size_t i = 0; i < fetches.size(); ++i) { 379 for (size_t i = 0; i < fetches.size(); ++i) {
366 ASSERT_EQ(fetches[i].request.url, requests[i].url); 380 ASSERT_EQ(fetches[i].request.url, requests[i].url);
367 EXPECT_EQ(fetches[i].request.method, requests[i].method); 381 EXPECT_EQ(fetches[i].request.method, requests[i].method);
368 382
369 EXPECT_EQ(fetches[i].request.url, fetches[i].response.url_list[0]); 383 EXPECT_EQ(fetches[i].response.url_list[0], fetches[i].request.url);
384 EXPECT_EQ(fetches[i].response.response_type,
385 blink::kWebServiceWorkerResponseTypeDefault);
386
387 switch (i) {
388 case 0:
389 EXPECT_EQ(fetches[i].response.status_code, kFirstResponseCode);
390 EXPECT_EQ(fetches[i].response.headers.count("Content-Type"), 1u);
391 EXPECT_EQ(fetches[i].response.headers.count("X-Cat"), 1u);
392 break;
393 case 1:
394 EXPECT_EQ(fetches[i].response.status_code, kSecondResponseCode);
395 EXPECT_EQ(fetches[i].response.headers.count("Content-Type"), 1u);
396 EXPECT_EQ(fetches[i].response.headers.count("X-Cat"), 0u);
397 break;
398 default:
399 NOTREACHED();
400 }
370 401
371 // TODO(peter): change-detector tests for unsupported properties. 402 // TODO(peter): change-detector tests for unsupported properties.
372 EXPECT_EQ(fetches[i].response.status_code, 200);
373 EXPECT_TRUE(fetches[i].response.status_text.empty());
374 EXPECT_EQ(fetches[i].response.response_type,
375 blink::kWebServiceWorkerResponseTypeDefault);
376 EXPECT_FALSE(fetches[i].response.headers.empty());
377 EXPECT_EQ(fetches[i].response.error, 403 EXPECT_EQ(fetches[i].response.error,
378 blink::kWebServiceWorkerResponseErrorUnknown); 404 blink::kWebServiceWorkerResponseErrorUnknown);
379 405
380 // Verify that all properties have a sensible value. 406 // Verify that all properties have a sensible value.
381 EXPECT_FALSE(fetches[i].response.response_time.is_null()); 407 EXPECT_FALSE(fetches[i].response.response_time.is_null());
382 408
383 // Verify that the response blobs have been populated. We cannot consume 409 // Verify that the response blobs have been populated. We cannot consume
384 // their data here since the handles have already been released. 410 // their data here since the handles have already been released.
385 ASSERT_FALSE(fetches[i].response.blob_uuid.empty()); 411 ASSERT_FALSE(fetches[i].response.blob_uuid.empty());
386 ASSERT_GT(fetches[i].response.blob_size, 0u); 412 ASSERT_GT(fetches[i].response.blob_size, 0u);
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 const bool has_alternative_tag = 551 const bool has_alternative_tag =
526 tags[0] == kAlternativeTag || tags[1] == kAlternativeTag; 552 tags[0] == kAlternativeTag || tags[1] == kAlternativeTag;
527 553
528 EXPECT_TRUE(has_example_tag); 554 EXPECT_TRUE(has_example_tag);
529 EXPECT_TRUE(has_alternative_tag); 555 EXPECT_TRUE(has_alternative_tag);
530 } 556 }
531 } 557 }
532 558
533 } // namespace 559 } // namespace
534 } // namespace content 560 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698