summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/fetch/fetch_request_data_test.cc
blob: 78efa87e618dddc25ee069a8ca93bd4180bb8157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/fetch/fetch_request_data.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink.h"
#include "third_party/blink/renderer/core/fetch/fetch_header_list.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"

namespace blink {

namespace {

mojom::blink::FetchAPIRequestPtr PrepareFetchAPIRequest() {
  auto request = mojom::blink::FetchAPIRequest::New();
  request->url = KURL("https://example.com");
  // "sec-fetch-" will be excluded forcibly for service worker fetch events.
  request->headers.insert(String("sec-fetch-xx"), String("xxx"));
  request->headers.insert(String("sec-fetch-yy"), String("xxx"));
  // "x-bye-bye" will be excluded by FetchRequestDataTestingPlatformSupport for
  // service worker fetch events.
  request->headers.insert(String("x-bye-bye"), String("xxx"));
  // "x-hi-hi" will be kept in any case.
  request->headers.insert(String("x-hi-hi"), String("xxx"));
  return request;
}

}  // namespace

class FetchRequestDataTestingPlatformSupport : public TestingPlatformSupport {
 public:
  bool IsExcludedHeaderForServiceWorkerFetchEvent(
      const WebString& header_name) override {
    return header_name == "x-bye-bye";
  }
};

TEST(FetchRequestDataTest, For_ServiceWorkerFetchEvent_Headers) {
  {
    FetchRequestData* request_data = FetchRequestData::Create(
        nullptr /* script_state */, PrepareFetchAPIRequest(),
        FetchRequestData::ForServiceWorkerFetchEvent::kTrue);
    EXPECT_EQ(2U, request_data->HeaderList()->size());
    EXPECT_TRUE(request_data->HeaderList()->Has("x-hi-hi"));
    EXPECT_TRUE(request_data->HeaderList()->Has("x-bye-bye"));
    EXPECT_FALSE(request_data->HeaderList()->Has("sec-fetch-xx"));
    EXPECT_FALSE(request_data->HeaderList()->Has("sec-fetch-yy"));
  }

  {
    ScopedTestingPlatformSupport<FetchRequestDataTestingPlatformSupport>
        platform;

    FetchRequestData* request_data = FetchRequestData::Create(
        nullptr /* script_state */, PrepareFetchAPIRequest(),
        FetchRequestData::ForServiceWorkerFetchEvent::kTrue);
    EXPECT_EQ(1U, request_data->HeaderList()->size());
    EXPECT_TRUE(request_data->HeaderList()->Has("x-hi-hi"));
    EXPECT_FALSE(request_data->HeaderList()->Has("x-bye-bye"));
    EXPECT_FALSE(request_data->HeaderList()->Has("sec-fetch-xx"));
    EXPECT_FALSE(request_data->HeaderList()->Has("sec-fetch-yy"));
  }
}

TEST(FetchRequestDataTest, Not_For_ServiceWorkerFetchEvent_Headers) {
  {
    FetchRequestData* request_data = FetchRequestData::Create(
        nullptr /* script_state */, PrepareFetchAPIRequest(),
        FetchRequestData::ForServiceWorkerFetchEvent::kFalse);
    EXPECT_EQ(4U, request_data->HeaderList()->size());
    EXPECT_TRUE(request_data->HeaderList()->Has("x-hi-hi"));
    EXPECT_TRUE(request_data->HeaderList()->Has("x-bye-bye"));
    EXPECT_TRUE(request_data->HeaderList()->Has("sec-fetch-xx"));
    EXPECT_TRUE(request_data->HeaderList()->Has("sec-fetch-yy"));
  }

  {
    ScopedTestingPlatformSupport<FetchRequestDataTestingPlatformSupport>
        platform;

    FetchRequestData* request_data = FetchRequestData::Create(
        nullptr /* script_state */, PrepareFetchAPIRequest(),
        FetchRequestData::ForServiceWorkerFetchEvent::kFalse);
    EXPECT_EQ(4U, request_data->HeaderList()->size());
    EXPECT_TRUE(request_data->HeaderList()->Has("x-hi-hi"));
    EXPECT_TRUE(request_data->HeaderList()->Has("x-bye-bye"));
    EXPECT_TRUE(request_data->HeaderList()->Has("sec-fetch-xx"));
    EXPECT_TRUE(request_data->HeaderList()->Has("sec-fetch-yy"));
  }
}

}  // namespace blink