#What is it? XHRFixtures helps you to load external XHR-Fixtures with jquery to use it with qunit and sinon. It includes internal logics to validate requests or responses, for your tests. It's also possible to autogenerate some tests, or run the tests against a rest-api.
#Features
- Load external .json-fixtures for qunit - sinon
- includes a validation-logic for http response and request objects
- response and request can be used independently
- autogenerated tests are possible
#Usage
At first you need to define some external XHR-Fixtures. This is done by adding a File within your fixture directory. A very Basic Example looks like this:
[
{
"simple fixture": {
"__valid_response": {
"status": 200,
"headers": {
"Content-type": {
"type": "RegExp",
"pattern": "json",
"modifiers": "gim"
}
},
"payload": {
"session": {
"type": "RegExp",
"pattern": "\\w+"
}
}
},
"__valid_request": {
"method": "GET"
},
"request": {
"method": "GET",
"path": "/",
"headers": {
"Host": "www.myhost.com"
},
"payload": ""
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"Payload": "{\"session\": \"3adfjo2i3isakal2\", \"random\": \"3lasklj303829283wkj3\"}"
}
}
]
The library loads the JSON String. When you look at the Response-"Payload" in the example, you will recognize that it contains a JSON-String. The library recognize this also. So if you want to validate the JSON-Object it self you can define a structured validation object for that also. To get a basic understanding of what the library does, look at this simple example:
var fix = xhrfixtures();
fix.loadFixtures(["./test/fixtures/simple.json"], function (err, results) {
var fixture = fix.getFixtures()["simple real live fixture"]; //first one
//validate by integrated validation
fixture.validateResponse();
//validate by given spec
fixture.validateRequest({method: "GET", headers: {"Host": /myhost/}});
//validate by given spec, but expect to test to fail
fixture.validateRequest({method: "GET", headers: {"Host": /myhost/}}, true);
});
The validation object has the structure of the corresponding request/response-object. There for you could use regular expressions to validate the object to test. If you pass an object in the validation, where the corresponding test object defines a string, the library tries to parse the string as a json object and goes on with validation.
String to test:
"Payload": "{\"session\": \"3adfjo2i3isakal2\", \"random\": \"3lasklj303829283wkj3\"}"
and the corresponding validation object could look like this:
"payload": {
"session": {
"type": "RegExp",
"pattern": "\\w+"
}
}
which takes care, that the session-parameter is defined and contains any word-character. The "fix.loadFixtures" - Method loads the JSON File into an array of "xhrfixture"-Objects. You could use these fixtures to test a rest-api by firing the requests and save the response back to the fixture and then validate this. So its possible to fire a bunch of tests, where as the tests are nearly autogenerated.
The code could look like this
fix.loadFixtures(["./test/fixtures/myfixtures.json"], function (err, results) {
var fixtures = fix.getFixtures(); //first one
while (fixture = fixtures.shift()) {
//Fire requests and get response
...
//create the response-Object
...
//set the response object
fixture.setResponse(myResponseObj);
fixture.validateResponse();
}
});
i hope that helps. may be there are some jquery-helpers later on for that. Just check out the test directory to more examples.
#Dependencies
##JQuery That library uses jquery which is licensed under:
Copyright 2011, John Resig Dual licensed under the MIT or GPL Version 2 licenses. http://jquery.org/license
Includes Sizzle.js http://sizzlejs.com/
Copyright 2011, The Dojo Foundation Released under the MIT, BSD, and GPL Licenses.
##QUnit A JavaScript Unit Testing Framework
Copyright (c) 2011 John Resig, Jörn Zaefferer Dual licensed under the MIT (MIT-LICENSE.txt) or GPL (GPL-LICENSE.txt) licenses.
##slide Also the library take advantage of a little script, known from the node.js community with a little correction by myself called "slide". You can find it on Github: https://github.com/isaacs/slide-flow-control
##sinonjs You could use xhrfixtures with that cool library by Christian Johansen (christian@cjohansen.no) It is licensed under the BSD License. You can find it at github und https://github.com/cjohansen/Sinon.JS
#License (The BSD License)
Copyright (c) 2011, Mario Scheliga, mario@sourcegarden.com All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of Mario Scheliga nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.