Skip to content

Commit

Permalink
Changed to form data
Browse files Browse the repository at this point in the history
Fixed #1
  • Loading branch information
confuser committed May 10, 2016
1 parent 57ca790 commit 636eabf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ module.exports = function (publicKey, secretKey) {
, opts =
{ url: 'https://www.google.com/recaptcha/api/siteverify'
, method: 'post'
, json: data
, form: data
}

if (req.ip) data.remoteip = req.ip

request(opts, function (error, res) {
if (error) return next(error)
if (!res.body) return next(new Error('Missing body response from recaptcha'))
if (res.body.success) return next()

var errors = res.body['error-codes']
var body

try {
body = JSON.parse(res.body)
} catch (e) {
return next(e)
}

if (body.success) return next()

var errors = body['error-codes']

if (!errors || errors.length === 0) return next(new Error('Recaptcha not successful but no error codes provided'))

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"coveralls": "cat coverage/lcov.info | coveralls",
"pretest": "npm run lint",
"test": "istanbul cover _mocha test",
"posttest": "npm run coveralls && (istanbul check-coverage --statements 90 --branches 75 --functions 100 --lines 95) || echo Look at 'coverage/lcov-report/index.html' to find out more",
"posttest": "npm run coveralls && (istanbul check-coverage --statements 90 --branches 75 --functions 100 --lines 95) || echo open coverage/lcov-report/index.html to find out more",
"prepublish": "npm test && npm prune"
},
"repository": {
Expand Down
18 changes: 15 additions & 3 deletions test/middleware.int.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Recaptcha Middleware', function () {
it('should use g-recaptcha-response body field', function (done) {
nock(recaptchaDomain)
.filteringRequestBody(function (body) {
assert.deepEqual(JSON.parse(body), { secret: 'a', response: 'hello' })
assert.deepEqual(body, 'secret=a&response=hello')

done()
})
Expand All @@ -42,7 +42,7 @@ describe('Recaptcha Middleware', function () {
it('should use g-recaptcha-response query field', function (done) {
nock(recaptchaDomain)
.filteringRequestBody(function (body) {
assert.deepEqual(JSON.parse(body), { secret: 'a', response: 'hello' })
assert.deepEqual(body, 'secret=a&response=hello')

done()
})
Expand All @@ -55,7 +55,7 @@ describe('Recaptcha Middleware', function () {
it('should attach ip', function (done) {
nock(recaptchaDomain)
.filteringRequestBody(function (body) {
assert.deepEqual(JSON.parse(body), { remoteip: 'a', secret: 'a', response: 'hello' })
assert.deepEqual(body, 'secret=a&response=hello&remoteip=a')

done()
})
Expand Down Expand Up @@ -89,6 +89,18 @@ describe('Recaptcha Middleware', function () {
})
})

it('should error on invalid JSON response', function (done) {
nock(recaptchaDomain)
.post(recaptchaRoute)
.reply(200, '{asdasd')

middleware('a', 'a')({ ip: 'a', query: { 'g-recaptcha-response': 'hello' } }, null, function (error) {
assert.equal(error.message, 'Unexpected token a')

done()
})
})

it('should error on empty error-codes', function (done) {
nock(recaptchaDomain)
.post(recaptchaRoute)
Expand Down

0 comments on commit 636eabf

Please sign in to comment.