ASP.NET Core Web API Interview Question #158

What API response behaviors should integration tests verify?

Testing, HttpClient, Integration Testing, Background Services & Deployment Senior Advanced

Quick Interview Answer

Verify status codes, response bodies, content types, headers, validation errors, authentication and authorization outcomes, Location headers for created resources, and important side effects.

Detailed Explanation

Verify status codes, response bodies, content types, headers, validation errors, authentication and authorization outcomes, Location headers for created resources, and important side effects.

Tests should reflect the public API contract. Avoid coupling every test to internal classes or private implementation details that clients cannot observe.

Code Example

var response = await client.PostAsJsonAsync(
    "/api/products",
    new { name = "Mouse", price = 29.99m });

Assert.Equal(HttpStatusCode.Created, response.StatusCode);
Assert.NotNull(response.Headers.Location);