Test content negotiation: Accept, Content-Type, 406 and 415
Two headers decide the format of an HTTP exchange. Content-Type says what the client sent. Accept says what it wants back. Most APIs handle the happy path, JSON in and JSON out, and do something surprising with everything else.
Content-Type on the request
Take one POST that works with JSON, and duplicate it with other content types. The body stays JSON text; only the header changes:
| Request | Content-Type | Expected |
|---|---|---|
| Create order (JSON) | application/json | 201 |
| Create order (JSON with charset) | application/json; charset=utf-8 | 201 |
| Create order (text/plain) | text/plain | 415 |
| Create order (XML) | application/xml | 415 |
| Create order (form) | application/x-www-form-urlencoded | 415 |
| Create order (no type) | header removed | 415 or 400 |
Requestarium sets Content-Type: application/json by default on new requests, so remove it explicitly in the last case. The quickest way is a pre-request script on that one request:
pm.request.headers.remove('Content-Type');Then:
pm.test('unsupported content type is refused', () => {
pm.expect(pm.response.code).to.be.oneOf([415, 400]);
});
pm.test('nothing was created', () => {
pm.expect(pm.response.code).to.not.equal(201);
});Why text/plain matters
Browsers send text/plain, application/x-www-form-urlencoded and multipart/form-data cross-origin without a CORS preflight. If your API accepts JSON sent as text/plain, a malicious page can make a logged-in user's browser call it with cookies, without the preflight that would have blocked it. Refusing those content types on JSON endpoints closes that hole. See testing CORS for the browser side.
Accept on the response
GET {{baseUrl}}/orders/{{orderId}}
Accept: {{accept}}accept,expectedStatus,expectedType
application/json,200,application/json
*/*,200,application/json
application/*,200,application/json
"application/json, text/plain;q=0.5",200,application/json
text/csv,406,
application/xml,406,const accept = pm.iterationData.get('accept');
pm.test(`Accept ${accept}: status`, () => pm.response.to.have.status(Number(pm.iterationData.get('expectedStatus'))));
const type = pm.iterationData.get('expectedType');
if (type) {
pm.test(`Accept ${accept}: content type`, () => pm.expect(pm.response.headers.get('Content-Type')).to.include(type));
}Plenty of APIs ignore Accept and always answer JSON, which is a valid choice. Then the text/csv row expects 200 with application/json, and you've documented that the API doesn't negotiate. What you want to catch is the in-between: an endpoint that answers 500 because a serializer for text/csv isn't registered.
Endpoints that do several formats
If an export endpoint offers CSV and JSON, test that the format follows Accept and that the body matches the header:
if (pm.response.headers.get('Content-Type').includes('text/csv')) {
pm.test('CSV body', () => pm.expect(pm.response.text().split('\n')[0]).to.include(','));
}See testing CSV export endpoints.
Vendor types and versions
Some APIs version through the media type, for example Accept: application/vnd.example.v2+json. Then test:
- the current version returns
v2-shaped bodies; - an old version returns either the old shape or
406, as documented; - a version that doesn't exist returns
406, not the latest version silently.
Errors use your format
A 406 or 415 is still an error response, and clients parse it like any other. Check it's JSON in your usual error shape and not a framework's HTML default.