简体   繁体   中英

Integration tests pass locally but fail on Azure Devops with 400 Bad Request - Invalid Hostname C#

In tests:

var query = "api/objectives?Year=2022&menteeId=eb32dd9d-ee98-400d-8567-811728840487";
_factory = new CustomWebApplicationFactory<Program>(provider, AppendLogEntry);
_client.DefaultRequestHeaders.Clear();
_client = _factory.CreateClient();
_client.DefaultRequestHeaders.TryAddWithoutValidation("Host", "localhost");
 //Act
var response = await _client.GetAsync(query);
var content = await response.Content.ReadAsStringAsync();
// Assert
response.StatusCode.ShouldBe(HttpStatusCode.OK, content);

Error:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></ HEAD >
**<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>**
</BODY></HTML>

I tried to set _client.DefaultRequestHeaders.TryAddWithoutValidation("Host", "localhost"); but still not working...

Solution: I tried to create a dockerfile and to run the test there. Everthing was ok. It seems that it was a problem from Azure Devops.

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