Browse Source

Don't throw HTTPError on 304 responses (#252)

extract-response
Luke Childs 8 years ago
committed by Sindre Sorhus
parent
commit
7b12b7582d
  1. 4
      index.js
  2. 8
      readme.md
  3. 13
      test/http.js

4
index.js

@ -119,7 +119,7 @@ function asPromise(opts) {
}
}
if (statusCode < 200 || statusCode > limitStatusCode) {
if (statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
throw new got.HTTPError(statusCode, opts);
}
@ -178,7 +178,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;
}

8
readme.md

@ -340,7 +340,9 @@ request(`https://${config.host}/production/`, {
```
## 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.
@ -355,6 +357,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

13
test/http.js

@ -15,6 +15,11 @@ test.before('setup', async () => {
res.end();
});
s.on('/304', (req, res) => {
res.statusCode = 304;
res.end();
});
s.on('/404', (req, res) => {
res.statusCode = 404;
res.end('not');
@ -54,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));

Loading…
Cancel
Save