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

make api reject 304s so caller can implement conditional requests #673

Merged
merged 4 commits into from
Dec 31, 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
1 change: 1 addition & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a 304 status in https://github.com/octokit/node-github/blob/master/lib/error.js#L47-L48? Maybe add a comment to reference this pull request, as it looks a little confusing to have a single 304 besides all the 400+ error codes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing, i’ll try to do it tomorrow

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the 304 to that file

} else {
res.data = data
Expand Down
42 changes: 42 additions & 0 deletions test/integration/conditional-request-test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})