Skip to content

Commit

Permalink
Merge pull request #161 from marduke182/159-check-content-type-in-fet…
Browse files Browse the repository at this point in the history
…ch-response

Solves #159 check content type in fetch response
  • Loading branch information
zzarcon authored Apr 11, 2019
2 parents 676a441 + 22caaec commit e7dc1d2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
57 changes: 56 additions & 1 deletion __tests__/router_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Server, Router } from '../src';
import { Server, Router, Response as KakapoResponse } from '../src';

describe('Router', () => {
test('Router # config # host', async () => {
Expand Down Expand Up @@ -261,5 +261,60 @@ describe('Router', () => {
expect(response.body).toEqual('async response');
});


test('Router # Fetch # Content-Type image/svg+xml', async () => {
const server = new Server();
const router = new Router();

const svgData = 'svgData';


router.get('/svg_content', request => {
return new KakapoResponse(200, svgData, {
'content-type': 'image/svg+xml'
});
});

server.use(router);

const response = await fetch('/svg_content');

expect(response.body).toEqual(svgData);
});


test('Router # Fetch # No Content Type', async () => {
const server = new Server();
const router = new Router();
const expectedResponse = { foo: 'bar'};

router.get('/json_content', request => {
return new KakapoResponse(200, expectedResponse);
});

server.use(router);

const response = await fetch('/json_content');

expect(response.json()).resolves.toEqual(expectedResponse);
});

test('Router # Fetch # Content Type application/json', async () => {
const server = new Server();
const router = new Router();
const expectedResponse = { foo: 'bar'};

router.get('/json_content', request => {
return new KakapoResponse(200, expectedResponse, {
'content-type': 'application/json',
});
});

server.use(router);

const response = await fetch('/json_content');

expect(response.body).toEqual(JSON.stringify(expectedResponse));
});
// @TODO Test strategies in router.
});
13 changes: 10 additions & 3 deletions src/interceptors/fetchInterceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ import {
import { mapRequestInfoToUrlString, canUseWindow } from "../utils";

const nativeFetch = canUseWindow && window.fetch;
const fakeResponse = (response = {}, headers = {}) => {
const responseStr = JSON.stringify(response);
return new Response(responseStr, { headers });
const fakeResponse = (response: string | any = {}, headers = {}) => {
// If content type exist and is different to application/json no parse the response
if (headers['content-type'] && headers['content-type'].indexOf('application/json') == -1) {
console.log(response);

return new Response(response, { headers });
}

// Default handler, If no content type response as json.
return new Response(JSON.stringify(response), { headers });
};

export class FakeFetch {
Expand Down

0 comments on commit e7dc1d2

Please sign in to comment.