From ab79b364cb5f0694d653b4070986f86892c708ff Mon Sep 17 00:00:00 2001 From: "H. G. Parra" Date: Wed, 4 Dec 2024 02:53:46 -0800 Subject: [PATCH] Fix typo and add tests (#87) Co-authored-by: Bryan Stopp --- src/handlers/{unkown.js => unknown.js} | 3 ++- src/index.js | 4 ++-- test/handlers/unknown.test.js | 10 ++++++++++ test/index.test.js | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) rename src/handlers/{unkown.js => unknown.js} (94%) create mode 100644 test/handlers/unknown.test.js create mode 100644 test/index.test.js diff --git a/src/handlers/unkown.js b/src/handlers/unknown.js similarity index 94% rename from src/handlers/unkown.js rename to src/handlers/unknown.js index d371cbe..c648720 100644 --- a/src/handlers/unkown.js +++ b/src/handlers/unknown.js @@ -9,7 +9,8 @@ * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ -export default function unkownHandler() { + +export default function unknownHandler() { const body = JSON.stringify({ message: 'Unknown method. Please see: https://docs.da.live for more information.' }); return { body, status: 501 }; } diff --git a/src/index.js b/src/index.js index 6dc1671..db41b6a 100644 --- a/src/index.js +++ b/src/index.js @@ -16,7 +16,7 @@ import headHandler from './handlers/head.js'; import getHandler from './handlers/get.js'; import postHandler from './handlers/post.js'; import deleteHandler from './handlers/delete.js'; -import unkownHandler from './handlers/unkown.js'; +import unknownHandler from './handlers/unknown.js'; export default { async fetch(req, env) { @@ -45,7 +45,7 @@ export default { respObj = await deleteHandler({ req, env, daCtx }); break; default: - respObj = unkownHandler(); + respObj = unknownHandler(); } return daResp(respObj); diff --git a/test/handlers/unknown.test.js b/test/handlers/unknown.test.js new file mode 100644 index 0000000..6b3ddff --- /dev/null +++ b/test/handlers/unknown.test.js @@ -0,0 +1,10 @@ +import assert from 'assert'; +import unknownHandler from '../../src/handlers/unknown.js'; + +describe('unknownHandler', () => { + it('should return unknown response', async () => { + const result = await unknownHandler({}); + assert.strictEqual(result.status, 501); + assert.strictEqual(result.body.includes('Unknown method'), true); + }); +}); diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..e30410e --- /dev/null +++ b/test/index.test.js @@ -0,0 +1,18 @@ +import assert from 'assert'; +import handler from '../src/index.js'; + +describe('fetch', () => { + it('should be callable', () => { + assert(handler.fetch); + }); + + it('should return a response object for options', async () => { + const resp = await handler.fetch({ method: 'OPTIONS' }, {}); + assert.strictEqual(resp.status, 204); + }); + + it('should return a response object for unknown', async () => { + const resp = await handler.fetch({ url: 'https://www.example.com', method: 'BLAH' }, {}); + assert.strictEqual(resp.status, 501); + }); +});