Socket
Socket
Sign inDemoInstall

chakram

Package Overview
Dependencies
52
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.3 to 0.1.0

examples/dweet.js

32

lib/chakram.js

@@ -11,3 +11,3 @@ /**

methods = require('./methods.js'),
chakramMatchers = require("./matchers/index.js"),
chakramMatchers = require("./assertions/index.js"),
chaiAsPromised,

@@ -74,5 +74,5 @@ chaiShallow = require('chai-shallow-deep-equal'),

/**
Chakram assertation constructor. Extends chai's extend method with Chakram's HTTP matchers.
Please see {@link http://chaijs.com/api/bdd/ chai's API documentation} for details on the default chai matchers and the {@link ChakramExpectation} documentation for the Chakram HTTP matchers.
@param {*} value - The variable to run assertations on, can be a {@link ChakramResponse} promise
Chakram assertion constructor. Extends chai's extend method with Chakram's HTTP assertions.
Please see {@link http://chaijs.com/api/bdd/ chai's API documentation} for details on the default chai assertions and the {@link ChakramExpectation} documentation for the Chakram HTTP assertions.
@param {*} value - The variable to run assertions on, can be a {@link ChakramResponse} promise
@returns {chakram-expectation} A Chakram expectation object

@@ -82,6 +82,6 @@ @alias module:chakram.expect

var expect = chakram.expect;
it("should support chakram and chai expectations", function () {
var chakramRequest = chakram.get("http://google.com");
it("should support chakram and chai assertions", function () {
var google = chakram.get("http://google.com");
expect(true).to.be.true;
expect(chakramRequest).to.have.status(200);
expect(google).to.have.status(200);
expect(1).to.be.below(10);

@@ -106,3 +106,3 @@ expect("teststring").to.be.a('string');

/**
Returns a promise which is fulfilled once all promises in the array argument are fulfilled.
Returns a promise which is fulfilled once all promises in the provided array are fulfilled.
Identical to {@link https://github.com/kriskowal/q/wiki/API-Reference#promiseall Q.all}.

@@ -117,4 +117,4 @@ @method

/**
Returns a promise which is fulfilled once all promises in the array argument are fulfilled.
Similar to {@link https://github.com/kriskowal/q/wiki/API-Reference#promiseall Q.all}, however, instead of being fulfilled with an array containing the fulfillment value of each promise, it is fulfilled with the fulfillment value of the last promise in the array argument. This allows chaining of HTTP calls.
Returns a promise which is fulfilled once all promises in the provided array are fulfilled.
Similar to {@link https://github.com/kriskowal/q/wiki/API-Reference#promiseall Q.all}, however, instead of being fulfilled with an array containing the fulfillment value of each promise, it is fulfilled with the fulfillment value of the last promise in the provided array. This allows chaining of HTTP calls.
@param {Promise[]} promiseArray - An array of promises to wait for

@@ -125,6 +125,6 @@ @returns {Promise}

it("should support grouping multiple tests", function () {
var request = chakram.get("http://httpbin.org/get");
var response = chakram.get("http://httpbin.org/get");
return chakram.waitFor([
expect(request).to.have.status(200),
expect(request).not.to.have.status(404)
expect(response).to.have.status(200),
expect(response).not.to.have.status(404)
]);

@@ -148,5 +148,5 @@ });

it("should support auto waiting for tests", function() {
var request = chakram.get("http://httpbin.org/get");
expect(request).to.have.status(200);
expect(request).not.to.have.status(404);
var response = chakram.get("http://httpbin.org/get");
expect(response).to.have.status(200);
expect(response).not.to.have.status(404);
return chakram.wait();

@@ -153,0 +153,0 @@ });

{
"name": "chakram",
"version": "0.0.3",
"description": "Chakram is a REST API testing framework designed to perform end to end tests on JSON REST endpoints. The library offers a BDD testing style and fully exploits javascript promises",
"version": "0.1.0",
"description": "Chakram is an API testing framework designed to test JSON REST endpoints. The library offers a BDD testing style and fully exploits javascript promises",
"main": "lib/chakram.js",

@@ -18,9 +18,10 @@ "license": {

"keywords": [
"testing",
"api",
"test",
"API",
"REST",
"HTTP",
"JSON",
"mocha",
"chai",
"bdd"
"BDD"
],

@@ -47,5 +48,5 @@ "dependencies": {

"pretest": "npm install",
"test": "istanbul cover _mocha test/* test/**/*",
"test": "istanbul cover _mocha test/* test/**/* examples/*",
"predoc": "npm install",
"doc": "jsdoc -t node_modules/jaguarjs-jsdoc -c conf.json -R README.md -r lib"
"doc": "jsdoc -t node_modules/jaguarjs-jsdoc -R README.md -r lib"
},

@@ -52,0 +53,0 @@ "engines": {

@@ -5,5 +5,5 @@ # Chakram

Chakram is a REST API testing framework designed to perform end to end tests on JSON REST endpoints. The library offers a BDD testing style and fully exploits javascript promises - the resulting tests are simple, clear and expressive. The library is built on [node.js](https://nodejs.org/), [mocha](http://mochajs.org/) and [chai](http://chaijs.com/).
Chakram is an API testing framework designed to perform end to end tests on JSON REST endpoints. The library offers a BDD testing style and fully exploits javascript promises - the resulting tests are simple, clear and expressive. The library is built on [node.js](https://nodejs.org/), [mocha](http://mochajs.org/) and [chai](http://chaijs.com/).
More information is available in the [library's documentation](http://dareid.github.io/chakram/) and its [own tests](https://github.com/dareid/chakram/tree/master/test) which demonstrate all of Chakram's functionality.
More information is available in the [library's documentation](http://dareid.github.io/chakram/) and its [own tests](https://github.com/dareid/chakram/tree/master/test) which demonstrate all of Chakram's capabilities. Example API tests of publically accessable APIs are available in the [examples directory](https://github.com/dareid/chakram/tree/master/examples).

@@ -17,17 +17,6 @@ ## Install Chakram

## Introduction
This test tool builds on top of the mocha testing framework, as such, the tests follow the typical BDD style. As this library focuses on testing REST API endpoints, the tests are naturally asynchronous. The library is written to fully exploit javascript promises. Mocha has native support for promises, simply return a promise from 'it' and the test runner will wait until the promise has been fulfilled.
Chakram builds on top of the mocha testing framework, as such, the tests follow the [BDD style](http://mochajs.org/#getting-started). As this library focuses on testing REST APIs, the tests are naturally asynchronous. Mocha has [native support for promises](http://mochajs.org/#asynchronous-code) which Chakram exploits. All requests and expectations return promises which fulfill to [Chakram response objects](http://dareid.github.io/chakram/global.html#ChakramResponse).
```js
var chakram = require('chakram'),
expect = chakram.expect;
The example below demonstrates a GET request and an assertion of the returned status code. The assertion of the status code returns a promise which is fulfilled once the status code has been checked.
describe("Minimal example", function() {
it("should provide a simple async testing framework", function () {
var request = chakram.get("http://httpbin.org/get");
return expect(request).to.have.status(200);
});
});
```
The example above demonstrates a GET request and an assertation of the returned status code. The example 'it' returns a promise which is fulfilled once the status code has been checked. Both the request and the expect statement return promises, it is important to return the correct promise otherwise the test may finish before the status code is checked. To help, chakram includes a wait method, which will return a promise which will be fulfilled once all assertations have been fulfilled. Below is a similar test rewritten using the wait method. In addition, the library will fail any tests which finish before all the assertations have fulfilled.
```js

@@ -39,10 +28,7 @@ var chakram = require('chakram'),

it("should provide a simple async testing framework", function () {
var request = chakram.get("http://httpbin.org/get");
expect(request).to.have.status(200);
expect(request).not.to.have.header('non-existing-header');
return chakram.wait();
var response = chakram.get("http://httpbin.org/get");
return expect(response).to.have.status(200);
});
});
```
Below is a larger example testing the [Random User Generator API](https://randomuser.me/).

@@ -55,15 +41,15 @@

describe("Random User API", function() {
var apiRequest;
var apiResponse;
before(function () {
apiRequest = chakram.get("http://api.randomuser.me/?gender=female");
apiResponse = chakram.get("http://api.randomuser.me/?gender=female");
});
it("should return 200 on success", function () {
return expect(apiRequest).to.have.status(200);
return expect(apiResponse).to.have.status(200);
});
it("should return content type and server headers", function () {
expect(apiRequest).to.have.header("server");
expect(apiRequest).to.have.header("content-type", /json/);
expect(apiResponse).to.have.header("server");
expect(apiResponse).to.have.header("content-type", /json/);
return chakram.wait();

@@ -73,3 +59,3 @@ });

it("should include email, username, password and phone number", function () {
return expect(apiRequest).to.have.schema('results[0].user', {
return expect(apiResponse).to.have.schema('results[0].user', {
"required": [

@@ -85,11 +71,18 @@ "email",

it("should return a female user", function () {
return expect(apiRequest).to.have.json('results[0].user.gender', 'female');
return expect(apiResponse).to.have.json('results[0].user.gender', 'female');
});
it("should return a valid email address", function () {
return expect(apiResponse).to.have.json(function(json) {
var email = json.results[0].user.email;
expect(/\S+@\S+\.\S+/.test(email)).to.be.true;
});
});
it("should return a single random user", function () {
return expect(apiRequest).to.have.schema('results', {minItems: 1, maxItems: 1});
return expect(apiResponse).to.have.schema('results', {minItems: 1, maxItems: 1});
});
it("should not be gzip compressed", function () {
return expect(apiRequest).not.to.be.encoded.with.gzip;
return expect(apiResponse).not.to.be.encoded.with.gzip;
});

@@ -99,7 +92,7 @@

this.timeout(10000);
var multipleRequests = [];
var multipleResponses = [];
for(var ct = 0; ct < 5; ct++) {
multipleRequests.push(chakram.get("http://api.randomuser.me/?gender=female"));
multipleResponses.push(chakram.get("http://api.randomuser.me/?gender=female"));
}
return chakram.all(multipleRequests).then(function(responses) {
return chakram.all(multipleResponses).then(function(responses) {
var returnedUsernames = responses.map(function(response) {

@@ -117,3 +110,19 @@ return response.body.results[0].user.username;

```
It is important that tests wait for all requests and assertions to be completed. To help, chakram includes a wait method, this returns a promise which will be fulfilled once all assertions have been performed. In addition, Chakram will fail any tests which do not wait for assertions to complete. Below is a test using the wait method.
```js
var chakram = require('chakram'),
expect = chakram.expect;
describe("Minimal example", function() {
it("should provide a simple async testing framework", function () {
var response = chakram.get("http://httpbin.org/get");
expect(response).to.have.status(200);
expect(response).not.to.have.header('non-existing-header');
return chakram.wait();
});
});
```
## Run Tests

@@ -120,0 +129,0 @@ To run Chakram tests, install the Mocha testing framework globally (or as a dev dependancy):

@@ -5,17 +5,17 @@ var chakram = require('./../lib/chakram.js'),

describe("Documentation examples", function() {
it("should support chakram and chai expectations", function () {
var chakramRequest = chakram.get("http://google.com");
it("should support chakram and chai assertions", function () {
var google = chakram.get("http://google.com");
expect(true).to.be.true;
expect(chakramRequest).to.have.status(200);
expect(google).to.have.status(200);
expect(1).to.be.below(10);
expect("teststring").to.be.a('string');
return chakram.wait();
});
});
it("should support grouping multiple tests", function () {
var request = chakram.get("http://httpbin.org/get");
var response = chakram.get("http://httpbin.org/get");
return chakram.waitFor([
expect(request).to.have.status(200),
expect(request).not.to.have.status(404)
expect(response).to.have.status(200),
expect(response).not.to.have.status(404)
]);

@@ -25,29 +25,40 @@ });

it("should support auto waiting for tests", function() {
var request = chakram.get("http://httpbin.org/get");
expect(request).to.have.status(200);
expect(request).not.to.have.status(404);
var response = chakram.get("http://httpbin.org/get");
expect(response).to.have.status(200);
expect(response).not.to.have.status(404);
return chakram.wait();
});
it("should detect deflate compression", function () {
var deflate = chakram.get("http://httpbin.org/deflate");
return expect(deflate).to.be.encoded.with.deflate;
});
it("should detect gzip compression", function () {
var gzip = chakram.get("http://httpbin.org/gzip");
return expect(gzip).to.be.encoded.with.gzip;
});
it("should allow checking of HTTP cookies", function () {
var request = chakram.get("http://httpbin.org/cookies/set?chakram=testval");
return chakram.waitFor([
expect(request).to.have.cookie('chakram'),
expect(request).to.have.cookie('chakram', 'testval'),
expect(request).to.have.cookie('chakram', /val/)
]);
var response = chakram.get("http://httpbin.org/cookies/set?chakram=testval");
expect(response).to.have.cookie('chakram');
expect(response).to.have.cookie('chakram', 'testval');
expect(response).to.have.cookie('chakram', /val/);
return chakram.wait();
});
it("should allow checking of HTTP headers", function () {
var request = chakram.get("http://httpbin.org/get");
return chakram.waitFor([
expect(request).to.have.header('content-type'),
expect(request).to.have.header('content-type', 'application/json'),
expect(request).to.have.header('content-type', /json/)
]);
var response = chakram.get("http://httpbin.org/get");
expect(response).to.have.header('content-type');
expect(response).to.have.header('content-type', 'application/json');
expect(response).to.have.header('content-type', /json/);
expect(response).to.have.header('content-type', function(contentType) {
expect(contentType).to.equal('application/json');
});
return chakram.wait();
});
it("should allow checking of JSON return bodies", function () {
var request = chakram.get("http://httpbin.org/get");
expect(request).to.include.json({
var response = chakram.get("http://httpbin.org/get");
expect(response).to.include.json({
url: "http://httpbin.org/get",

@@ -58,15 +69,13 @@ headers: {

});
expect(request).to.have.json('url', "http://httpbin.org/get");
expect(response).to.have.json('url', "http://httpbin.org/get");
expect(response).to.have.json('url', function (url) {
expect(url).to.equal("http://httpbin.org/get");
});
return chakram.wait();
});
it("should allow checking of the response's status code", function () {
var request = chakram.get("http://httpbin.org/get");
return expect(request).to.have.status(200);
});
it("should check that the returned JSON object satisifies a JSON schema", function () {
var request = chakram.get("http://httpbin.org/get");
expect(request).to.have.schema('headers', {"required": ["Host", "Accept"]});
expect(request).to.have.schema({
var response = chakram.get("http://httpbin.org/get");
expect(response).to.have.schema('headers', {"required": ["Host", "Accept"]});
expect(response).to.have.schema({
"type": "object",

@@ -84,13 +93,8 @@ properties: {

});
it("should detect deflate compression", function () {
var deflate = chakram.get("http://httpbin.org/deflate");
return expect(deflate).to.be.encoded.with.deflate;
it("should allow checking of the response's status code", function () {
var response = chakram.get("http://httpbin.org/get");
return expect(response).to.have.status(200);
});
it("should detect gzip compression", function () {
var gzip = chakram.get("http://httpbin.org/gzip");
return expect(gzip).to.be.encoded.with.gzip;
});
});

@@ -5,8 +5,8 @@ var chakram = require('./../lib/chakram.js'),

describe("Methods", function() {
var testWriteMethods = function (testMethod, testUrl) {
it("should support JSON requests", function () {
var json = {"num": 2,"str": "test"};
var post = testMethod(testUrl, json);
return post.then(function(resp) {
var response = testMethod(testUrl, json);
return response.then(function(resp) {
expect(resp.body).to.be.an('object');

@@ -20,4 +20,4 @@ expect(resp.body.json).to.deep.equal(json);

var stringPost = "testing with a string post";
var post = testMethod(testUrl, stringPost, {json:false});
return post.then(function(resp) {
var response = testMethod(testUrl, stringPost, {json:false});
return response.then(function(resp) {
expect(resp.body).to.be.a('string');

@@ -28,2 +28,12 @@ expect(JSON.parse(resp.body).data).to.be.equal(stringPost);

});
it("should support sending custom headers", function () {
var customHeaders = {
"Token": "dummy token value"
};
var response = testMethod(testUrl, {}, {
headers: customHeaders
});
return expect(response).to.include.json('headers', customHeaders);
});
};

@@ -30,0 +40,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc