Created
April 29, 2014 11:28
-
-
Save Stofkn/11397460 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
var request = require('supertest'), | |
agent = request.agent(), | |
server = require('../../../server/app'); | |
// Set root url. | |
request = request('http://localhost:9000'); | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
// ### Helper functions for sending requests | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Abstractions for sending request. | |
// Attaches a cookie with session id `s` for each request. | |
function post (path) { | |
var req = request.post(path); | |
agent.attachCookies(req); | |
return req.set('x-csrf-token', agent.csrf); | |
} | |
function put (path) { | |
var req = request.put(path); | |
agent.attachCookies(req); | |
return req.set('x-csrf-token', agent.csrf); | |
} | |
function del (path) { | |
var req = request.del(path); | |
agent.attachCookies(req); | |
return req.set('x-csrf-token', agent.csrf); | |
} | |
function get (path) { | |
var req = request.get(path); | |
agent.attachCookies(req); | |
return req.expect('x-csrf-token', /[a-zA-Z0-9/+]{20,}[=]/); | |
} | |
function saveCookies (res) { | |
agent.saveCookies(res); | |
} | |
function saveCsrf (res) { | |
agent.csrf = res.headers['x-csrf-token']; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
exports.start = server.start; | |
exports.stop = server.stop; | |
exports.post = post; | |
exports.put = put; | |
exports.del = del; | |
exports.get = get; | |
exports.saveCookies = saveCookies; | |
exports.saveCsrf = saveCsrf; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment