Skip to content

feat: allow string responses #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Each mock object contains the following properties.
| `url` | Supports both **named parameters** (`/:foo/:bar`) and **query parameters**.(`/foo?bar=true`) | true | - |
| `method` | Supports `GET`, `POST`, `PUT`, `PATCH` and `DELETE` methods. | true | - |
| `status` | All possible [HTTP status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status). | true | - |
| `response` | A valid JSON format(Array or Object) or function. <br/> Response function is a function that contains request object as a parameter. See the **Custom Response** section for example. | true | - |
| `response` | A valid JSON format(Array or Object), function or string. <br/> Response function is a function that contains request object as a parameter. See the **Custom Response** section for example. | true | - |
| `delay` | Emulate delayed response time in milliseconds. | - | `0` |


Expand Down
4 changes: 4 additions & 0 deletions packages/mock-addon/src/utils/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export function getResponseHeaderMap(xhr) {
export const defaultResponseHeaders = {
'content-type': 'application/json',
};

export const textResponseHeaders = {
'content-type': 'text/plain',
};
6 changes: 4 additions & 2 deletions packages/mock-addon/src/utils/response.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'whatwg-fetch';
import statusTextMap from './statusMap';
import { defaultResponseHeaders } from './headers';
import { defaultResponseHeaders, textResponseHeaders } from './headers';

export function CustomResponse(url, status, responseText) {
const text =
Expand All @@ -13,7 +13,9 @@ export function CustomResponse(url, status, responseText) {
status: status,
statusText: statusTextMap[status.toString()],
headers: new Headers({
...defaultResponseHeaders,
...(typeof responseText === 'string'
? textResponseHeaders
: defaultResponseHeaders),
}),
url,
});
Expand Down
6 changes: 4 additions & 2 deletions packages/mock-addon/src/utils/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ describe('CustomResponse', () => {
});
it('should return text as a string if responseText is string', async () => {
const response = new CustomResponse(mockURL, 200, 'This is a string');
const actual = await response.text();
expect(actual).toEqual('This is a string');
const actualText = await response.text();
const actualContentType = await response.headers.get('content-type');
expect(actualText).toEqual('This is a string');
expect(actualContentType).toEqual('text/plain');
});
it('should return text as a string if responseText is an object', async () => {
const response = new CustomResponse(mockURL, 200, { key: 'test' });
Expand Down
1 change: 1 addition & 0 deletions packages/mock-addon/src/utils/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const schema = {
return (
(isObject(value) ||
Array.isArray(value) ||
typeof value === 'string' ||
typeof value === 'function') &&
value !== null
);
Expand Down
8 changes: 4 additions & 4 deletions packages/mock-addon/src/utils/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Validator', () => {
expect(actual).toEqual([]);
});

it('should return rul error array if url is not a string', () => {
it('should return url error array if url is not a string', () => {
const mock = {
url: {},
method: 'GET',
Expand Down Expand Up @@ -204,16 +204,16 @@ describe('Validator', () => {
expect(actual).toEqual([]);
});

it('should return not valid response error if response is a string', () => {
it('should return empty error if response is a string', () => {
const mock = {
url: 'https://jsonplaceholder.typicode.com/todos/:id',
method: 'GET',
status: 200,
delay: 0,
response: 'string',
response: 'a string value',
};
const actual = validate(mock, schema);
expect(actual).toEqual(['response: "string" is not valid.']);
expect(actual).toEqual([]);
});

it('should return not valid response error if response is null', () => {
Expand Down