Skip to content

Commit

Permalink
Fix handling of invalid arguments (#2367)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zen-cronic authored Jul 24, 2024
1 parent bc17e81 commit f44ef43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ const create = (defaults: InstanceDefaults): Got => {
const lastHandler = (normalized: Options): GotReturn => {
// Note: `options` is `undefined` when `new Options(...)` fails
request.options = normalized;
request._noPipe = !normalized.isStream;
request._noPipe = !normalized?.isStream;
void request.flush();

if (normalized.isStream) {
if (normalized?.isStream) {
return request;
}

Expand All @@ -72,7 +72,7 @@ const create = (defaults: InstanceDefaults): Got => {

const result = handler(newOptions, iterateHandlers) as GotReturn;

if (is.promise(result) && !request.options.isStream) {
if (is.promise(result) && !request.options?.isStream) {
promise ||= asPromise(request);

if (result !== promise) {
Expand Down
10 changes: 10 additions & 0 deletions test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ test('throws if the url option is missing', async t => {
});
});

test('throws if an invalid argument is passed', async t => {
await t.throwsAsync(
// @ts-expect-error Error tests
got(false),
{
instanceOf: RequestError,
message: 'Expected values which are `string`, `URL`, `Object`, or `undefined`. Received values of type `boolean`.',
});
});

test('throws an error if the protocol is not specified', async t => {
const error = await t.throwsAsync(got('example.com'));
invalidUrl(t, error!, 'example.com');
Expand Down

0 comments on commit f44ef43

Please sign in to comment.