Search code examples
insomnia

Using Insomnia can you validate attributes in the response header?


Using Insomnia can you validate attributes in the response header? For example can date or server or custom attributes be evaluated?

The following code easily evaluates the response status.

    // Validate HTTP Response code
    const response1 = await insomnia.send();
    expect(response1.status).to.equal(401);

The following code parses and evaluates JSON response data

    // Parse JSON data from response and gather relevant data
    const jsonData = JSON.parse(response1.data);
    const message = (jsonData.message);
    const reason = (jsonData.reason);
    expect (reason).to.contain('lost');

How can I evaluate the header of the response to evaluate keyword/value pairs within it?

I have tried several ways to retrieve the header and its data but it is always undefined.

    // Parse response header
    const header = (response1.header);
    const server = (header.server);
    expect (server).to.equal('testServer');

Solution

  • I have found the following approach to allow for working with keywords in the header. The trick was finding that the header is called headers in the response and how to work with keywords that contain a hyphen.

    // Analyze response header
    const headers = response1.headers;
    expect (headers.date).to.contain('2024');
    expect (headers['content-type']).to.contain('application/json');