Agent class

Executes HTTP requests during application testing.

Agents create and execute test requests. For most cases, methods like get, post, put and delete are used to execute these requests. For more granular control, request creates a request object that can be configured in more detail.

Each Agent has a set of default values, such as headers, that it uses for each of its requests. When many test requests have common headers, it is preferable to create an Agent for the similar requests. For example, after authenticating a user during tests, an Agent can add the 'Authorization' header for that user to each of its requests.

The default constructor takes an Application and configures itself from that application's options. The application is typically provided by TestHarness. TestHarness contains a default agent. Example usage:

    void main() {
      final harness = TestHarness<MyChannel>()..install();

      test("GET /thing returns 200", () async {
        final resp = await harness.agent.get("/thing");
        expect(resp, hasStatus(200));
      });
    }

Constructors

Agent(Application<ApplicationChannel?>? app)
Configures a new agent that sends requests to app.
Agent.from(Agent original)
Configures a new agent with the same properties as original.
Agent.fromOptions(ApplicationOptions config, {bool useHTTPS = false})
Configures a new agent that sends requests to a server configured by config.
Agent.onPort(int? _port)
Configures a new agent that sends requests to 'http://localhost:_port'.

Properties

accept List<ContentType>
Adds Accept header to requests from this agent.
no getter
baseURL String
The base URL that this agent's requests will be made against.
no setter
bearerAuthorization String
Adds bearer authorization to requests from this agent.
no getter
contentType ContentType
Sets the default content-type of requests made by this agent.
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
headers Map<String, dynamic>
Default headers to be added to requests made by this agent.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

close() → void
Closes this instances underlying HTTP client.
delete(String path, {dynamic body, Map<String, dynamic>? headers, Map<String, dynamic>? query}) Future<TestResponse>
Makes a DELETE request with this agent.
execute(String method, String path, {dynamic body, Map<String, dynamic>? headers, Map<String, dynamic>? query}) Future<TestResponse>
Executes an HTTP request with this agent.
get(String path, {Map<String, dynamic>? headers, Map<String, dynamic>? query}) Future<TestResponse>
Makes a GET request with this agent.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
post(String path, {dynamic body, Map<String, dynamic>? headers, Map<String, dynamic>? query}) Future<TestResponse>
Makes a POST request with this agent.
put(String path, {dynamic body, Map<String, dynamic>? headers, Map<String, dynamic>? query}) Future<TestResponse>
Makes a PUT request with this agent.
request(String path) TestRequest
Creates a request object for path that can be configured and executed later.
setBasicAuthorization(String username, String? password) → void
Adds basic authorization to requests from this agent.
toString() String
A string representation of this object.
inherited

Operators

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