Skip to content

Commit

Permalink
Merge pull request #46 from iharthi/fix/delete-requests-fail
Browse files Browse the repository at this point in the history
Fix requests without content failing
  • Loading branch information
jorgenader authored Jan 11, 2019
2 parents 75b8c53 + fa26200 commit 929a31a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions packages/saga-router/test/SagaResource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('createSagaRouter functional', () => {

await expectResponse(store.runSaga(api.dogs.details.del(
params, null, null, null, { onRequestError: onError },
)), { deleted: true }, store);
)), null, store);

try {
await store.runSaga(api.dogs.details.fetch(params)).toPromise();
Expand All @@ -140,7 +140,7 @@ describe('createSagaRouter functional', () => {
expect(err.statusCode).toEqual(404);
}

expect(store.getState()).toEqual({ deleted: true });
expect(store.getState()).toEqual(null);
});

test('statusValidationError is handled properly', async () => {
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('createSagaRouter functional', () => {

await expectResponse(
store.runSagaInitialized(api.dogs.details.del(params, null, null, null, requestConfig)),
{ deleted: true }, store,
null, store,
);

try {
Expand All @@ -220,7 +220,7 @@ describe('createSagaRouter functional', () => {
expect(onError.mock.calls.length).toEqual(1);
}

expect(store.getState()).toEqual({ deleted: true });
expect(store.getState()).toEqual(null);

try {
await store.runSagaInitialized(api.dogs.details.del(params, null, null, null, requestConfig)).toPromise();
Expand Down
6 changes: 2 additions & 4 deletions packages/test-server/src/test-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,8 @@ function configureServer(logger: boolean = false) {

if (dogIndex !== -1) {
allDogs.splice(dogIndex, 1);

res.status(200).json({
deleted: true,
});
res.removeHeader('Content-Type');
res.status(204).end();
} else {
res.status(404).json({
message: 'object does not exist',
Expand Down
21 changes: 11 additions & 10 deletions packages/tg-resources-fetch/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,17 @@ function parseHeaders(headers: Headers): HeadersObject {


function parseFetchResponse(response: Response, req: Request): Promise<FetchResponseInterface> {
// Content type will be required
// Content type will not be required when there is no content
// This is only valid for HEAD requests and status_code=204
if (response.status === 204 || req.method.toLowerCase() === 'head') {
return Promise.resolve({
status: response.status,
headers: parseHeaders(response.headers),
// Respond with same values that superagent produces
body: response.status === 204 ? null : {},
text: response.status === 204 ? null : '{}',
});
}
if (!response.headers.has('content-type')) {
// istanbul ignore next: Only happens w/ custom server that does not set Content-Type
throw new Error('Content type is missing from request');
Expand All @@ -140,15 +150,6 @@ function parseFetchResponse(response: Response, req: Request): Promise<FetchResp
// Get content string to use correct parser
const contentType: string = response.headers.get('content-type') as string;

if (req.method.toLowerCase() === 'head') {
return Promise.resolve({
status: response.status,
headers: parseHeaders(response.headers),
body: {},
text: '{}',
});
}

if (contentType.includes('application/json')) {
return response.json().then((body: any) => ({
status: response.status,
Expand Down
2 changes: 1 addition & 1 deletion packages/tg-resources-fetch/test/functional-fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ describe('Resource basic requests work', () => {
apiRoot: hostUrl,
});

await expectResponse(res.del({ pk: 'f2d8f2a6-7b68-4f81-8e47-787e4260b815' }), { deleted: true });
await expectResponse(res.del({ pk: 'f2d8f2a6-7b68-4f81-8e47-787e4260b815' }), null);
await expectError(res.fetch({ pk: 'f2d8f2a6-7b68-4f81-8e47-787e4260b815' }), { statusCode: 404 });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ describe('Resource basic requests work', () => {
apiRoot: hostUrl,
});

await expectResponse(res.del({ pk: 'f2d8f2a6-7b68-4f81-8e47-787e4260b815' }), { deleted: true });
await expectResponse(res.del({ pk: 'f2d8f2a6-7b68-4f81-8e47-787e4260b815' }), null);
await expectError(res.fetch({ pk: 'f2d8f2a6-7b68-4f81-8e47-787e4260b815' }), { statusCode: 404 });
});

Expand Down

0 comments on commit 929a31a

Please sign in to comment.