Fix MVC 5 Tests Missing Redirect Loops Because the Client Follows Automatically

A test sees a final response and provides little information when repeated redirects cause intermittent failures.

Applies to ASP.NET MVC 5 on .NET Framework. Use a staging or development copy to reproduce the behavior and inspect the actual response before changing production.

Diagnostic flowchart

Does the test client follow redirects automatically? Yes: Inspect hops individually. No: Look for application self-links. Then run the regression checks.

Isolate the cause

Does the test client follow redirects automatically?

  • Yes. Automatic following can hide the first response status and Location target that would reveal the wrong route.
  • No. If redirection is already disabled, inspect the captured Location chain and the authentication state at each hop.

Apply the fix

Create a test client that exposes raw redirects. Assert the first target, then follow a small documented number of expected hops rather than assuming the redirect is correct.

using (var handler = new HttpClientHandler { AllowAutoRedirect = false })
using (var client = new HttpClient(handler)) {
    var first = await client.GetAsync(testUrl);
    Assert.IsNotNull(first.Headers.Location);
}

Verify the result

  • Expected path. Test the legitimate flow and confirm the first location differs from the original URL where expected.
  • Regression check. Verify the flow terminates within its expected number of redirects in both signed-in and signed-out cases.

Microsoft reference for testing and lifecycle · ASP.NET PB Tutorial Index

Comments