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..41dc53d 100644 --- a/src/handlers/unkown.js +++ b/src/handlers/unknown.js @@ -9,7 +9,7 @@ * 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/src/utils/daCtx.js b/src/utils/daCtx.js index 5615bc0..acd5e89 100644 --- a/src/utils/daCtx.js +++ b/src/utils/daCtx.js @@ -46,7 +46,7 @@ export default async function getDaCtx(req, env) { org, users, fullKey, - initiator: req.headers.get('x-da-initiator'), + initiator: req.headers?.get('x-da-initiator'), }; // Get org properties diff --git a/test/handlers/unknown.test.js b/test/handlers/unknown.test.js new file mode 100644 index 0000000..860cc93 --- /dev/null +++ b/test/handlers/unknown.test.js @@ -0,0 +1,21 @@ +/* + * Copyright 2024 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +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..7b50dd3 --- /dev/null +++ b/test/index.test.js @@ -0,0 +1,30 @@ +/* + * Copyright 2024 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +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); + }); +});