Permalink
Browse files

Adding Frisby tests to repo

* Added Jasmine tests for Frisby library itself
* Added Frisby methods for setting response info directly for tests
  • Loading branch information...
1 parent 791bb1b commit 203abeade1e71d697a50d04fa6640c74ac17e132 @vlucas committed Jan 3, 2012
Showing with 213 additions and 16 deletions.
  1. +76 −15 lib/frisby.js
  2. +1 −1 package.json
  3. +83 −0 spec/frisby_matchers_spec.js
  4. +53 −0 spec/frisby_spec.js
View
@@ -51,7 +51,15 @@ function Frisby(msg) {
itInfo: null,
it: null,
expects: [],
- after: []
+ after: [],
+
+ // Response storage
+ response: {
+ error: null,
+ status: null,
+ headers: [],
+ body: null
+ }
};
this.currentRequestFinished = false;
@@ -71,7 +79,11 @@ function Frisby(msg) {
// @param {Object}
//
Frisby.prototype.setup = function(opts) {
- this._setup = _.extend(this._setup, opts);
+ if(_.isUndefined(opts)) {
+ return this._setup;
+ }
+ var oldSetup = this._setup;
+ this._setup = opts;
if(typeof this._setup.request === "undefined") {
this._setup.request = {};
this._setup.request.headers = {};
@@ -164,6 +176,7 @@ Frisby.prototype._request = function (/* method [uri, data, params] */) {
port = this.port && this.port !== 80 ? ':' + this.port : '',
fullUri,
outgoing = {
+ json: params.json || false,
uri: null,
body: null,
method: 'GET',
@@ -190,6 +203,7 @@ Frisby.prototype._request = function (/* method [uri, data, params] */) {
if (data) {
// if JSON data
if(outgoing.json) {
+ outgoing.headers['Content-Type'] = 'application/json';
outgoing.body = JSON.stringify(data);
} else {
// Default to 'application/x-www-form-urlencoded'
@@ -204,7 +218,7 @@ Frisby.prototype._request = function (/* method [uri, data, params] */) {
outgoing.method = method;
//
- // Create the description for this test.
+ // Create the description for this test based on the METHOD and URL
//
this.current.itInfo = method.toUpperCase() + ' ' + outgoing.uri;
@@ -216,6 +230,14 @@ Frisby.prototype._request = function (/* method [uri, data, params] */) {
self.currentRequestFinished = false;
request(outgoing, function(err, res, body) {
self.currentRequestFinished = {err: err, res: res, body: body, req: outgoing};
+
+ // Store relevant current response parts
+ self.current.response = {
+ error: err,
+ status: res.statusCode,
+ headers: res.headers,
+ body: body
+ };
});
};
@@ -230,7 +252,7 @@ Frisby.prototype._request = function (/* method [uri, data, params] */) {
Frisby.prototype.expectStatus = function(statusCode) {
var self = this;
this.current.expects.push(function() {
- expect(self.currentRequestFinished.res.statusCode).toEqual(statusCode);
+ expect(self.current.response.status).toEqual(statusCode);
})
return this;
};
@@ -240,8 +262,8 @@ Frisby.prototype.expectHeader = function(header, content) {
var self = this;
var header = header+"".toLowerCase();
this.current.expects.push(function() {
- if(typeof self.currentRequestFinished.res.headers[header] !== "undefined") {
- expect(self.currentRequestFinished.res.headers[header].toLowerCase()).toEqual(content.toLowerCase());
+ if(typeof self.current.response.headers[header] !== "undefined") {
+ expect(self.current.response.headers[header].toLowerCase()).toEqual(content.toLowerCase());
} else {
fail("Header '" + header.toLowerCase() + "' not present in HTTP response");
}
@@ -254,8 +276,8 @@ Frisby.prototype.expectHeaderContains = function(header, content) {
var self = this;
var header = header+"".toLowerCase();
this.current.expects.push(function() {
- if(typeof self.currentRequestFinished.res.headers[header] !== "undefined") {
- expect(self.currentRequestFinished.res.headers[header].toLowerCase()).toContain(content.toLowerCase());
+ if(typeof self.current.response.headers[header] !== "undefined") {
+ expect(self.current.response.headers[header].toLowerCase()).toContain(content.toLowerCase());
} else {
fail("Header '" + header.toLowerCase() + "' not present in HTTP response");
}
@@ -267,8 +289,8 @@ Frisby.prototype.expectHeaderContains = function(header, content) {
Frisby.prototype.expectBodyContains = function(content) {
var self = this;
this.current.expects.push(function() {
- if(typeof self.currentRequestFinished.body !== "undefined") {
- expect(self.currentRequestFinished.body).toContain(content);
+ if(typeof self.current.response.body !== "undefined") {
+ expect(self.current.response.body).toContain(content);
} else {
fail("No HTTP response body was present or HTTP response was empty");
}
@@ -285,7 +307,7 @@ Frisby.prototype.expectJSONTypes = function(/* [tree], jsonTest */) {
type = null;
this.current.expects.push(function() {
- var jsonBody = JSON.parse(self.currentRequestFinished.body);
+ var jsonBody = JSON.parse(self.current.response.body);
// Use given path to check deep objects
if(path) {
_.each(path.split('.'), function(segment) {
@@ -357,7 +379,7 @@ Frisby.prototype.expectJSON = function(jsonTest) {
type = null;
this.current.expects.push(function() {
- var jsonBody = JSON.parse(self.currentRequestFinished.body);
+ var jsonBody = JSON.parse(self.current.response.body);
// Use given path to check deep objects
if(path) {
_.each(path.split('.'), function(segment) {
@@ -469,7 +491,7 @@ Frisby.prototype.inspectStatus = function() {
Frisby.prototype.after = function(cb) {
var self = this;
this.current.after.push(function() {
- cb.call(this, self.currentRequestFinished.err, self.currentRequestFinished.res, self.currentRequestFinished.body);
+ cb.call(this, self.current.response.error, self.currentRequestFinished.res, self.current.response.body);
});
return this;
};
@@ -479,18 +501,49 @@ Frisby.prototype.after = function(cb) {
Frisby.prototype.afterJSON = function(cb) {
var self = this;
this.current.after.push(function() {
- var bodyJSON = JSON.parse(self.currentRequestFinished.body);
+ var bodyJSON = JSON.parse(self.current.response.body);
cb.call(this, bodyJSON);
});
return this;
};
+//
+// Methods to manually set parts of the response for matcher testing
+//
+
+// Set response from JSON object
+Frisby.prototype.setResponseJSON = function(json) {
+ this.currentRequestFinished = true;
+ this.current.response.body = JSON.stringify(json);
+ return json;
+};
+
+// Set raw response body
+Frisby.prototype.setResponseBody = function(body) {
+ this.currentRequestFinished = true;
+ this.current.response.body = body;
+ return body;
+};
+
+// Set response headers
+Frisby.prototype.setResponseHeaders = function(/* array */ headers) {
+ this.current.response.headers = headers;
+ return headers;
+};
+
+// Set single response header by key with specified value
+Frisby.prototype.setResponseHeader = function(key, value) {
+ this.current.response.headers[key.toLowerCase()] = value.toLowerCase();
+ return this.current.response.headers[key.toLowerCase()];
+};
+
//
// Toss (Run the current Frisby test)
//
Frisby.prototype.toss = function() {
var self = this;
+
// Assemble all Jasmine tests and RUN them!
describe(self.current.describe, function() {
@@ -578,6 +631,14 @@ beforeEach(function() {
this.message = function() {
return "Actual JSON did not match expected";
}
+
+ if(typeof expected !== "object") {
+ this.message = function() {
+ return "Expected valid JavaScript object to be given, got " + typeof expected;
+ }
+ return false;
+ }
+
return _jsonContains(this.actual, expected);
},
toContainJsonTypes: function(expected) {
@@ -698,4 +759,4 @@ exports.toss = exports.create;
// Public methods and properties
exports.globalSetup = globalSetup;
-exports.version = '0.2.3';
+exports.version = '0.2.5';
View
@@ -1,6 +1,6 @@
{
"name" : "frisby"
-, "version" : "0.2.3"
+, "version" : "0.2.5"
, "description" : "REST API Endpoint Testing built on Jasmine"
, "homepage" : ""
, "author" : "Vance Lucas <vance@vancelucas.com>"
@@ -0,0 +1,83 @@
+frisby = require('../lib/frisby');
+
+
+describe('Frisby matchers', function() {
+
+ it('toContainJSON should match exactly', function() {
+ // Set fake JSON body
+ var testJson = {
+ test_str: "I am a string!",
+ test_int: 42,
+ test_float: 42.42
+ };
+
+ // Expectation
+ expect(testJson).toContainJson({
+ test_str: "I am a string!",
+ test_int: 42,
+ test_float: 42.42
+ });
+ });
+
+
+ it('toContainJSON should match callbacks', function() {
+ // Set fake JSON body
+ var testJson = {
+ test_str: "I am a string!",
+ test_int: 42,
+ test_float: 42.42
+ };
+
+ // Expectation
+ expect(testJson).toContainJson({
+ test_str: "I am a string!",
+ test_int: function(val) { expect(val).toMatch(/\d+/); }
+ });
+ });
+
+
+ it('toContainJSON should not match with undefined variable', function() {
+ // Set fake JSON body
+ var testJson = {
+ test_str: "I am a string!",
+ test_int: 42,
+ test_float: 42.42
+ };
+
+ // Expectation
+ expect(testJson).not.toContainJson(undefined);
+ });
+
+
+ it('JSONTypes should match String', function() {
+ // Set fake JSON body
+ var testJson = {
+ test_str: "I am a string!",
+ test_int: 42,
+ test_float: 42.42
+ };
+
+ // Expectation
+ expect(testJson).toContainJsonTypes({
+ test_str: String,
+ test_int: Number
+ });
+ });
+
+
+ it('JSONTypes should match String', function() {
+ // Set fake JSON body
+ var testJson = {
+ test_str: "I am a string!",
+ test_int: 42,
+ test_float: 42.42
+ };
+
+ // Expectation
+ expect(testJson).toContainJsonTypes({
+ test_str: String,
+ test_int: Number
+ });
+ });
+
+});
View
@@ -0,0 +1,53 @@
+frisby = require('../lib/frisby');
+
+
+describe('Frisby object setup', function() {
+
+ it('global setup should be empty', function() {
+ expect({
+ request: {
+ headers: {}
+ }
+ }).toEqual(frisby.globalSetup());
+ })
+
+ it('should be empty on creation', function() {
+ var f1 = frisby.create('test 1');
+
+ expect({
+ request: {
+ headers: {}
+ }
+ }).toEqual(f1._setup);
+ });
+
+ it('should be independent of other Frisby objects', function() {
+ var f1 = frisby.create('test 1');
+ var f2 = frisby.create('test 2');
+
+ // Equal setup
+ expect(f1._setup).toEqual(f2._setup);
+
+ // Different describe statements
+ expect(f1.current.describe).not.toEqual(f2.current.describe);
+
+ // Add header only to f1
+ f1.setup({
+ request: {
+ headers: {
+ 'Accept': 'application/json'
+ }
+ }
+ });
+ f2.setup({
+ request: {
+ headers: {
+ 'Accept': 'application/x-json'
+ }
+ }
+ });
+
+ // Different setup
+ expect(f1.setup()).not.toEqual(f2.setup());
+ });
+});

0 comments on commit 203abea

Please sign in to comment.