A Successful HTTP Status Still Needs a Valid Warehouse Response
An integration receives HTTP 200 and records an order handover as accepted. The response body is actually an HTML login page. The network request completed, but the application never obtained the acceptance record it expected. For a Shopify 3PL integration, keep transport success and the response contract as separate checks. This standalone example accepts one deliberately narrow contract: an…
An integration confirms an order as accepted when it receives an HTTP 200 status code. However, the response body may contain an HTML login page instead of the expected acceptance record. For a Shopify 3PL integration, it is crucial to treat transport success and the response contract as separate checks. This example focuses on a narrow contract: an application/json response with accepted: true, the expected request reference, and a nonempty receipt identifier.
To validate the response, the function checks the caller's expected reference, HTTP result, declared media type, and parsed payload. The confirmed array stores the acceptance record, which only updates after all checks pass. The function first verifies that the expected reference is a non-empty string. It then checks if the response is successful (HTTP status in the 200-299 range). Next, it verifies that the response content type is application/json. If any of these checks fail, an error is thrown.
The function attempts to parse the response body as JSON. If parsing fails, an error is thrown. If parsing succeeds, it checks if the body is an object, not an array, has the accepted property set to true, the requestRef property matching the expected reference, and a non-empty receiptId property that matches the receiptId. If any of these checks fail, an error is thrown. If all checks pass, the function returns an object containing the requestRef and receiptId.
In the main function, an array of confirmed records is initialized. A record function is defined, which takes a response and an optional expected reference. It calls readAcceptance, passing the response and expected reference, and adds the returned receipt to the confirmed array. Good and invalid responses are created, demonstrating successful and unsuccessful validation. The main function asserts that the confirmed array matches the expected receipt.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.