Skip to content

Commit

Permalink
Merge pull request #13 from truework/content-length-check
Browse files Browse the repository at this point in the history
v1.2.0
  • Loading branch information
estrattonbailey authored Jun 23, 2020
2 parents 774bc5e + 0ef3093 commit 378a2f5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dist
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_js/
.rts2_cache_es/
.rts2_cache_umd/
5 changes: 1 addition & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ export function gretch<T = DefaultGretchResponse, A = DefaultGretchError>(
response = (await sent).clone();
status = response.status || 500;

if (
status !== 204 &&
parseInt(response.headers.get("Content-Length")) > 0
) {
if (status !== 204) {
resolved = await response.clone()[key]();
}

Expand Down
103 changes: 66 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gretchen",
"version": "1.1.2",
"version": "1.2.0-0",
"description": "Making fetch happen in Typescript.",
"source": "index.ts",
"main": "dist/gretchen.js",
Expand Down Expand Up @@ -58,7 +58,7 @@
"husky": "^4.2.3",
"microbundle": "^0.11.0",
"node-fetch": "^2.6.0",
"np": "^6.2.2",
"np": "^6.2.3",
"prettier": "^1.19.1",
"pretty-quick": "^2.0.1",
"ts-node": "^8.4.1"
Expand Down
37 changes: 3 additions & 34 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ test("returns data as error", async t => {
test(`body exists, will fail to parse non-json`, async t => {
const server = createServer((req, res) => {
res.writeHead(200, {
"Content-Length": "3"
"Content-Type": "application/json"
});
res.end("hey");
});
Expand All @@ -228,14 +228,9 @@ test(`body exists, will fail to parse non-json`, async t => {
});
});

/**
* Ccalling writeHead overrides default Content-Length
* set by res.end(), i.e. if we removed writeHead here
* this test would break.
*/
test(`won't parse body if Content-Length header doesn't exist`, async t => {
test(`won't parse body if 204 and json()`, async t => {
const server = createServer((req, res) => {
res.writeHead(200, {
res.writeHead(204, {
"Content-Type": "application/json"
});
res.end(JSON.stringify({ foo: true }));
Expand All @@ -258,32 +253,6 @@ test(`won't parse body if Content-Length header doesn't exist`, async t => {
});
});

test(`body does not exist, will not fail to parse non-json`, async t => {
const server = createServer((req, res) => {
res.writeHead(200);
res.end();
});

await new Promise(r => {
server.listen(async () => {
// @ts-ignore
const { port } = server.address();

const res = await gretch(`http://127.0.0.1:${port}`).json();

if (res.error) {
t.fail();
}

t.pass();

server.close();

r();
});
});
});

test(`hooks`, async t => {
const server = createServer((req, res) => {
res.writeHead(200);
Expand Down

0 comments on commit 378a2f5

Please sign in to comment.