Skip to content

Commit dbb0532

Browse files
committed
src,test: expand test coverage for urlpattern and fix error
1 parent 2bd5694 commit dbb0532

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/node_url_pattern.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ URLPatternRegexProvider::regex_search(std::string_view input,
103103
return std::nullopt;
104104
}
105105

106+
// V8 checks that the regexp exec result is one of the correct types.
107+
DCHECK_IMPLIES(!entry->IsUndefined(), entry->IsString());
108+
106109
if (entry->IsUndefined()) {
107110
result.emplace_back(std::nullopt);
108111
} else if (entry->IsString()) {
109112
Utf8Value utf8_entry(isolate, entry.As<String>());
110113
result.emplace_back(utf8_entry.ToString());
111-
} else {
112-
UNREACHABLE("v8::RegExp::Exec return a non-string, non-undefined value.");
113114
}
114115
}
115116
return result;
@@ -184,7 +185,8 @@ void URLPattern::New(const FunctionCallbackInfo<Value>& args) {
184185
return;
185186
}
186187
} else {
187-
THROW_ERR_MISSING_ARGS(env, "baseURL or options must be provided");
188+
THROW_ERR_INVALID_ARG_TYPE(env,
189+
"second argument must be a string or object");
188190
return;
189191
}
190192

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const { URLPattern } = require('url');
6+
const { throws } = require('assert');
7+
8+
// Verifies that calling URLPattern with no new keyword throws.
9+
throws(() => URLPattern(), {
10+
code: 'ERR_CONSTRUCT_CALL_REQUIRED',
11+
});
12+
13+
// Verifies that type checks are performed on the arguments.
14+
throws(() => new URLPattern(1), {
15+
code: 'ERR_INVALID_ARG_TYPE',
16+
});
17+
18+
throws(() => new URLPattern({}, 1), {
19+
code: 'ERR_INVALID_ARG_TYPE',
20+
});
21+
22+
throws(() => new URLPattern({}, '', 1), {
23+
code: 'ERR_INVALID_ARG_TYPE',
24+
});
25+
26+
throws(() => new URLPattern({}, { ignoreCase: '' }), {
27+
code: 'ERR_INVALID_ARG_TYPE',
28+
});
29+
30+
const pattern = new URLPattern();
31+
32+
throws(() => pattern.exec(1), {
33+
code: 'ERR_INVALID_ARG_TYPE',
34+
});
35+
36+
throws(() => pattern.exec('', 1), {
37+
code: 'ERR_INVALID_ARG_TYPE',
38+
});
39+
40+
throws(() => pattern.test(1), {
41+
code: 'ERR_INVALID_ARG_TYPE',
42+
});
43+
44+
throws(() => pattern.test('', 1), {
45+
code: 'ERR_INVALID_ARG_TYPE',
46+
});

0 commit comments

Comments
 (0)