Module: chakram-expectation

chakram-expectation

Extends the chai.expect object with additional HTTP matchers.

Methods

staticmodule:chakram-expectation.cookie(name, value)

assertions/cookie.js, line 15
Either checks that a cookie exists or ensures the cookie matches a given value
Name Type Description
name String checks a cookie with this name exists
value String | RegExp optional if specified, checks the cookie matches the given string or regular expression
Example
it("should allow checking of HTTP cookies", function () {
    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();
});

staticmodule:chakram-expectation.deflate()

assertions/compression.js, line 29
Checks that the response is deflate compressed
Example
it("should detect deflate compression", function () {
        var deflate = chakram.get("http://httpbin.org/deflate");
        return expect(deflate).to.be.encoded.with.deflate;
    });

staticmodule:chakram-expectation.gzip()

assertions/compression.js, line 16
Checks that the response is gzip compressed
Example
it("should detect gzip compression", function () {
        var gzip = chakram.get("http://httpbin.org/gzip");
        return expect(gzip).to.be.encoded.with.gzip;
    });

staticmodule:chakram-expectation.header(name, value)

assertions/header.js, line 19
Either checks that a header exists or ensures the header matches a given value
Name Type Description
name String checks a header with this name exists
value String | RegExp | function optional if specified, checks the header matches the given string or regular expression OR calls the provided function passing the header's value
Example
it("should allow checking of HTTP headers", function () {
    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();
});

staticmodule:chakram-expectation.json(subelement, expectedValue)

assertions/json.js, line 25
Checks the content of a JSON object within the return body. By default this will check the body JSON exactly matches the given object. If the 'comprise' chain element is used, it checks that the object specified is contained within the body JSON. An additional first argument allows sub object checks.
Name Type Description
subelement String optional if specified a subelement of the JSON body is checked, specified using dot notation
expectedValue * | function a JSON serializable object which should match the JSON body or the JSON body's subelement OR a custom function which is called with the JSON body or the JSON body's subelement
Example
it("should allow checking of JSON return bodies", function () {
    var response = chakram.get("http://httpbin.org/get");
    expect(response).to.comprise.of.json({
        url: "http://httpbin.org/get",
        headers: {
            Host: "httpbin.org",
        }
    });
    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();
});

staticmodule:chakram-expectation.responsetime(milliseconds)

assertions/responsetime.js, line 12
Checks the response time of the response is less than or equal to the provided millisecond value.
Name Type Description
milliseconds Number the expected maximum response time in milliseconds
Example
it("should allow checking maximum response time", function () {
    var request = chakram.get("http://httpbin.org/delay/2");
    return expect(request).to.have.responsetime(3000);
});

staticmodule:chakram-expectation.schema(subelement, expectedSchema)

assertions/schema.js, line 28
Checks the schema of the returned JSON object against a provided JSON Schema. This assertion utilizes the brilliant tv4 library. An optional dot notation argument allows a subelement of the JSON object to checked against a JSON schema. Amoungst others, this can confirm types, array lengths, required fields, min and max of numbers and string lengths. For more examples see the test/assertions/schema.js tests.
Name Type Description
subelement String optional if specified a subelement of the JSON body is checked, specified using dot notation
expectedSchema * a JSON schema object which should match the JSON body or the JSON body's subelement. For more details on format see the JSON schema website
Example
it("should check that the returned JSON object satisifies a JSON schema", function () {
    var response = chakram.get("http://httpbin.org/get");
    expect(response).to.have.schema('headers', {"required": ["Host", "Accept"]});
    expect(response).to.have.schema({
        "type": "object",
        properties: {
            url: {
                type: "string"
            },
            headers: {
                type: "object"
            }
        }
    });
    return chakram.wait();
});

staticmodule:chakram-expectation.status(code)

assertions/statuscode.js, line 12
Checks the status code of the response
Name Type Description
code Number the expected status code from the response
Example
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);
});