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

Don't throw HTTPError on 304 responses #252

Merged
merged 4 commits into from
May 1, 2017
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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function asPromise(opts) {
}
}

if (statusCode < 200 || statusCode > limitStatusCode) {
if (statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
throw new got.HTTPError(statusCode, opts);
}

Expand Down Expand Up @@ -185,7 +185,7 @@ function asStream(opts) {

res.pipe(output);

if (statusCode < 200 || statusCode > 299) {
if (statusCode !== 304 && (statusCode < 200 || statusCode > 299)) {
proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
return;
}
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ got('unix:/var/run/docker.sock:/containers/json');
```


## Tip
## Tips

### User Agent

It's a good idea to set the `'user-agent'` header so the provider can more easily see how their resource is used. By default, it's the URL to this repo.

Expand All @@ -316,6 +318,10 @@ got('todomvc.com', {
});
```

### 304 Responses

Bear in mind, if you send an `if-modified-since` header and receive a `304 Not Modified` response, the body will be empty. It's your responsibility to cache and retrieve the body contents.


## Related

Expand Down
19 changes: 15 additions & 4 deletions test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ test.before('setup', async () => {
res.end();
});

s.on('/304', (req, res) => {
res.statusCode = 304;
res.end();
});

s.on('/404', (req, res) => {
setTimeout(() => {
res.statusCode = 404;
res.end('not');
}, 10);
res.statusCode = 404;
res.end('not');
});

s.on('/?recent=true', (req, res) => {
Expand Down Expand Up @@ -56,6 +59,14 @@ test('error with code', async t => {
}
});

test('status code 304 doesn\'t throw', async t => {
const p = got(`${s.url}/304`);
await t.notThrows(p);
const response = await p;
t.is(response.statusCode, 304);
t.is(response.body, '');
});

test('buffer on encoding === null', async t => {
const data = (await got(s.url, {encoding: null})).body;
t.truthy(Buffer.isBuffer(data));
Expand Down