diff --git a/lib/error.js b/lib/error.js index c68b028083..66fd720d36 100644 --- a/lib/error.js +++ b/lib/error.js @@ -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', diff --git a/lib/index.js b/lib/index.js index 74af1ca1c2..fdd98bb098 100644 --- a/lib/index.js +++ b/lib/index.js @@ -738,13 +738,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 diff --git a/test/integration/conditional-request-test.js b/test/integration/conditional-request-test.js new file mode 100644 index 0000000000..045729eba5 --- /dev/null +++ b/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) + }) + }) +})