1

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);
            }
        }
    }
Community
  • 1
  • 1
karry
  • 13
  • 4

2 Answers2

1

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.

gijswijs
  • 1,958
  • 19
  • 24
-2

You should test with default constructor of HttpClient:

var client = new HttpClient();
Bob Dust
  • 2,370
  • 1
  • 17
  • 13
  • that way it will create a normal networking httpclient, and not a client tightly bound to the local temporary in-memory HttpServer instance – quetzalcoatl Apr 22 '20 at 14:35