TestRequest class

Test request builder for constructing and sending HTTP requests in tests.

Provides a fluent interface for building HTTP requests with headers, body, authentication, and other options. Supports common content types and authentication schemes with convenient helper methods.

Example:

// Simple GET request
final response = await testApp.get('/api/users').send();

// POST with JSON body
final response = await testApp.post('/api/users')
  .json({'name': 'John', 'email': 'john@example.com'})
  .send();

// Request with authentication
final response = await testApp.get('/api/profile')
  .bearer('jwt-token-here')
  .send();

// Form data submission
final response = await testApp.post('/api/login')
  .form({'username': 'user', 'password': 'pass'})
  .send();

// Custom headers
final response = await testApp.get('/api/data')
  .header('X-API-Version', '2.0')
  .header('Accept', 'application/json')
  .send();

// Expect specific response
await testApp.get('/api/users/123')
  .expect(200);

// Expect JSON response
await testApp.get('/api/users/123')
  .expectJson({'id': 123, 'name': 'John'});

Features:

  • Fluent interface for request building
  • Built-in support for JSON, form data, and text content
  • Authentication helpers (Basic Auth, Bearer tokens)
  • Cookie management
  • Response assertions
  • Automatic content-type handling

Constructors

TestRequest(HttpClient _client, String method, String path, String baseUrl)
Creates a test request builder for the given parameters.

Properties

baseUrl → String
Base URL of the test server.
final
hashCode → int
The hash code for this object.
no setterinherited
method → String
HTTP method for the request.
final
path → String
Request path (relative to base URL).
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

auth(String username, String password) → TestRequest
Set HTTP Basic Authentication header.
bearer(String token) → TestRequest
Set Bearer token authentication header.
body(dynamic data) → TestRequest
Set raw request body content.
contentType(String type) → TestRequest
Set the Content-Type header for the request.
Add a cookie to the request.
expect(int statusCode) → Future<TestResponse>
Send request and assert the response has a specific status code.
expectJson(Map<String, dynamic> expected) → Future<TestResponse>
Send request and assert the response contains expected JSON.
expectStatus(int statusCode, [Map<String, dynamic>? json]) → Future<TestResponse>
Send request and assert both status code and optional JSON content.
form(Map<String, String> data) → TestRequest
Set request body as URL-encoded form data.
Set a single HTTP header for the request.
headers(Map<String, String> headers) → TestRequest
Set multiple HTTP headers at once.
json(dynamic data) → TestRequest
Set request body as JSON and automatically set Content-Type.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
send() → Future<TestResponse>
Send the HTTP request and return the response.
text(String data) → TestRequest
Set request body as plain text.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited