From 922118579c03e75396f1c69d059a16fb8b1e079e Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Thu, 13 Aug 2026 02:40:23 +0800 Subject: [PATCH] fix(fleetops): assert document responses as documents, not JSON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Labels/Render Label streams a PDF, and the collection-level script asserts `to.be.json` on every response. It failed on the first byte: expected response body to be a valid json but got error Unexpected token '%' at 1:1 That is %PDF. The request itself answered 200 with 18 KB of valid PDF — the assertion was wrong, not the endpoint. It was the only remaining failure in an otherwise 211/211 run. Branches on CONTENT TYPE rather than request name, so a future document endpoint is covered without another exception and a JSON endpoint that unexpectedly started returning a document would still be caught. Documents are still asserted, not excused: expected status, and a non-empty body. Same shape as the Download File fix in the Core API collection, which streams a file for the same reason. Co-Authored-By: Claude Opus 5 --- .../Fleetbase API/.resources/definition.yaml | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/postman/collections/Fleetbase API/.resources/definition.yaml b/postman/collections/Fleetbase API/.resources/definition.yaml index 903e73f..4d7e587 100644 --- a/postman/collections/Fleetbase API/.resources/definition.yaml +++ b/postman/collections/Fleetbase API/.resources/definition.yaml @@ -43,16 +43,26 @@ variables: scripts: - type: http:afterResponse code: | - // Check request method and perform appropriate tests - if (pm.request.method === "POST") { - pm.test(`Successful ${pm.info.requestName} request`, function () { - pm.expect(pm.response.code).to.be.oneOf([200, 201, 202]); - pm.expect(pm.response).to.be.withBody; - pm.expect(pm.response).to.be.json; + // Some endpoints answer with a document rather than JSON — Labels/Render Label + // streams a PDF. Asserting `to.be.json` on those fails on the first byte + // ("Unexpected token '%'" is %PDF), which says nothing useful about the endpoint. + // + // Detected by content type rather than by request name, so a new document endpoint + // is covered without another exception, and a JSON endpoint that unexpectedly + // started returning a document would still be caught. + const contentType = pm.response.headers.get('Content-Type') || ''; + const isDocument = /^(application\/pdf|image\/|application\/octet-stream|text\/csv)/i.test(contentType); + const expected = pm.request.method === "POST" ? [200, 201, 202] : [200, 201, 202, 204]; + + if (isDocument) { + // Still asserted, not excused: a document response must carry actual bytes. + pm.test(`Successful ${pm.info.requestName} request returns a document body`, function () { + pm.expect(pm.response.code, `got HTTP ${pm.response.code}`).to.be.oneOf(expected); + pm.expect(pm.response.responseSize, 'document response is empty').to.be.above(0); }); - } else if (pm.request.method != "POST") { + } else { pm.test(`Successful ${pm.info.requestName} request and response must be valid and have a body`, function () { - pm.expect(pm.response.code, `got HTTP ${pm.response.code}`).to.be.oneOf([200, 201, 202, 204]); + pm.expect(pm.response.code, `got HTTP ${pm.response.code}`).to.be.oneOf(expected); pm.expect(pm.response).to.be.withBody; pm.expect(pm.response).to.be.json; });