Skip to content

Commit

Permalink
Fix Unknown Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
bstopp committed Dec 5, 2024
1 parent e1d5a00 commit cd2d06c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/handlers/unkown.js → src/handlers/unknown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -45,7 +45,7 @@ export default {
respObj = await deleteHandler({ req, env, daCtx });
break;
default:
respObj = unkownHandler();
respObj = unknownHandler();
}

return daResp(respObj);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/daCtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/handlers/unknown.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
30 changes: 30 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit cd2d06c

Please sign in to comment.