-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,5 +31,8 @@ | |
"devDependencies": { | ||
"chai": "4.3.10", | ||
"sinon": "17.0.1" | ||
}, | ||
"dependencies": { | ||
"@adobe/fetch": "4.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2023 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 { Response } from '@adobe/fetch'; | ||
|
||
/** | ||
* Creates a response with a JSON body. Defaults to 200 status. | ||
* @param {object} body - JSON body. | ||
* @param {number} status - Optional status code. | ||
* @param {object} headers - Optional headers. | ||
* @return {Response} Response. | ||
*/ | ||
export function createResponse(body, status = 200, headers = {}) { | ||
return new Response( | ||
JSON.stringify(body), | ||
{ | ||
headers: { 'content-type': 'application/json; charset=utf-8', ...headers }, | ||
status, | ||
}, | ||
); | ||
} | ||
export function ok(body) { | ||
return createResponse(body, 200); | ||
} | ||
|
||
export function noContent(body) { | ||
return createResponse(body, 204); | ||
} | ||
|
||
export function badRequest(message) { | ||
return createResponse({ message }, 400); | ||
} | ||
|
||
export function notFound(message) { | ||
return createResponse({ message }, 404); | ||
} | ||
|
||
export function internalServerError(message) { | ||
return createResponse({ message }, 500, { | ||
'x-error': `internal server error: ${message}`, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2023 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. | ||
*/ | ||
/* eslint-env mocha */ | ||
import { expect } from 'chai'; | ||
import { | ||
ok, badRequest, notFound, internalServerError, | ||
} from '../src/http-utils.js'; | ||
|
||
describe('http-utils', () => { | ||
it('ok should return a 200 response with JSON body', async () => { | ||
const body = { key: 'value' }; | ||
const response = ok(body); | ||
expect(response.status).to.equal(200); | ||
expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8'); | ||
const respJson = await response.json(); | ||
expect(respJson).to.eql(body); | ||
}); | ||
|
||
it('badRequest should return a 400 response with JSON body', async () => { | ||
const response = badRequest('Bad Request'); | ||
expect(response.status).to.equal(400); | ||
expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8'); | ||
const respJson = await response.json(); | ||
expect(respJson).to.eql({ message: 'Bad Request' }); | ||
}); | ||
|
||
it('notFound return a 404 response with JSON body', async () => { | ||
const response = notFound('Not Found'); | ||
expect(response.status).to.equal(404); | ||
expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8'); | ||
const respJson = await response.json(); | ||
expect(respJson).to.eql({ message: 'Not Found' }); | ||
}); | ||
|
||
it('internalServerError should return a 500 response with JSON body', async () => { | ||
const response = internalServerError('uh oh'); | ||
expect(response.status).to.equal(500); | ||
expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8'); | ||
expect(response.headers.get('x-error')).to.equal('internal server error: uh oh'); | ||
const respJson = await response.json(); | ||
expect(respJson).to.eql({ message: 'uh oh' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters