vlucas / frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.

Home Page:http://frisbyjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Header value is splitted in array

gaelreyrol opened this issue · comments

Hi,

Complex header values that contains commas such as Link can't be actually asserted with frisby because the header value for some reason is splitted by comma (this change comes from af834ba and is undocumented).

For example if my header value is this:

</api/resources?currentPage=1&perPage=10>; rel="first",</api/resources?currentPage=1&perPage=10>; rel="prev",</api/resources?currentPage=1&perPage=10>; rel="self"

The value is splitted in an Array of 3 items:

[
    "</api/resources?currentPage=1&perPage=10>; rel=\"first\"",
    "</api/resources?currentPage=1&perPage=10>; rel=\"prev\"",
    "</api/resources?currentPage=1&perPage=10>; rel=\"self\""
]

Which makes it impossible to assert with:

expect('header', 'link', '</api/resources?currentPage=1&perPage=10>; rel="first",</api/resources?currentPage=1&perPage=10>; rel="prev",</api/resources?currentPage=1&perPage=10>; rel="self"')

And produces an irrelevant error:

Header value '</api/resources?currentPage=1&perPage=10>; rel="first",</api/resources?currentPage=1&perPage=10>; rel="prev",</api/resources?currentPage=1&perPage=10>; rel="self"' did not match for header 'link': '</api/resources?currentPage=1&perPage=10>; rel="first",</resources?currentPage=1&perPage=10>; rel="prev",</api/resources?currentPage=1&perPage=10>; rel="self"

If we remove the split(/, */) and rearrange the assertion, the error is resolved and the assertion is correctly verified. But it will break array header assertions so may be the solution is to be able to give an array to the expected header value to verify that each value is available.

It would look like this:

expect('header', 'link', [
    '</api/resources?currentPage=1&perPage=10>; rel="first"',
    '</api/resources?currentPage=1&perPage=10>; rel="prev"',
    '</api/resources?currentPage=1&perPage=10>; rel="self"',
])

Thanks for reading,
Hope it will help !

Regards

@gaelreyrol

For comma-separated list, the following will be helpful.

https://stackoverflow.com/questions/3096888/standard-for-adding-multiple-values-of-a-single-http-header-to-a-request-or-resp

This specification continues from old version frisby.

#477 is change on node-fetch(http-client) side.

It is exactly what I am doing but the only way for me to test each part is to do something like this:

.expect(
    'header',
    'link',
    '</api/resources?currentPage=1&perPage=10>; rel="first"'
)
.expect(
    'header',
    'link',
    '</api/resources?currentPage=1&perPage=10>; rel="prev"'
)
.expect(
    'header',
    'link',
    '</api/resources?currentPage=1&perPage=10>; rel="self"'
)
.expect(
    'header',
    'link',
    '</api/resources?currentPage=1&perPage=10>; rel="next"'
)
.expect(
    'header',
    'link',
    '</api/resources?currentPage=1&perPage=10>; rel="last"'
)

Which is not convenient...

For simple header value match, you can do the following:

.then(res => {
  expect(res.headers.get('link')).toBe('</api/resources?currentPage=1&perPage=10>; rel="first",</api/resources?currentPage=1&perPage=10>; rel="prev",</api/resources?currentPage=1&perPage=10>; rel="self"');
})

ref. :
https://github.com/vlucas/frisby#using-jasmine-matchers-directly

Or you can define Custom Expect Handlers.

Well okay my bad ... I was stuck on the fact that I had to use expect('header', 'link', ...).

Sorry for the inconvenience.