Skip to content

Commit

Permalink
fix(buildApiResponse): pass correct response types (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera authored Jan 25, 2024
1 parent 4467f24 commit ce29a70
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/__tests__/__snapshots__/buildApiResponse.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports[`buildApiResponse should build a response with specific properties: buil
],
[
"Content-Type",
"text/plain",
"application/json",
],
]
`;
Expand Down Expand Up @@ -94,7 +94,10 @@ exports[`buildApiResponse should parse random mock api settings: parseCustomMock

exports[`buildApiResponse should return a mock mime type and parsed content: parseContentAndType 1`] = `
{
"content": ""{\\n \\"foo\\": \\"hello\\",\\n \\"bar\\": \\"world\\"\\n}"",
"content": "{
"foo": "hello",
"bar": "world"
}",
"contentType": "application/json",
}
`;
Expand Down
28 changes: 23 additions & 5 deletions src/__tests__/buildApiResponse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,25 @@ describe('buildApiResponse', () => {
const mockApiResponse = [
{
type: 'get',
url: '/hello/world/'
url: '/hello/world/',
success: {
examples: [
{
title: 'Success-Response:',
content: 'HTTP/1.1 200 OK\n{\n "foo": "hello",\n "bar": "world"\n}',
type: 'json'
}
]
},
error: {
examples: [
{
title: 'Error-Response:',
content: 'HTTP/1.1 400 Bad Request\n{\n "detail": "Bad request."\n}',
type: 'json'
}
]
}
}
];

Expand All @@ -58,15 +76,15 @@ describe('buildApiResponse', () => {
const objContent = 'HTTP/1.1 200 OK\n{\n "foo": "hello",\n "bar": "world"\n}';
const contentType = 'json';

expect(getContentAndType({ content: objContent, contentType })).toMatchSnapshot('parseContentAndType');
expect(getContentAndType({ content: objContent, type: contentType })).toMatchSnapshot('parseContentAndType');
expect(getContentAndType({ content: objContent })).toMatchSnapshot('parseContentAndType.fallback');
expect(getContentAndType({ content: objContent, contentType: 'lorem/ipsum' })).toMatchSnapshot(
expect(getContentAndType({ content: objContent, type: 'lorem/ipsum' })).toMatchSnapshot(
'parseContentAndType.passthrough'
);

const xmlContent = `<lorem><ipsum dolor="sit" /></lorem>`;
expect(getContentAndType({ content: xmlContent, contentType: 'xml' })).toMatchSnapshot('parseContentAndType.xml');
expect(getContentAndType({ content: xmlContent, contentType: 'svg' })).toMatchSnapshot('parseContentAndType.svg');
expect(getContentAndType({ content: xmlContent, type: 'xml' })).toMatchSnapshot('parseContentAndType.xml');
expect(getContentAndType({ content: xmlContent, type: 'svg' })).toMatchSnapshot('parseContentAndType.svg');
});

it('should parse custom mock api settings', () => {
Expand Down
16 changes: 8 additions & 8 deletions src/buildApiResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,43 @@ const getCustomMockSettings = memo(({ settings = [] } = {}) => {
*
* @param {object} params
* @param {string} params.content
* @param {string} params.contentType
* @param {string} params.type
* @returns {{content: string, contentType: string}}
*/
const getContentAndType = memo(({ content = '', contentType = 'text' } = {}) => {
const getContentAndType = memo(({ content = '', type: contentType } = {}) => {
let updatedContent = content;
let updatedType;

if (/^HTTP/.test(content)) {
updatedContent = content.split(/\n/).slice(1).join('\n');
}

if (new RegExp('json', 'i').test(contentType)) {
updatedContent = JSON.stringify(updatedContent, null, 2);
}

/**
* Ignore content types that already contain a `/`
*/
if (contentType.split('/').length > 1) {
if (contentType?.split('/').length > 1) {
return {
content: updatedContent,
contentType
};
}

switch (contentType) {
case 'zip':
case 'gzip':
case 'json':
updatedType = 'application/json';
updatedType = `application/${contentType}`;
break;
case 'xml':
case 'html':
case 'csv':
case 'css':
updatedType = `text/${contentType}`;
break;
case 'svg':
updatedType = 'image/svg+xml';
break;
case 'txt':
default:
updatedType = 'text/plain';
break;
Expand Down

0 comments on commit ce29a70

Please sign in to comment.