Skip to main content

Header Extensions

server_testing provides utilities for working with HTTP headers in tests through the HttpHeaderExtension.

Converting Headers to Map

The toMap() extension method converts HttpHeaders to a Map for easier manipulation:

test('header manipulation', () {
final response = await client.get('/api');

// Get headers as map
final headerMap = response.headers.toMap();

// Access header values
final contentType = headerMap['content-type'];
final authorization = headerMap['authorization'];

// Headers are preserved as lists
expect(headerMap['accept'], isList);
});

Working with Header Maps

Use the header map for testing and manipulation:

test('verify headers', () async {
final response = await client.get('/api');
final headers = response.headers.toMap();

// Check header existence
expect(headers.containsKey('content-type'), isTrue);

// Check header values
expect(
headers['content-type']?[0],
equals('application/json')
);

// Check multiple values
expect(
headers['accept']?.length,
greaterThan(1)
);
});