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

Unified Diff: components/link_header_util/link_header_util_unittest.cc

Issue 1811163002: Share link header parsing code between blink and content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@base-optional
Patch Set: Created 4 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: components/link_header_util/link_header_util_unittest.cc
diff --git a/content/browser/service_worker/link_header_support_unittest.cc b/components/link_header_util/link_header_util_unittest.cc
similarity index 50%
copy from content/browser/service_worker/link_header_support_unittest.cc
copy to components/link_header_util/link_header_util_unittest.cc
index ae293fe720e7cb20056db06d13118ccba05bcb5b..adc3f6eb091a6bfd900a5b184cdf5064acf4fb4f 100644
--- a/content/browser/service_worker/link_header_support_unittest.cc
+++ b/components/link_header_util/link_header_util_unittest.cc
@@ -2,27 +2,31 @@
// 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/link_header_support.h"
+#include "components/link_header_util/link_header_util.h"
-#include "base/command_line.h"
#include "base/logging.h"
#include "base/run_loop.h"
-#include "content/browser/service_worker/embedded_worker_test_helper.h"
-#include "content/browser/service_worker/service_worker_context_wrapper.h"
-#include "content/browser/service_worker/service_worker_registration.h"
-#include "content/public/browser/resource_request_info.h"
-#include "content/public/common/content_switches.h"
-#include "content/public/test/mock_resource_context.h"
-#include "content/public/test/test_browser_thread_bundle.h"
-#include "net/http/http_response_headers.h"
-#include "net/url_request/url_request_test_job.h"
-#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
-namespace content {
+namespace link_header_util {
namespace {
+void SplitLinkHeaderForTesting(const std::string& header,
+ std::vector<std::string>* values) {
+ std::vector<StringIteratorPair> values_iterators = SplitLinkHeader(header);
+ values->clear();
+ for (const auto& pair : values_iterators)
+ values->push_back(std::string(pair.first, pair.second));
+}
+
+bool ParseLinkHeaderValueForTesting(
+ std::string value,
+ std::string* url,
+ std::unordered_map<std::string, base::Optional<std::string>>* params) {
+ return ParseLinkHeaderValue(value.begin(), value.end(), url, params);
+}
+
TEST(LinkHeaderTest, SplitEmpty) {
std::vector<std::string> values;
SplitLinkHeaderForTesting("", &values);
@@ -93,13 +97,13 @@ TEST_P(SimpleParseTest, Simple) {
const SimpleParseTestData test = GetParam();
std::string url;
- std::unordered_map<std::string, std::string> params;
+ std::unordered_map<std::string, base::Optional<std::string>> params;
EXPECT_EQ(test.valid,
ParseLinkHeaderValueForTesting(test.link, &url, &params));
if (test.valid) {
EXPECT_EQ(test.url, url);
- EXPECT_EQ(test.rel, params["rel"]);
- EXPECT_EQ(test.as, params["as"]);
+ EXPECT_EQ(test.rel, params["rel"].value_or(""));
+ EXPECT_EQ(test.as, params["as"].value_or(""));
}
}
@@ -218,193 +222,6 @@ INSTANTIATE_TEST_CASE_P(LinkHeaderTest,
SimpleParseTest,
testing::ValuesIn(simple_parse_tests));
-void SaveFoundRegistrationsCallback(
- ServiceWorkerStatusCode expected_status,
- bool* called,
- std::vector<ServiceWorkerRegistrationInfo>* registrations,
- ServiceWorkerStatusCode status,
- const std::vector<ServiceWorkerRegistrationInfo>& result) {
- EXPECT_EQ(expected_status, status);
- *called = true;
- *registrations = result;
-}
-
-ServiceWorkerContextWrapper::GetRegistrationsInfosCallback
-SaveFoundRegistrations(
- ServiceWorkerStatusCode expected_status,
- bool* called,
- std::vector<ServiceWorkerRegistrationInfo>* registrations) {
- *called = false;
- return base::Bind(&SaveFoundRegistrationsCallback, expected_status, called,
- registrations);
-}
-
-class LinkHeaderServiceWorkerTest : public ::testing::Test {
- public:
- LinkHeaderServiceWorkerTest()
- : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
- resource_context_(&request_context_) {
- base::CommandLine::ForCurrentProcess()->AppendSwitch(
- switches::kEnableExperimentalWebPlatformFeatures);
- }
- ~LinkHeaderServiceWorkerTest() override {}
-
- void SetUp() override {
- helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
- }
-
- void TearDown() override { helper_.reset(); }
-
- ServiceWorkerContextWrapper* context_wrapper() {
- return helper_->context_wrapper();
- }
-
- void ProcessLinkHeader(const GURL& request_url,
- const std::string& link_header) {
- scoped_ptr<net::URLRequest> request = request_context_.CreateRequest(
- request_url, net::DEFAULT_PRIORITY, &request_delegate_);
- ResourceRequestInfo::AllocateForTesting(
- request.get(), RESOURCE_TYPE_SCRIPT, &resource_context_,
- -1 /* render_process_id */, -1 /* render_view_id */,
- -1 /* render_frame_id */, false /* is_main_frame */,
- false /* parent_is_main_frame */, true /* allow_download */,
- true /* is_async */, false /* is_using_lofi */);
-
- ProcessLinkHeaderForRequest(request.get(), link_header, context_wrapper());
- base::RunLoop().RunUntilIdle();
- }
-
- std::vector<ServiceWorkerRegistrationInfo> GetRegistrations() {
- bool called;
- std::vector<ServiceWorkerRegistrationInfo> registrations;
- context_wrapper()->GetAllRegistrations(
- SaveFoundRegistrations(SERVICE_WORKER_OK, &called, &registrations));
- base::RunLoop().RunUntilIdle();
- EXPECT_TRUE(called);
- return registrations;
- }
-
- private:
- TestBrowserThreadBundle thread_bundle_;
- scoped_ptr<EmbeddedWorkerTestHelper> helper_;
- net::TestURLRequestContext request_context_;
- net::TestDelegate request_delegate_;
- MockResourceContext resource_context_;
-};
-
-TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_Basic) {
- ProcessLinkHeader(GURL("https://example.com/foo/bar/"),
- "<../foo.js>; rel=serviceworker");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(1u, registrations.size());
- EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern);
- EXPECT_EQ(GURL("https://example.com/foo/foo.js"),
- registrations[0].active_version.script_url);
-}
-
-TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeWithFragment) {
- ProcessLinkHeader(GURL("https://example.com/foo/bar/"),
- "<../bar.js>; rel=serviceworker; scope=\"scope#ref\"");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(1u, registrations.size());
- EXPECT_EQ(GURL("https://example.com/foo/bar/scope"),
- registrations[0].pattern);
- EXPECT_EQ(GURL("https://example.com/foo/bar.js"),
- registrations[0].active_version.script_url);
-}
-
-TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeAbsoluteUrl) {
- ProcessLinkHeader(GURL("https://example.com/foo/bar/"),
- "<bar.js>; rel=serviceworker; "
- "scope=\"https://example.com:443/foo/bar/scope\"");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(1u, registrations.size());
- EXPECT_EQ(GURL("https://example.com/foo/bar/scope"),
- registrations[0].pattern);
- EXPECT_EQ(GURL("https://example.com/foo/bar/bar.js"),
- registrations[0].active_version.script_url);
-}
-
-TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeDifferentOrigin) {
- ProcessLinkHeader(
- GURL("https://example.com/foobar/"),
- "<bar.js>; rel=serviceworker; scope=\"https://google.com/scope\"");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(0u, registrations.size());
-}
-
-TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeUrlEncodedSlash) {
- ProcessLinkHeader(GURL("https://example.com/foobar/"),
- "<bar.js>; rel=serviceworker; scope=\"./foo%2Fbar\"");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(0u, registrations.size());
-}
-
-TEST_F(LinkHeaderServiceWorkerTest,
- InstallServiceWorker_ScriptUrlEncodedSlash) {
- ProcessLinkHeader(GURL("https://example.com/foobar/"),
- "<foo%2Fbar.js>; rel=serviceworker");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(0u, registrations.size());
-}
-
-TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScriptAbsoluteUrl) {
- ProcessLinkHeader(
- GURL("https://example.com/foobar/"),
- "<https://example.com/bar.js>; rel=serviceworker; scope=foo");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(1u, registrations.size());
- EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern);
- EXPECT_EQ(GURL("https://example.com/bar.js"),
- registrations[0].active_version.script_url);
-}
-
-TEST_F(LinkHeaderServiceWorkerTest,
- InstallServiceWorker_ScriptDifferentOrigin) {
- ProcessLinkHeader(
- GURL("https://example.com/foobar/"),
- "<https://google.com/bar.js>; rel=serviceworker; scope=foo");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(0u, registrations.size());
-}
-
-TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_MultipleWorkers) {
- ProcessLinkHeader(GURL("https://example.com/foobar/"),
- "<bar.js>; rel=serviceworker; scope=foo, <baz.js>; "
- "rel=serviceworker; scope=scope");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(2u, registrations.size());
- EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern);
- EXPECT_EQ(GURL("https://example.com/foobar/bar.js"),
- registrations[0].active_version.script_url);
- EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[1].pattern);
- EXPECT_EQ(GURL("https://example.com/foobar/baz.js"),
- registrations[1].active_version.script_url);
-}
-
-TEST_F(LinkHeaderServiceWorkerTest,
- InstallServiceWorker_ValidAndInvalidValues) {
- ProcessLinkHeader(
- GURL("https://example.com/foobar/"),
- "<https://google.com/bar.js>; rel=serviceworker; scope=foo, <baz.js>; "
- "rel=serviceworker; scope=scope");
-
- std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
- ASSERT_EQ(1u, registrations.size());
- EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[0].pattern);
- EXPECT_EQ(GURL("https://example.com/foobar/baz.js"),
- registrations[0].active_version.script_url);
-}
-
} // namespace
-} // namespace content
+} // namespace link_header_util

Powered by Google App Engine
This is Rietveld 408576698