Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src,test: expand test coverage for urlpattern and fix error #56878

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/node_url_pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ URLPatternRegexProvider::regex_search(std::string_view input,
return std::nullopt;
}

// V8 checks that the regexp exec result is one of the correct types.
DCHECK_IMPLIES(!entry->IsUndefined(), entry->IsString());

if (entry->IsUndefined()) {
result.emplace_back(std::nullopt);
} else if (entry->IsString()) {
Utf8Value utf8_entry(isolate, entry.As<String>());
result.emplace_back(utf8_entry.ToString());
} else {
UNREACHABLE("v8::RegExp::Exec return a non-string, non-undefined value.");
}
}
return result;
Expand Down Expand Up @@ -184,7 +185,8 @@ void URLPattern::New(const FunctionCallbackInfo<Value>& args) {
return;
}
} else {
THROW_ERR_MISSING_ARGS(env, "baseURL or options must be provided");
THROW_ERR_INVALID_ARG_TYPE(env,
"second argument must be a string or object");
return;
}

Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-urlpattern-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

require('../common');

const { URLPattern } = require('url');
const { throws } = require('assert');

// Verifies that calling URLPattern with no new keyword throws.
throws(() => URLPattern(), {
code: 'ERR_CONSTRUCT_CALL_REQUIRED',
});

// Verifies that type checks are performed on the arguments.
throws(() => new URLPattern(1), {
code: 'ERR_INVALID_ARG_TYPE',
});

throws(() => new URLPattern({}, 1), {
code: 'ERR_INVALID_ARG_TYPE',
});

throws(() => new URLPattern({}, '', 1), {
code: 'ERR_INVALID_ARG_TYPE',
});

throws(() => new URLPattern({}, { ignoreCase: '' }), {
code: 'ERR_INVALID_ARG_TYPE',
});

const pattern = new URLPattern();

throws(() => pattern.exec(1), {
code: 'ERR_INVALID_ARG_TYPE',
});

throws(() => pattern.exec('', 1), {
code: 'ERR_INVALID_ARG_TYPE',
});

throws(() => pattern.test(1), {
code: 'ERR_INVALID_ARG_TYPE',
});

throws(() => pattern.test('', 1), {
code: 'ERR_INVALID_ARG_TYPE',
});
Loading