From 4d770e3b89254c4cec30c422cdcdad257500c9cc Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Wed, 30 Jun 2021 09:14:27 -0700 Subject: [PATCH] fix: throw error if missing uri or url (#239) Co-authored-by: Justin Beckwith --- src/index.ts | 5 +++++ test/index.ts | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/index.ts b/src/index.ts index a102f28..9956e3f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -119,6 +119,11 @@ function requestToFetchOptions(reqOpts: Options) { let uri = ((reqOpts as OptionsWithUri).uri || (reqOpts as OptionsWithUrl).url) as string; + + if (!uri) { + throw new Error('Missing uri or url in reqOpts.'); + } + if (reqOpts.useQuerystring === true || typeof reqOpts.qs === 'object') { // eslint-disable-next-line @typescript-eslint/no-var-requires const qs = require('querystring'); diff --git a/test/index.ts b/test/index.ts index 4ad9be9..ab398ff 100644 --- a/test/index.ts +++ b/test/index.ts @@ -435,4 +435,26 @@ describe('teeny', () => { } ); }); + + it('should throw an exception if uri is an empty string', done => { + assert.throws( + () => { + teenyRequest({uri: ''}); + }, + /Missing uri or url in reqOpts/, + 'Did not throw with expected message' + ); + done(); + }); + + it('should throw an exception if url is an empty string', done => { + assert.throws( + () => { + teenyRequest({url: ''}); + }, + /Missing uri or url in reqOpts/, + 'Did not throw with expected message' + ); + done(); + }); });