forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
URLPattern: Create helper for accepting values from script.
This allows a string, dictionary or URLPattern wrapper to be coerced to URLPattern, so that code further in the function need only deal with complete patterns. Bug: 1504683 Change-Id: Ie7a6f9e4e92fd81f6eeba84403bebf2615af619c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5053645 Commit-Queue: Jeremy Roman <jbroman@chromium.org> Reviewed-by: Shunya Shishido <sisidovski@chromium.org> Cr-Commit-Position: refs/heads/main@{#1228890}
- Loading branch information
1 parent
682a574
commit a72861d
Showing
6 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
third_party/blink/renderer/core/url_pattern/url_pattern_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include "third_party/blink/renderer/core/url_pattern/url_pattern.h" | ||
|
||
#include "testing/gtest/include/gtest/gtest.h" | ||
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h" | ||
#include "third_party/blink/renderer/bindings/core/v8/v8_typedefs.h" | ||
#include "third_party/blink/renderer/bindings/core/v8/v8_union_urlpattern_urlpatterninit_usvstring.h" | ||
#include "third_party/blink/renderer/bindings/core/v8/v8_url_pattern.h" | ||
#include "third_party/blink/renderer/platform/bindings/exception_state.h" | ||
#include "third_party/blink/renderer/platform/bindings/v8_binding.h" | ||
#include "third_party/blink/renderer/platform/bindings/wrapper_type_info.h" | ||
|
||
namespace blink { | ||
|
||
namespace { | ||
|
||
v8::Local<v8::Value> Eval(V8TestingScope& scope, const char* source) { | ||
v8::Local<v8::Script> script = | ||
v8::Script::Compile(scope.GetContext(), | ||
V8String(scope.GetIsolate(), source)) | ||
.ToLocalChecked(); | ||
return script->Run(scope.GetContext()).ToLocalChecked(); | ||
} | ||
|
||
} // namespace | ||
|
||
TEST(URLPatternTest, CompatibleFromString) { | ||
KURL base_url("https://urlpattern.example/foo/bar"); | ||
V8TestingScope scope(base_url); | ||
v8::Local<v8::String> pattern_string = | ||
V8String(scope.GetIsolate(), "baz/:quux"); | ||
auto* compatible = V8URLPatternCompatible::Create( | ||
scope.GetIsolate(), pattern_string, ASSERT_NO_EXCEPTION); | ||
auto* url_pattern = | ||
URLPattern::From(compatible, base_url, ASSERT_NO_EXCEPTION); | ||
EXPECT_EQ(url_pattern->protocol(), "https"); | ||
EXPECT_EQ(url_pattern->hostname(), "urlpattern.example"); | ||
EXPECT_EQ(url_pattern->pathname(), "/foo/baz/:quux"); | ||
} | ||
|
||
TEST(URLPatternTest, CompatibleFromStringInvalid) { | ||
KURL base_url("https://urlpattern.example/foo/bar"); | ||
V8TestingScope scope(base_url); | ||
v8::Local<v8::String> pattern_string = V8String(scope.GetIsolate(), "{"); | ||
auto* compatible = V8URLPatternCompatible::Create( | ||
scope.GetIsolate(), pattern_string, ASSERT_NO_EXCEPTION); | ||
DummyExceptionStateForTesting exception_state; | ||
EXPECT_FALSE(URLPattern::From(compatible, base_url, exception_state)); | ||
EXPECT_TRUE(exception_state.HadException()); | ||
} | ||
|
||
TEST(URLPatternTest, CompatibleFromInit) { | ||
KURL base_url("https://urlpattern.example/foo/bar"); | ||
V8TestingScope scope(base_url); | ||
v8::Local<v8::Value> init = Eval(scope, "({search: 'a=42'})"); | ||
ASSERT_TRUE(init->IsObject()); | ||
auto* compatible = V8URLPatternCompatible::Create(scope.GetIsolate(), init, | ||
ASSERT_NO_EXCEPTION); | ||
auto* url_pattern = | ||
URLPattern::From(compatible, base_url, ASSERT_NO_EXCEPTION); | ||
EXPECT_EQ(url_pattern->protocol(), "https"); | ||
EXPECT_EQ(url_pattern->hostname(), "urlpattern.example"); | ||
EXPECT_EQ(url_pattern->pathname(), "/foo/bar"); | ||
EXPECT_EQ(url_pattern->search(), "a=42"); | ||
} | ||
|
||
TEST(URLPatternTest, CompatibleFromInitWithBaseURL) { | ||
KURL base_url("https://urlpattern.example/foo/bar"); | ||
V8TestingScope scope(base_url); | ||
v8::Local<v8::Value> init = | ||
Eval(scope, "({search: 'a=42', baseURL: 'https://alt.example/'})"); | ||
ASSERT_TRUE(init->IsObject()); | ||
auto* compatible = V8URLPatternCompatible::Create(scope.GetIsolate(), init, | ||
ASSERT_NO_EXCEPTION); | ||
auto* url_pattern = | ||
URLPattern::From(compatible, base_url, ASSERT_NO_EXCEPTION); | ||
EXPECT_EQ(url_pattern->protocol(), "https"); | ||
EXPECT_EQ(url_pattern->hostname(), "alt.example"); | ||
EXPECT_EQ(url_pattern->pathname(), "/"); | ||
EXPECT_EQ(url_pattern->search(), "a=42"); | ||
} | ||
|
||
TEST(URLPatternTest, CompatibleFromInitInvalid) { | ||
KURL base_url("https://urlpattern.example/foo/bar"); | ||
V8TestingScope scope(base_url); | ||
v8::Local<v8::Value> init = Eval(scope, "({hash: '{'})"); | ||
ASSERT_TRUE(init->IsObject()); | ||
auto* compatible = V8URLPatternCompatible::Create(scope.GetIsolate(), init, | ||
ASSERT_NO_EXCEPTION); | ||
DummyExceptionStateForTesting exception_state; | ||
EXPECT_FALSE(URLPattern::From(compatible, base_url, exception_state)); | ||
EXPECT_TRUE(exception_state.HadException()); | ||
} | ||
|
||
TEST(URLPatternTest, CompatibleFromURLPattern) { | ||
KURL base_url("https://urlpattern.example/foo/bar"); | ||
V8TestingScope scope(base_url); | ||
v8::Local<v8::Value> wrapper = | ||
Eval(scope, "new URLPattern({protocol: 'https'})"); | ||
ASSERT_TRUE(V8URLPattern::HasInstance(scope.GetIsolate(), wrapper)); | ||
auto* compatible = V8URLPatternCompatible::Create(scope.GetIsolate(), wrapper, | ||
ASSERT_NO_EXCEPTION); | ||
auto* url_pattern = | ||
URLPattern::From(compatible, base_url, ASSERT_NO_EXCEPTION); | ||
EXPECT_EQ(url_pattern, ToScriptWrappable(wrapper.As<v8::Object>())); | ||
} | ||
|
||
} // namespace blink |