Skip to content
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

Fix status code and json request normalization #213

Merged
Merged
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
29 changes: 26 additions & 3 deletions packages/parrot-fetch/__tests__/ParrotFetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ describe('ParrotFetch', () => {
});
});

it('should parse the body is content-type is json', () => {
const parrotFetch = new ParrotFetch();
const normalized = parrotFetch.normalizeRequest('http://www.parrot.com/squawk', {
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ ahoy: 'matey' }),
});
expect(normalized).toMatchObject({
path: '/squawk',
body: {
ahoy: 'matey',
},
protocol: 'http:',
host: 'www.parrot.com',
});
});

it('should resolve to context fetch', () => {
const input = 'http://www.parrot.com';
const contextFetch = jest.fn(() => 'ahoy');
Expand All @@ -39,12 +57,17 @@ describe('ParrotFetch', () => {
expect(resolved).toBe('ahoy');
});

it('should resolve response', () => {
it('should resolve response', async () => {
const input = 'http://www.parrot.com';
const contextFetch = jest.fn();
const parrotFetch = new ParrotFetch({}, contextFetch);
const resolved = parrotFetch.resolver(input)({ body: 'ahoy', status: 200 });
const resolved = parrotFetch.resolver(input)({ body: 'ahoy', status: 204 });
expect(contextFetch).not.toHaveBeenCalled();
return resolved.then(data => expect(data).toEqual(expect.any(Response)));
const data = await resolved;
await expect(data.text()).resolves.toBe('"ahoy"');
expect(data.status).toBe(204);
expect(Object.fromEntries([...data.headers])).toEqual({
'content-type': 'application/json',
});
});
});
19 changes: 16 additions & 3 deletions packages/parrot-fetch/src/ParrotFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ class ParrotFetch extends Parrot {

normalizeRequest = (input, { method = 'GET', ...init } = {}) => {
const { pathname: path, query, ...parsed } = parse(input, true);
const headers = new Headers(init.headers);
let { body } = init;
if (headers.get('Content-Type') === 'application/json') {
body = JSON.parse(body);
}
return {
...init,
...parsed,
body,
path,
query: Object.keys(query) && query,
method: method && method.toUpperCase(),
Expand All @@ -37,9 +43,16 @@ class ParrotFetch extends Parrot {
return this.contextFetch(input, init);
}
const { body, status } = response;
return Promise.resolve(
new Response(new Blob([JSON.stringify(body)], { status, type: 'application/json' }))
);
const responseBlob = new Blob([JSON.stringify(body)], {
type: 'application/json',
});
const responseOptions = {
status,
headers: {
'Content-Type': 'application/json',
},
};
return Promise.resolve(new Response(responseBlob, responseOptions));
};
}

Expand Down
Loading