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
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111

content / browser / service_worker / service_worker_main_resource_loader_interceptor_unittest.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/service_worker/service_worker_main_resource_loader_interceptor.h"

#include "build/chromeos_buildflags.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace content {

class ServiceWorkerMainResourceLoaderInterceptorTest : public testing::Test {
 public:
  ServiceWorkerMainResourceLoaderInterceptorTest() = default;
  ServiceWorkerMainResourceLoaderInterceptorTest(
      const ServiceWorkerMainResourceLoaderInterceptorTest&) = delete;
  ServiceWorkerMainResourceLoaderInterceptorTest& operator=(
      const ServiceWorkerMainResourceLoaderInterceptorTest&) = delete;
  ~ServiceWorkerMainResourceLoaderInterceptorTest() override = default;

  void SetUp() override {
    browser_context_ = std::make_unique<TestBrowserContext>();
  }

  bool ShouldCreateForNavigation(
      const GURL& url,
      network::mojom::RequestDestination request_destination) {
    return ServiceWorkerMainResourceLoaderInterceptor::
        ShouldCreateForNavigation(url, request_destination,
                                  browser_context_.get());
  }

 private:
  BrowserTaskEnvironment task_environment_{BrowserTaskEnvironment::IO_MAINLOOP};
  std::unique_ptr<TestBrowserContext> browser_context_;
};

TEST_F(ServiceWorkerMainResourceLoaderInterceptorTest,
       ShouldCreateForNavigation_HTTP) {
  EXPECT_TRUE(
      ShouldCreateForNavigation(GURL("http://host/scope/doc"),
                                network::mojom::RequestDestination::kDocument));
  EXPECT_FALSE(
      ShouldCreateForNavigation(GURL("http://host/scope/doc"),
                                network::mojom::RequestDestination::kEmbed));
  EXPECT_FALSE(
      ShouldCreateForNavigation(GURL("http://host/scope/doc"),
                                network::mojom::RequestDestination::kObject));
  EXPECT_TRUE(ShouldCreateForNavigation(
      GURL("http://host/scope/doc"),
      network::mojom::RequestDestination::kFencedframe));
}

TEST_F(ServiceWorkerMainResourceLoaderInterceptorTest,
       ShouldCreateForNavigation_HTTPS) {
  EXPECT_TRUE(
      ShouldCreateForNavigation(GURL("https://host/scope/doc"),
                                network::mojom::RequestDestination::kDocument));
  EXPECT_FALSE(
      ShouldCreateForNavigation(GURL("https://host/scope/doc"),
                                network::mojom::RequestDestination::kEmbed));
  EXPECT_FALSE(
      ShouldCreateForNavigation(GURL("https://host/scope/doc"),
                                network::mojom::RequestDestination::kObject));
  EXPECT_TRUE(ShouldCreateForNavigation(
      GURL("https://host/scope/doc"),
      network::mojom::RequestDestination::kFencedframe));
}

TEST_F(ServiceWorkerMainResourceLoaderInterceptorTest,
       ShouldCreateForNavigation_FTP) {
  EXPECT_FALSE(
      ShouldCreateForNavigation(GURL("ftp://host/scope/doc"),
                                network::mojom::RequestDestination::kDocument));
  EXPECT_FALSE(
      ShouldCreateForNavigation(GURL("ftp://host/scope/doc"),
                                network::mojom::RequestDestination::kEmbed));
  EXPECT_FALSE(
      ShouldCreateForNavigation(GURL("ftp://host/scope/doc"),
                                network::mojom::RequestDestination::kObject));
  EXPECT_FALSE(ShouldCreateForNavigation(
      GURL("ftp://host/scope/doc"),
      network::mojom::RequestDestination::kFencedframe));
}

TEST_F(ServiceWorkerMainResourceLoaderInterceptorTest,
       ShouldCreateForNavigation_ExternalFileScheme) {
  bool expected_handler_created = false;
#if BUILDFLAG(IS_CHROMEOS_ASH)
  expected_handler_created = true;
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
  EXPECT_EQ(
      expected_handler_created,
      ShouldCreateForNavigation(GURL("externalfile:drive/doc"),
                                network::mojom::RequestDestination::kDocument));
  EXPECT_FALSE(
      ShouldCreateForNavigation(GURL("externalfile:drive/doc"),
                                network::mojom::RequestDestination::kEmbed));
  EXPECT_FALSE(
      ShouldCreateForNavigation(GURL("externalfile:drive/doc"),
                                network::mojom::RequestDestination::kObject));
  EXPECT_EQ(expected_handler_created,
            ShouldCreateForNavigation(
                GURL("externalfile:drive/doc"),
                network::mojom::RequestDestination::kFencedframe));
}

}  // namespace content