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);
            }
        }
    }
3
  • What does Ok() do? Commented Nov 30, 2016 at 4:10
  • Did you ever fix this? I have the exact same issue. Commented Jan 10, 2018 at 6:27
  • Consider using OWIN TestServer instead. Commented Jan 10, 2018 at 7:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-2

You should test with default constructor of HttpClient:

var client = new HttpClient();

1 Comment

that way it will create a normal networking httpclient, and not a client tightly bound to the local temporary in-memory HttpServer instance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.