简体   繁体   中英

ASP.NET Core 3: response 404 during integration test produced by RedirectResult(“MyList”)

I have two pages /MyArea/Pages/Create.cshtml and /MyArea/Pages/MyList.cshtml . So I create an entity and finish it with return RedirectResult("MyList") .

This works in browser.

But during the integration test (using Microsoft.AspNetCore.Mvc.Testing package ver 3.0) when I open the /MyArea/Create page and POST the form:

 var createConfirmHttpResponseMessage = await httpClient.SendAsync(postRequest1);
 Assert.IsTrue(createConfirmHttpResponseMessage.StatusCode == HttpStatusCode.OK);

All this returns

{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Content-Length: 0}}

Entity was created and return RedirectResult("MyList") was called

Request message reported: {Method: GET, RequestUri: 'http://localhost/MyList', Version: 1.1, Content: <null>, Headers: { Cookie: .AspNetCore.Antiforgery.... Cookie: .AspNetCore.Antiforgery....}}

I see that request's message is incorrect: RequestUri should contain an area //localhost/ MyArea /MyList,

What kind of "current location management" I should add to the integration test code to force httpClient "respect" the area?

PS I could write it this way return RedirectResult("/MyArea/MyList") on server side but now I want to know how to repeat browser's behavior in the integration test?

One option that I have found so far is to set clientOptions.AllowAutoRedirect=false and then process redirects manually. It is strange that HttpClient doesn't "respect" current location during the automated redirect. It seems like a bug of HttpClient .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM