.http file format lets you define HTTP requests in a readable text format. Breakdance’s DotHttp library
provides a runtime and source generator to execute these files as unit tests, with full support for variables,
environments, and request chaining.
Prerequisites
1
Install the package
Add the Breakdance DotHttp package to your test project:
2
Create a .http file
Add a
.http file to your project. Visual Studio and VS Code provide syntax highlighting.Quick Start
Create a test class that inherits fromDotHttpTestBase:
ApiTests.cs
The .http File Format
A.http file contains one or more HTTP requests separated by ###:
api.http
- An optional comment line starting with
#or// - The HTTP method and URL
- Optional headers (key: value format)
- A blank line separating headers from body
- An optional request body
Variables
Variables make your requests dynamic and reusable.Defining Variables
Define variables at the top of your.http file using @variable = value:
Setting Variables in Code
Dynamic Variables
Breakdance supports dynamic variables that generate values at runtime:DateTime Formatting and Offsets
Dynamic datetime variables support formatting and offsets:ms (milliseconds), s (seconds), m (minutes), h (hours), d (days), w (weeks), M (months), y (years)
Environment Files
Store environment-specific variables inhttp-client.env.json:
http-client.env.json
User Overrides
Keep secrets out of source control with.user files:
http-client.env.json.user
Request Chaining
Chain requests together by capturing responses and referencing them in subsequent requests.Naming Requests
Use# @name to give a request a name for later reference:
Response Reference Syntax
JSONPath Examples
Chaining in Code
Smart Assertions
Go beyond simple status code checking withDotHttpAssertions:
Error Pattern Detection
AssertNoErrorsInBodyAsync catches common API anti-patterns where an error is returned with a 200 status:
"error":or"errors":fields"success":falseor"status":"error"- XML
<error>elements "fault":fields
Using with Response Caching
Combine DotHttp testing with cached responses for deterministic tests:Parsing .http Files
UseDotHttpFileParser to parse existing .http files:
Related Resources
Response Caching
Capture and replay HTTP responses for deterministic tests
In-Memory Web API
Test ASP.NET Web API controllers without a server