Why Use Response Snapshots?
Speed
Tests run instantly by reading from disk instead of making network calls
Reliability
Tests pass consistently without API availability or rate limiting issues
Offline Testing
Run your test suite anywhere, even without internet access
CI/CD Friendly
No secrets or API credentials needed in your build pipeline
How It Works
Breakdance provides twoDelegatingHandler classes for the snapshot workflow:
| Handler | Purpose |
|---|---|
ResponseSnapshotCaptureHandler | Makes real HTTP calls and saves responses to files |
ResponseSnapshotReplayHandler | Reads responses from files instead of making HTTP calls |
- Run tests once with the Capture handler to record real responses
- Check captured response files into source control
- Run tests with the Replay handler for fast, deterministic execution
Quick Start
1
Install the package
2
Create a folder for snapshot files
3
Capture responses
Run your tests with the Capture handler to record real API responses.
4
Switch to Replay handler
Change to the Replay handler for subsequent test runs.
Capturing Responses
UseResponseSnapshotCaptureHandler to capture real API responses:
ApiCaptureTests.cs
Replaying Responses
Switch toResponseSnapshotReplayHandler to serve captured responses:
ApiTests.cs
The Replay handler throws
InvalidOperationException if a snapshot file doesn’t exist.
This helps catch missing captures early.File Organization
Responses are organized by host and path:| Request URL | File Path |
|---|---|
https://api.example.com/users | ResponseSnapshots/api.example.com/users.json |
https://api.example.com/users/123 | ResponseSnapshots/api.example.com/users/123.json |
https://api.example.com/v1/items?type=active | ResponseSnapshots/api.example.com/v1/items.json |
application/json→.jsonapplication/xmlortext/xml→.xmltext/html→.html- Others →
.txt
Using with DotHttpTestBase
Combine response snapshots with request snapshots (.http files) for comprehensive testing:
Capture/Replay Pattern
A common pattern is to have separate capture and test modes:Environment-Specific Responses
Capture responses per environment for different test scenarios:Testing Edge Cases
Create response files manually to test specific scenarios:ResponseSnapshots/error-scenarios/api.example.com/users.json
Best Practices
Check response files into source control
Check response files into source control
Response snapshot files should be committed alongside your tests. This ensures everyone
on the team and your CI pipeline can run tests without API access.
Refresh snapshots periodically
Refresh snapshots periodically
API responses change over time. Set a reminder to refresh your captured responses
monthly or when API versions change.
Don't capture sensitive data
Don't capture sensitive data
Be careful not to capture responses containing secrets, tokens, or personal data.
Either sanitize the response files or use test accounts with non-sensitive data.
Use meaningful directory names
Use meaningful directory names
Organize response files by feature or test scenario for easier maintenance:
Handle concurrent writes
Handle concurrent writes
ResponseSnapshotCaptureHandler includes retry logic for file locking when
multiple tests run in parallel. No additional configuration needed.Troubleshooting
Response snapshot file not found
Response snapshot file not found
The Replay handler throws
InvalidOperationException with the expected file path.
Check:- The file exists at the expected path
- The URL matches exactly (including query string handling)
- The ResponseSnapshots folder is copied to test output (set Copy to Output Directory)
Wrong Content-Type
Wrong Content-Type
The handler sets Content-Type based on file extension. If you need a specific
Content-Type, ensure your file has the correct extension (
.json, .xml, etc.).Tests pass locally but fail in CI
Tests pass locally but fail in CI
Ensure snapshot files are included in your test project:
Migration from TestCache Classes
If you’re upgrading from an earlier version of Breakdance, the following classes have been renamed:| Old Name | New Name |
|---|---|
TestCacheWriteDelegatingHandler | ResponseSnapshotCaptureHandler |
TestCacheReadDelegatingHandler | ResponseSnapshotReplayHandler |
TestCacheDelegatingHandlerBase | ResponseSnapshotHandlerBase |
ResponseFilesPath property | ResponseSnapshotsPath property |
[Obsolete] and will be removed in a future major version.