Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 840464e

Browse files
author
Miroslav Bajtoš
committed
helpers: add parameter for request body
Allow tests to send arbitrary request body in the requests being tested.
1 parent d5a0cc8 commit 840464e

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

lib/helpers.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ _beforeEach.givenAnAnonymousToken = function(attrs, optionalHandler) {
142142
_beforeEach.givenModel('accessToken', {id: '$anonymous'}, optionalHandler);
143143
}
144144

145-
_describe.whenCalledRemotely = function(verb, url, cb) {
145+
_describe.whenCalledRemotely = function(verb, url, data, cb) {
146+
if (cb == undefined) {
147+
cb = data;
148+
data = null;
149+
}
150+
146151
var urlStr = url;
147152
if(typeof url === 'function') {
148153
urlStr = '/<dynamic>';
@@ -165,6 +170,12 @@ _describe.whenCalledRemotely = function(verb, url, cb) {
165170
if(this.loggedInAccessToken) {
166171
this.http.set('authorization', this.loggedInAccessToken.id);
167172
}
173+
if (data) {
174+
var payload = data;
175+
if (typeof data === 'function')
176+
payload = data.call(this);
177+
this.http.send(payload);
178+
}
168179
this.req = this.http.req;
169180
var test = this;
170181
this.http.end(function(err) {
@@ -186,24 +197,24 @@ _describe.whenLoggedInAsUser = function(credentials, cb) {
186197
});
187198
}
188199

189-
_describe.whenCalledByUser = function(credentials, verb, url, cb) {
200+
_describe.whenCalledByUser = function(credentials, verb, url, data, cb) {
190201
describe('when called by logged in user', function () {
191202
_beforeEach.givenLoggedInUser(credentials);
192-
_describe.whenCalledRemotely(verb, url, cb);
203+
_describe.whenCalledRemotely(verb, url, data, cb);
193204
});
194205
}
195206

196-
_describe.whenCalledAnonymously = function(verb, url, cb) {
207+
_describe.whenCalledAnonymously = function(verb, url, data, cb) {
197208
describe('when called anonymously', function () {
198209
_beforeEach.givenAnAnonymousToken();
199-
_describe.whenCalledRemotely(verb, url, cb);
210+
_describe.whenCalledRemotely(verb, url, data, cb);
200211
});
201212
}
202213

203-
_describe.whenCalledUnauthenticated = function(verb, url, cb) {
214+
_describe.whenCalledUnauthenticated = function(verb, url, data, cb) {
204215
describe('when called with unauthenticated token', function () {
205216
_beforeEach.givenAnAnonymousToken();
206-
_describe.whenCalledRemotely(verb, url, cb);
217+
_describe.whenCalledRemotely(verb, url, data, cb);
207218
});
208219
}
209220

@@ -234,8 +245,8 @@ _it.shouldNotBeFound = function() {
234245
}
235246

236247
_it.shouldBeAllowedWhenCalledAnonymously =
237-
function(verb, url) {
238-
_describe.whenCalledAnonymously(verb, url, function() {
248+
function(verb, url, data) {
249+
_describe.whenCalledAnonymously(verb, url, data, function() {
239250
_it.shouldBeAllowed();
240251
});
241252
}
@@ -248,8 +259,8 @@ function(verb, url) {
248259
}
249260

250261
_it.shouldBeAllowedWhenCalledUnauthenticated =
251-
function(verb, url) {
252-
_describe.whenCalledUnauthenticated(verb, url, function() {
262+
function(verb, url, data) {
263+
_describe.whenCalledUnauthenticated(verb, url, data, function() {
253264
_it.shouldBeAllowed();
254265
});
255266
}
@@ -262,8 +273,8 @@ function(verb, url) {
262273
}
263274

264275
_it.shouldBeAllowedWhenCalledByUser =
265-
function(credentials, verb, url) {
266-
_describe.whenCalledByUser(credentials, verb, url, function() {
276+
function(credentials, verb, url, data) {
277+
_describe.whenCalledByUser(credentials, verb, url, data, function() {
267278
_it.shouldBeAllowed();
268279
});
269280
}

0 commit comments

Comments
 (0)