Last active
August 29, 2015 14:23
-
-
Save paulserraino/3e2d16a3b1fec3ded2b7 to your computer and use it in GitHub Desktop.
testing secure routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert') | |
var request = require('request') | |
describe('My API', function () { | |
// initialize request cookie jar | |
request = request.defaults({ jar: request.jar() }) | |
var cred = { | |
username: 'foo' | |
, password: 'bar' | |
}; | |
// login as user 'foo' before the first test | |
before('POST /login', function (done) { | |
request.post('/login', cred, done) | |
}) | |
// test a post request to your secure route | |
it('POST /api/v1/posts', function (done) { | |
var data = { | |
title: 'my blog post' | |
, created_at: new Date() | |
, content: 'Bespoke polaroid beard, American Apparel sustainable' | |
} | |
request.post('/api/v1/posts', data, function (error, res, body) { | |
assert(!error, error); | |
assert.equal(res.statusCode, 200); | |
assert.equal(res.request.uri.pathname, '/api/v1/posts'); | |
done() | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment