i referered this httpclient and httpserver to do integrate test over my MVC API, but get 500 error, i can ensure my action don't have internal error because i can success request during Fiddler. my code is very simple as the referer, i pasted them below. i don't have enough reputation to comment the original question, so asked new question here.
my controller action
[HttpGet]
[Route("api/InjectDocuments/hello/")]
public IHttpActionResult Hello()
{
return Ok();
}
my test method
[TestMethod]
public async Task InjectDocumentE2ETest()
{
var config = new HttpConfiguration();
//config.MapHttpAttributeRoutes();
//config.Routes.MapHttpRoute(name: "Default", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional });
WebApiConfig.Register(config);
using (var server = new HttpServer(config))
{
var client = new HttpClient(server);
// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string url = "http://localhost/api/InjectDocuments/hello";
var request = new HttpRequestMessage
{
RequestUri = new Uri(url),
Method = HttpMethod.Get
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var response = await client.SendAsync(request))
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
using (var response = await client.GetAsync(url))
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
}
You need to get more info on the exception itself. You can do this by breaking on every exception thrown by CLR. Do this by checking Debug > Windows > Exception Settings and turning on "Common Language Runtime Exceptions"
Now your test will break with a far more useful message.
Mine was "Transactions are not supported by the in-memory store."
I could then suppress that error by adding this to the configuration of the in memory database .ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning))
For more info on that specific error check https://github.com/aspnet/EntityFrameworkCore/issues/2866
But it's definitely not a given that you suffer from the same error.
您应该使用HttpClient
默认构造函数进行测试:
var client = new 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.