Skip to content

Instantly share code, notes, and snippets.

@Stofkn
Created April 29, 2014 11:28
Show Gist options
  • Save Stofkn/11397460 to your computer and use it in GitHub Desktop.
Save Stofkn/11397460 to your computer and use it in GitHub Desktop.
'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