Before/After API Testing: Compare Bytes, Not Just Objects

📖 1 minute read

When refactoring code that talks to external APIs, how do you know you didn’t break something subtle?

Compare both the parsed response AND the raw wire format:

// Test old implementation
$oldClient = new OldApiClient($config);
$oldParsed = $oldClient->fetchData($params);
$oldRaw = $oldClient->getLastRawResponse();

// Test new implementation  
$newClient = new NewApiClient($config);
$newParsed = $newClient->fetchData($params);
$newRaw = $newClient->getLastRawResponse();

// Compare both
assert($oldParsed == $newParsed);  // Functional behavior
assert($oldRaw === $newRaw);       // Wire-level compatibility

Why both? Because:

  • Parsed objects verify functional correctness
  • Raw responses catch encoding issues, header differences, whitespace handling
  • Some APIs are picky about request formatting even if the parsed result looks identical

This approach caught cases where new code was adding HTTP headers the old code didn’t send, and another where namespace handling differed slightly. Both “worked” but matching exact wire format means safer deployment.

When refactoring integrations, test the bytes on the wire, not just the objects in memory.

Daryle De Silva

VP of Technology

11+ years building and scaling web applications. Writing about what I learn in the trenches.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *