Last active
December 27, 2015 15:09
-
-
Save shibukawa/7345717 to your computer and use it in GitHub Desktop.
node.js HTTP test snippet
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
// npm install request | |
// npm install mocha | |
var assert = require('assert'); | |
var request = require('request'); | |
describe('Login', function () { | |
it("can logout", function (done) { | |
request.post({ | |
url: 'http://127.0.0.1:3000/login', | |
followRedirect: false, | |
jar: true, | |
form: {email: 'test_user@example.com', password: 'correct_password'} | |
}, function (err,resp,body) { | |
request.get({ | |
url: 'http://127.0.0.1:3000/logout', | |
followRedirect: false, | |
jar: true | |
}, function (err, resp, body) { | |
assert.equal(resp.statusCode, 302); | |
assert.equal(resp.headers.location, '/login'); | |
done(); | |
} | |
); | |
}); | |
}); |
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
// npm install redis | |
// In test code | |
if (!global.redis) { | |
global.redis = redis.createClient(); | |
global.redis.select(15); | |
} | |
describe('some test', function () { | |
before(funciton () { | |
global.redis.flushdb(); | |
}); | |
}); | |
// In app.js of express | |
global.redis = redis.createClient(); | |
if ('testing' == app.get('env')) { | |
global.redis.select(15); | |
} | |
/* Run test script | |
#!/usr/bin/env sh | |
npm install | |
NODE_ENV=testing node app.js & | |
npm test | |
pkill node | |
*/ |
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
// npm install request | |
// npm install mocha | |
var assert = require('assert'); | |
var request = require('request'); | |
it("At first, it should redirect to /login", function (done) { | |
request({url: 'http://127.0.0.1:3000/', followRedirect: false}, function (err,resp,body) { | |
assert.equal(resp.statusCode, 302); | |
assert.equal(resp.headers.location, '/login'); | |
done(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment