vlucas / frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.
http://frisbyjs.com
1.52k stars 201 forks source link

How to assert header exists, but don't care about value #329

Closed nodesocket closed 7 years ago

nodesocket commented 8 years ago

How can I assert that a header exists, but I don't care about the value? I.E. only care that the header key exists.

Thanks.

sasikanth commented 8 years ago

Here is a sample for you..

var frisby = require("frisby");

frisby.create("Sample header test case")
    .get("http://httpbin.org/response-headers?key=value")
    .expectHeader("key",'value')  // checks for equality.
    .expectHeaderToMatch("key",'')  // checks if 'key' exists & matches the pattern ''
    .expectHeaderToMatch("key",'lue') // checks if 'key' exists & matches the pattern 'lue'
    .after(function(err,res,body){
        console.log(res.headers);
        expect(res.headers.key).toBeDefined();
        expect(res.headers.otherkey).not.toBeDefined(); 
        expect(res.headers.hasOwnProperty('key')).toBe(true); 
        expect(res.headers.key).toEqual(jasmine.any(String));
        expect(res.headers.key).not.toEqual(jasmine.any(Number));  
        expect(res.headers.key).not.toEqual(jasmine.any(Object)); 
        expect(res.headers.key).toMatch('');
    })  
    .toss();

~ Sasi

nodesocket commented 8 years ago

Thanks. expectHeaderExists('X-Foo-Bar') would be useful. Maybe worth adding in.

vlucas commented 7 years ago

Frisby v2 has a use-case for this. If you only supply a single argument, it only checks for the header's existence: .expect('header', 'X-Foo-bar').