Skip to content

Commit

Permalink
feat: error on 304s (#673)
Browse files Browse the repository at this point in the history
304s are only returned if according headers are sent. Caching has to be handled by user
  • Loading branch information
jsg2021 authored and gr2m committed Dec 31, 2017
1 parent 8940925 commit a0c65a2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/error.js
Expand Up @@ -45,6 +45,7 @@ Util.inherits(exports.HttpError, Error);
}).call(exports.HttpError.prototype)

var statusCodes = {
304: 'Not Modified', // See PR #673 (https://github.com/octokit/node-github/pull/673)
400: 'Bad Request',
401: 'Unauthorized',
402: 'Payment Required',
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Expand Up @@ -739,13 +739,13 @@ var Client = module.exports = function (config) {
callCallback(err)
})
res.on('end', function () {
if (res.statusCode >= 301 && res.statusCode <= 307) {
if (res.statusCode !== 304 && res.statusCode >= 301 && res.statusCode <= 307) {
options.path = Url.parse(res.headers.location, true).path
httpSendRequest()
return
}

if (res.statusCode >= 400 || res.statusCode < 10) {
if (res.statusCode === 304 || res.statusCode >= 400 || res.statusCode < 10) {
callCallback(new error.HttpError(data, res.statusCode, res.headers))
} else {
res.data = data
Expand Down
42 changes: 42 additions & 0 deletions test/integration/conditional-request-test.js
@@ -0,0 +1,42 @@
const chai = require('chai')
const nock = require('nock')

const GitHub = require('../../')

const mocha = require('mocha')
const describe = mocha.describe
const it = mocha.it
chai.should()

describe('request 304s', () => {
it('304 etag', () => {
nock('https://request-errors-test.com')
.get('/orgs/myorg')
.reply(304, '')

const github = new GitHub({
host: 'request-errors-test.com'
})

return github.orgs.get({org: 'myorg', headers: {'If-None-Match': 'etag'}})

.catch(exception => {
exception.code.should.equal(304)
})
})
it('304 last-modified', () => {
nock('https://request-errors-test.com')
.get('/orgs/myorg')
.reply(304, '')

const github = new GitHub({
host: 'request-errors-test.com'
})

return github.orgs.get({org: 'myorg', headers: {'If-Modified-Since': 'Sun Dec 24 2017 22:00:00 GMT-0600 (CST)'}})

.catch(exception => {
exception.code.should.equal(304)
})
})
})

0 comments on commit a0c65a2

Please sign in to comment.