public void BasicSelfHostedEchoControllerWorks() { ScenarioHelper.RunTest( "Echo", "/{s}", new HttpRequestMessage(HttpMethod.Post, "http://localhost/Echo/foo"), (response) => { Assert.DoesNotThrow(() => response.EnsureSuccessStatusCode()); Assert.Equal("foo", response.Content.ReadAsAsync <string>().Result); } ); }
public void HttpResponseExceptionWithExplicitStatusCode(string throwAt) { HttpRequestMessage request = new HttpRequestMessage(); request.RequestUri = new Uri(ScenarioHelper.BaseAddress + "/ExceptionTests/ReturnString"); request.Method = HttpMethod.Post; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Content = new StringContent("\"" + throwAt + "\"", Encoding.UTF8, "application/json"); ScenarioHelper.RunTest( "ExceptionTests", "/{action}", request, response => { Assert.NotNull(response.Content); Assert.NotNull(response.Content.Headers.ContentType); Assert.Equal(response.Content.Headers.ContentType.MediaType, "application/json"); if (throwAt == "DoNotThrow") { Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("Hello World!", response.Content.ReadAsAsync <string>(new List <MediaTypeFormatter>() { new JsonMediaTypeFormatter() }).Result); } else { Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); Assert.Equal(String.Format("Error at {0}", throwAt), response.Content.ReadAsAsync <string>(new List <MediaTypeFormatter>() { new JsonMediaTypeFormatter() }).Result); } }, config => { config.Services.Replace(typeof(IContentNegotiator), new CustomContentNegotiator(throwAt)); config.MessageHandlers.Add(new CustomMessageHandler(throwAt)); config.Filters.Add(new CustomActionFilterAttribute(throwAt)); config.Filters.Add(new CustomAuthorizationFilterAttribute(throwAt)); config.Filters.Add(new CustomAuthenticationFilter(throwAt)); config.Filters.Add(new CustomExceptionFilterAttribute(throwAt)); config.Formatters.Clear(); config.Formatters.Add(new CustomJsonMediaTypeFormatter(throwAt)); } ); }
public void SimpleConNegWorks(string mediaType) { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Echo/ContentNegotiatedEcho/foo"); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType)); ScenarioHelper.RunTest( "Echo", "/{action}/{s}", request, (response) => { Assert.DoesNotThrow(() => response.EnsureSuccessStatusCode()); Assert.Equal(mediaType, response.Content.Headers.ContentType.MediaType); } ); }