Updating FunctionalTests to use HttpClient abstractions

This commit is contained in:
Pranav K 2014-08-28 15:54:50 -07:00
parent ccc20a38c1
commit b8692c2c21
17 changed files with 387 additions and 447 deletions

View File

@ -20,7 +20,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedMessage = "TODO: No service for type 'ActivatorWebSite.CannotBeActivatedController+FakeType' " + var expectedMessage = "TODO: No service for type 'ActivatorWebSite.CannotBeActivatedController+FakeType' " +
"has been registered."; "has been registered.";
@ -34,15 +34,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expected = "4|some-text"; var expected = "4|some-text";
// Act // Act
var result = await client.GetAsync("http://localhost/Plain?foo=some-text"); var response = await client.GetAsync("http://localhost/Plain?foo=some-text");
// Assert // Assert
Assert.Equal("Fake-Value", result.HttpContext.Response.Headers["X-Fake-Header"]); var headerValue = Assert.Single(response.Headers.GetValues("X-Fake-Header"));
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); Assert.Equal("Fake-Value", headerValue);
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expected, body); Assert.Equal(expected, body);
} }
@ -51,14 +52,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expected = "Hello world"; var expected = "Hello world";
// Act // Act
var result = await client.GetAsync("http://localhost/Regular"); var body = await client.GetStringAsync("http://localhost/Regular");
// Assert // Assert
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
Assert.Equal(expected, body); Assert.Equal(expected, body);
} }
@ -66,14 +66,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
public async Task ViewActivator_ActivatesDefaultInjectedProperties() public async Task ViewActivator_ActivatesDefaultInjectedProperties()
{ {
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expected = @"<label for=""Hello"">Hello</label> world! /View/ConsumeServicesFromBaseType"; var expected = @"<label for=""Hello"">Hello</label> world! /View/ConsumeServicesFromBaseType";
// Act // Act
var result = await client.GetAsync("http://localhost/View/ConsumeDefaultProperties"); var body = await client.GetStringAsync("http://localhost/View/ConsumeDefaultProperties");
// Assert // Assert
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
Assert.Equal(expected, body.Trim()); Assert.Equal(expected, body.Trim());
} }
@ -81,14 +80,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
public async Task ViewActivator_ActivatesAndContextualizesInjectedServices() public async Task ViewActivator_ActivatesAndContextualizesInjectedServices()
{ {
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expected = "4 test-value"; var expected = "4 test-value";
// Act // Act
var result = await client.GetAsync("http://localhost/View/ConsumeInjectedService?test=test-value"); var body = await client.GetStringAsync("http://localhost/View/ConsumeInjectedService?test=test-value");
// Assert // Assert
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
Assert.Equal(expected, body.Trim()); Assert.Equal(expected, body.Trim());
} }
@ -96,15 +94,14 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
public async Task ViewActivator_ActivatesServicesFromBaseType() public async Task ViewActivator_ActivatesServicesFromBaseType()
{ {
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expected = var expected =
@"/content/scripts/test.js"; @"/content/scripts/test.js";
// Act // Act
var result = await client.GetAsync("http://localhost/View/ConsumeServicesFromBaseType"); var body = await client.GetStringAsync("http://localhost/View/ConsumeServicesFromBaseType");
// Assert // Assert
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
Assert.Equal(expected, body.Trim()); Assert.Equal(expected, body.Trim());
} }
} }

View File

@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
@ -11,32 +13,28 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
public class AntiForgeryTests public class AntiForgeryTests
{ {
private readonly IServiceProvider _services; private readonly IServiceProvider _services = TestHelper.CreateServices("AntiForgeryWebSite");
private readonly Action<IBuilder> _app = new AntiForgeryWebSite.Startup().Configure; private readonly Action<IBuilder> _app = new AntiForgeryWebSite.Startup().Configure;
public AntiForgeryTests()
{
_services = TestHelper.CreateServices("AntiForgeryWebSite");
}
[Fact] [Fact]
public async Task MultipleAFTokensWithinTheSamePage_AreAllowed() public async Task MultipleAFTokensWithinTheSamePage_AreAllowed()
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Account/Login"); var response = await client.GetAsync("http://localhost/Account/Login");
//Assert //Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("SAMEORIGIN", response.Headers["X-Frame-Options"]); var header = Assert.Single(response.Headers.GetValues("X-Frame-Options"));
Assert.Equal("SAMEORIGIN", header);
var setCookieHeader = response.Headers.GetCommaSeparatedValues("Set-Cookie"); var setCookieHeader = response.Headers.GetValues("Set-Cookie").ToArray();
Assert.Equal(2, setCookieHeader.Count); Assert.Equal(2, setCookieHeader.Length);
Assert.Equal(true, setCookieHeader[0].StartsWith("__RequestVerificationToken")); Assert.True(setCookieHeader[0].StartsWith("__RequestVerificationToken"));
Assert.Equal(true, setCookieHeader[1].StartsWith("__RequestVerificationToken")); Assert.True(setCookieHeader[1].StartsWith("__RequestVerificationToken"));
} }
} }
} }

View File

@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Net;
using System.Net.Http.Headers;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using BasicWebSite; using BasicWebSite;
@ -13,7 +15,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
public class BasicTests public class BasicTests
{ {
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider = TestHelper.CreateServices("BasicWebSite");
private readonly Action<IBuilder> _app = new Startup().Configure; private readonly Action<IBuilder> _app = new Startup().Configure;
// Some tests require comparing the actual response body against an expected response baseline // Some tests require comparing the actual response body against an expected response baseline
@ -22,11 +24,6 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// use it on all the rest of the tests. // use it on all the rest of the tests.
private readonly Assembly _resourcesAssembly = typeof(BasicTests).GetTypeInfo().Assembly; private readonly Assembly _resourcesAssembly = typeof(BasicTests).GetTypeInfo().Assembly;
public BasicTests()
{
_provider = TestHelper.CreateServices("BasicWebSite");
}
[InlineData("http://localhost/")] [InlineData("http://localhost/")]
[InlineData("http://localhost/Home")] [InlineData("http://localhost/Home")]
[InlineData("http://localhost/Home/Index")] [InlineData("http://localhost/Home/Index")]
@ -36,7 +33,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");
// The K runtime compiles every file under compiler/resources as a resource at runtime with the same name // The K runtime compiles every file under compiler/resources as a resource at runtime with the same name
// as the file name, in order to update a baseline you just need to change the file in that folder. // as the file name, in order to update a baseline you just need to change the file in that folder.
@ -45,12 +43,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// Act // Act
// The host is not important as everything runs in memory and tests are isolated from each other. // The host is not important as everything runs in memory and tests are isolated from each other.
var result = await client.GetAsync(url); var response = await client.GetAsync(url);
var responseContent = await result.ReadBodyAsStringAsync(); var responseContent = await response.Content.ReadAsStringAsync();
// Assert // Assert
Assert.Equal(200, result.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(result.ContentType, "text/html; charset=utf-8"); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
Assert.Equal(expectedContent, responseContent); Assert.Equal(expectedContent, responseContent);
} }
@ -59,16 +57,18 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync("compiler/resources/BasicWebSite.Home.PlainView.html"); var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync("compiler/resources/BasicWebSite.Home.PlainView.html");
var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");
// Act // Act
var result = await client.GetAsync("http://localhost/Home/PlainView"); var response = await client.GetAsync("http://localhost/Home/PlainView");
var responseContent = await result.ReadBodyAsStringAsync(); var responseContent = await response.Content.ReadAsStringAsync();
// Assert // Assert
Assert.Equal(200, result.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(result.ContentType, "text/html; charset=utf-8"); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
Assert.Equal(expectedContent, responseContent); Assert.Equal(expectedContent, responseContent);
} }
@ -77,17 +77,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var result = await client.GetAsync("http://localhost/Home/NoContentResult"); var response = await client.GetAsync("http://localhost/Home/NoContentResult");
var responseContent = await response.Content.ReadAsStringAsync();
// Assert // Assert
Assert.Equal(204, result.StatusCode); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
Assert.Null(result.ContentType); Assert.Null(response.Content.Headers.ContentType);
Assert.Null(result.ContentLength); Assert.Equal(0, response.Content.Headers.ContentLength);
Assert.NotNull(result.Body); Assert.Equal(0, responseContent.Length);
Assert.Equal(0, result.Body.Length);
} }
[Fact] [Fact]
@ -95,14 +95,14 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var result = await client.GetAsync("http://localhost/Home/ActionReturningTask"); var response = await client.GetAsync("http://localhost/Home/ActionReturningTask");
// Assert // Assert
Assert.Equal(204, result.StatusCode); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
var body = await result.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal("Hello world", body); Assert.Equal("Hello world", body);
} }
@ -111,25 +111,19 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContent = "1"; var expectedContent = "1";
// Call the server 3 times, and make sure the return value remains the same. // Act and Assert
var results = new string[3]; for (var i = 0; i < 3; i++)
// Act
for (int i = 0; i < 3; i++)
{ {
var result = await client.GetAsync("http://localhost/Monitor/CountActionDescriptorInvocations"); var result = await client.GetAsync("http://localhost/Monitor/CountActionDescriptorInvocations");
Assert.Equal(200, result.StatusCode); Assert.Equal(HttpStatusCode.OK, result.StatusCode);
results[i] = await result.ReadBodyAsStringAsync(); var responseContent = await result.Content.ReadAsStringAsync();
}
// Assert Assert.Equal(expectedContent, responseContent);
Assert.Equal(expectedContent, results[0]); }
Assert.Equal(expectedContent, results[1]);
Assert.Equal(expectedContent, results[2]);
} }
} }
} }

View File

@ -11,26 +11,20 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
public class CompositeViewEngineTests public class CompositeViewEngineTests
{ {
private readonly IServiceProvider _services; private readonly IServiceProvider _services = TestHelper.CreateServices("CompositeViewEngine");
private readonly Action<IBuilder> _app = new CompositeViewEngine.Startup().Configure; private readonly Action<IBuilder> _app = new CompositeViewEngine.Startup().Configure;
public CompositeViewEngineTests()
{
_services = TestHelper.CreateServices("CompositeViewEngine");
}
[Fact] [Fact]
public async Task CompositeViewEngine_FindsPartialViewsAcrossAllEngines() public async Task CompositeViewEngine_FindsPartialViewsAcrossAllEngines()
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/"); var body = await client.GetStringAsync("http://localhost/");
// Assert // Assert
var body = await response.ReadBodyAsStringAsync();
Assert.Equal("Hello world", body.Trim()); Assert.Equal("Hello world", body.Trim());
} }
@ -39,13 +33,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Home/TestView"); var body = await client.GetStringAsync("http://localhost/Home/TestView");
// Assert // Assert
var body = await response.ReadBodyAsStringAsync();
Assert.Equal("Content from test view", body.Trim()); Assert.Equal("Content from test view", body.Trim());
} }
} }

View File

@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Net;
using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using ConnegWebsite; using ConnegWebsite;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
@ -20,18 +22,18 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Selects custom even though it is last in the list. // Selects custom even though it is last in the list.
var expectedContentType = "application/custom;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("application/custom;charset=utf-8");
var expectedBody = "Written using custom format."; var expectedBody = "Written using custom format.";
// Act // Act
var result = await client.GetAsync("http://localhost/Normal/WriteUserUsingCustomFormat"); var response = await client.GetAsync("http://localhost/Normal/WriteUserUsingCustomFormat");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -40,16 +42,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/json;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("application/json;charset=utf-8");
var expectedBody = "{\r\n \"Name\": \"My name\",\r\n \"Address\": \"My address\"\r\n}"; var expectedBody = "{\r\n \"Name\": \"My name\",\r\n \"Address\": \"My address\"\r\n}";
// Act // Act
var result = await client.GetAsync("http://localhost/Normal/MultipleAllowedContentTypes"); var response = await client.GetAsync("http://localhost/Normal/MultipleAllowedContentTypes");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -58,16 +60,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "text/plain;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("text/plain;charset=utf-8");
var expectedBody = "NormalController"; var expectedBody = "NormalController";
// Act // Act
var result = await client.GetAsync("http://localhost/Normal/ReturnClassName"); var response = await client.GetAsync("http://localhost/Normal/ReturnClassName");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -76,15 +78,14 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/json;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("application/json;charset=utf-8");
//var expectedBody = "\"NormalController\"";
// Act // Act
var result = await client.GetAsync("http://localhost/Normal/ReturnUser"); var response = await client.GetAsync("http://localhost/Normal/ReturnUser");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
} }
[Fact] [Fact]
@ -92,31 +93,32 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var result = await client.GetAsync("http://localhost/Normal/ReturnUser_NoMatchingFormatter"); var response = await client.GetAsync("http://localhost/Normal/ReturnUser_NoMatchingFormatter");
// Assert // Assert
Assert.Equal(406, result.HttpContext.Response.StatusCode); Assert.Equal(HttpStatusCode.NotAcceptable, response.StatusCode);
} }
[Fact] [Fact]
public async Task ProducesContentAttribute_OnAction_OverridesTheValueOnClass() public async Task ProducesContentAttribute_OnAction_OverridesTheValueOnClass()
{ {
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Value on the class is application/json. // Value on the class is application/json.
var expectedContentType = "application/custom_ProducesContentBaseController_Action;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse(
"application/custom_ProducesContentBaseController_Action;charset=utf-8");
var expectedBody = "ProducesContentBaseController"; var expectedBody = "ProducesContentBaseController";
// Act // Act
var result = await client.GetAsync("http://localhost/ProducesContentBase/ReturnClassName"); var response = await client.GetAsync("http://localhost/ProducesContentBase/ReturnClassName");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -124,17 +126,18 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
public async Task ProducesContentAttribute_OnDerivedClass_OverridesTheValueOnBaseClass() public async Task ProducesContentAttribute_OnDerivedClass_OverridesTheValueOnBaseClass()
{ {
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/custom_ProducesContentOnClassController;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse(
"application/custom_ProducesContentOnClassController;charset=utf-8");
var expectedBody = "ProducesContentOnClassController"; var expectedBody = "ProducesContentOnClassController";
// Act // Act
var result = await client.GetAsync( var response = await client.GetAsync(
"http://localhost/ProducesContentOnClass/ReturnClassNameWithNoContentTypeOnAction"); "http://localhost/ProducesContentOnClass/ReturnClassNameWithNoContentTypeOnAction");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -142,16 +145,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
public async Task ProducesContentAttribute_OnDerivedAction_OverridesTheValueOnBaseClass() public async Task ProducesContentAttribute_OnDerivedAction_OverridesTheValueOnBaseClass()
{ {
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/custom_NoProducesContentOnClassController_Action;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse(
"application/custom_NoProducesContentOnClassController_Action;charset=utf-8");
var expectedBody = "NoProducesContentOnClassController"; var expectedBody = "NoProducesContentOnClassController";
// Act // Act
var result = await client.GetAsync("http://localhost/NoProducesContentOnClass/ReturnClassName"); var response = await client.GetAsync("http://localhost/NoProducesContentOnClass/ReturnClassName");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -159,16 +163,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
public async Task ProducesContentAttribute_OnDerivedAction_OverridesTheValueOnBaseAction() public async Task ProducesContentAttribute_OnDerivedAction_OverridesTheValueOnBaseAction()
{ {
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/custom_NoProducesContentOnClassController_Action;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse(
"application/custom_NoProducesContentOnClassController_Action;charset=utf-8");
var expectedBody = "NoProducesContentOnClassController"; var expectedBody = "NoProducesContentOnClassController";
// Act // Act
var result = await client.GetAsync("http://localhost/NoProducesContentOnClass/ReturnClassName"); var response = await client.GetAsync("http://localhost/NoProducesContentOnClass/ReturnClassName");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -177,16 +182,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/custom_ProducesContentOnClassController_Action;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse(
"application/custom_ProducesContentOnClassController_Action;charset=utf-8");
var expectedBody = "ProducesContentOnClassController"; var expectedBody = "ProducesContentOnClassController";
// Act // Act
var result = await client.GetAsync("http://localhost/ProducesContentOnClass/ReturnClassNameContentTypeOnDerivedAction"); var response = await client.GetAsync("http://localhost/ProducesContentOnClass/ReturnClassNameContentTypeOnDerivedAction");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
[Fact] [Fact]
@ -194,16 +200,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/json;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("application/json;charset=utf-8");
var expectedBody = "{\"MethodName\":\"Produces_WithNonObjectResult\"}"; var expectedBody = "{\"MethodName\":\"Produces_WithNonObjectResult\"}";
// Act // Act
var result = await client.GetAsync("http://localhost/JsonResult/Produces_WithNonObjectResult"); var response = await client.GetAsync("http://localhost/JsonResult/Produces_WithNonObjectResult");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -212,16 +218,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/json;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("application/json;charset=utf-8");
var expectedBody = "{\"MethodName\":\"ReturnJsonResult\"}"; var expectedBody = "{\"MethodName\":\"ReturnJsonResult\"}";
// Act // Act
var result = await client.GetAsync("http://localhost/JsonResult/ReturnJsonResult"); var response = await client.GetAsync("http://localhost/JsonResult/ReturnJsonResult");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -230,16 +236,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/custom-json;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("application/custom-json;charset=utf-8");
var expectedBody = "{ MethodName = ReturnJsonResult_WithCustomMediaType }"; var expectedBody = "{ MethodName = ReturnJsonResult_WithCustomMediaType }";
// Act // Act
var result = await client.GetAsync("http://localhost/JsonResult/ReturnJsonResult_WithCustomMediaType"); var response = await client.GetAsync("http://localhost/JsonResult/ReturnJsonResult_WithCustomMediaType");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -248,16 +254,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/json;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("application/json;charset=utf-8");
var expectedBody = "{\"MethodName\":\"ReturnJsonResult_WithCustomMediaType_NoFormatter\"}"; var expectedBody = "{\"MethodName\":\"ReturnJsonResult_WithCustomMediaType_NoFormatter\"}";
// Act // Act
var result = await client.GetAsync("http://localhost/JsonResult/ReturnJsonResult_WithCustomMediaType_NoFormatter"); var response = await client.GetAsync("http://localhost/JsonResult/ReturnJsonResult_WithCustomMediaType_NoFormatter");
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
} }

View File

@ -1,10 +1,9 @@
#if NET45 #if NET45
using System; using System;
using System.IO; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutofacWebSite; using AutofacWebSite;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
using Xunit; using Xunit;
@ -20,20 +19,19 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// Arrange // Arrange
var provider = TestHelper.CreateServices("AutofacWebSite"); var provider = TestHelper.CreateServices("AutofacWebSite");
Action<IBuilder> app = new Startup().Configure; Action<IBuilder> app = new Startup().Configure;
TestServer server = null; HttpResponseMessage response = null;
HttpResponse response = null;
// Act & Assert // Act & Assert
await Assert.DoesNotThrowAsync(async () => await Assert.DoesNotThrowAsync(async () =>
{ {
// This essentially calls into the Startup.Configuration method // This essentially calls into the Startup.Configuration method
server = TestServer.Create(provider, app); var server = TestServer.Create(provider, app);
// Make a request to start resolving DI pieces // Make a request to start resolving DI pieces
response = await server.Handler.GetAsync(url); response = await server.CreateClient().GetAsync(url);
}); });
var actualResponseBody = new StreamReader(response.Body).ReadToEnd(); var actualResponseBody = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedResponseBody, actualResponseBody); Assert.Equal(expectedResponseBody, actualResponseBody);
} }
} }

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
@ -21,13 +20,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
var expected = @"Hello Person1"; var expected = @"Hello Person1";
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var result = await client.GetAsync("http://localhost/Directives/ViewInheritsInjectAndUsingsFromViewStarts"); var body = await client.GetStringAsync("http://localhost/Directives/ViewInheritsInjectAndUsingsFromViewStarts");
// Assert // Assert
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
Assert.Equal(expected, body.Trim()); Assert.Equal(expected, body.Trim());
} }
@ -36,15 +34,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
var expected = @"WriteLiteral says:layout:Write says:Write says:Hello Person2"; var expected = @"WriteLiteral says:layout:Write says:Write says:Hello Person2";
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var result = await client.GetAsync("http://localhost/Directives/ViewInheritsBasePageFromViewStarts"); var body = await client.GetStringAsync("http://localhost/Directives/ViewInheritsBasePageFromViewStarts");
// Assert // Assert
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
Assert.Equal(expected, body.Trim()); Assert.Equal(expected, body.Trim());
} }
} }
} }

View File

@ -1,22 +0,0 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Mvc.FunctionalTests
{
// This class contains methods to make easier to read responses in different formats
// until there is a built-in easier way to do it.
public static class HttpResponseHelpers
{
public static async Task<string> ReadBodyAsStringAsync(this HttpResponse response)
{
using (var streamReader = new StreamReader(response.Body))
{
return await streamReader.ReadToEndAsync();
}
}
}
}

View File

@ -2,20 +2,13 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic; using System.Net;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using InlineConstraints; using InlineConstraints;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Routing;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.Runtime;
using Microsoft.Framework.Runtime.Infrastructure;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.FunctionalTests namespace Microsoft.AspNet.Mvc.FunctionalTests
@ -24,7 +17,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
private readonly Action<IBuilder> _app = new Startup().Configure; private readonly Action<IBuilder> _app = new Startup().Configure;
public InlineConstraintTests() public InlineConstraintTests()
{ {
_provider = TestHelper.CreateServices("InlineConstraintsWebSite"); _provider = TestHelper.CreateServices("InlineConstraintsWebSite");
@ -37,20 +30,20 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
public async Task RoutingToANonExistantArea_WithExistConstraint_RoutesToCorrectAction() public async Task RoutingToANonExistantArea_WithExistConstraint_RoutesToCorrectAction()
{ {
var svc = _provider.GetService<ICommandLineArgumentBuilder>(); var svc = _provider.GetService<ICommandLineArgumentBuilder>();
svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue="+ svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue=" +
"{area:exists}/{controller=Home}/{action=Index}"); "{area:exists}/{controller=Home}/{action=Index}");
svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue="+ svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue=" +
"{controller=Home}/{action=Index}"); "{controller=Home}/{action=Index}");
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var result = await client.GetAsync("http://localhost/Users"); var response = await client.GetAsync("http://localhost/Users");
Assert.Equal(200, result.StatusCode);
// Assert // Assert
var returnValue = await result.ReadBodyAsStringAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var returnValue = await response.Content.ReadAsStringAsync();
Assert.Equal("Users.Index", returnValue); Assert.Equal("Users.Index", returnValue);
} }
@ -59,17 +52,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var svc = _provider.GetService<ICommandLineArgumentBuilder>(); var svc = _provider.GetService<ICommandLineArgumentBuilder>();
svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue="+ svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue=" +
"{area}/{controller=Home}/{action=Index}"); "{area}/{controller=Home}/{action=Index}");
svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue"+ svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue" +
"={controller=Home}/{action=Index}"); "={controller=Home}/{action=Index}");
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act & Assert // Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException> var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetAsync("http://localhost/Users"));
(async () => await client.GetAsync("http://localhost/Users"));
Assert.Equal("The view 'Index' was not found." + Assert.Equal("The view 'Index' was not found." +
" The following locations were searched:\r\n/Areas/Users/Views/Home/Index.cshtml\r\n" + " The following locations were searched:\r\n/Areas/Users/Views/Home/Index.cshtml\r\n" +

View File

@ -2,6 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
@ -12,30 +16,26 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
public class InputFormatterTests public class InputFormatterTests
{ {
private readonly IServiceProvider _services; private readonly IServiceProvider _services = TestHelper.CreateServices("FormatterWebSite");
private readonly Action<IBuilder> _app = new FormatterWebSite.Startup().Configure; private readonly Action<IBuilder> _app = new FormatterWebSite.Startup().Configure;
public InputFormatterTests()
{
_services = TestHelper.CreateServices("FormatterWebSite");
}
[Fact] [Fact]
public async Task CheckIfXmlInputFormatterIsBeingCalled() public async Task CheckIfXmlInputFormatterIsBeingCalled()
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
var sampleInputInt = 10; var sampleInputInt = 10;
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<DummyClass><SampleInt>" + sampleInputInt.ToString() + "</SampleInt></DummyClass>"; "<DummyClass><SampleInt>" + sampleInputInt.ToString() + "</SampleInt></DummyClass>";
var content = new StringContent(input, Encoding.UTF8, "application/xml");
// Act // Act
var response = await client.PostAsync("http://localhost/Home/Index", input, "application/xml"); var response = await client.PostAsync("http://localhost/Home/Index", content);
//Assert //Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(sampleInputInt.ToString(), await response.ReadBodyAsStringAsync()); Assert.Equal(sampleInputInt.ToString(), await response.Content.ReadAsStringAsync());
} }
[Theory] [Theory]
@ -48,16 +48,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
var sampleInputInt = 10; var sampleInputInt = 10;
var input = "{\"SampleInt\":10}"; var input = "{\"SampleInt\":10}";
var content = new StringContent(input, Encoding.UTF8, requestContentType);
// Act // Act
var response = await client.PostAsync("http://localhost/Home/Index", input, requestContentType); var response = await client.PostAsync("http://localhost/Home/Index", content);
//Assert //Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(sampleInputInt.ToString(), await response.ReadBodyAsStringAsync()); Assert.Equal(sampleInputInt.ToString(), await response.Content.ReadAsStringAsync());
} }
[Theory] [Theory]
@ -71,24 +72,27 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
[InlineData("invalid", false)] [InlineData("invalid", false)]
[InlineData("application/custom", false)] [InlineData("application/custom", false)]
[InlineData("image/jpg", false)] [InlineData("image/jpg", false)]
public async Task ModelStateErrorValidation_NoInputFormatterFound_ForGivenContetType(string requestContentType, public async Task ModelStateErrorValidation_NoInputFormatterFound_ForGivenContentType(string requestContentType,
bool filterHandlesModelStateError) bool filterHandlesModelStateError)
{ {
// Arrange // Arrange
var actionName = filterHandlesModelStateError ? "ActionFilterHandlesError" : "ActionHandlesError"; var actionName = filterHandlesModelStateError ? "ActionFilterHandlesError" : "ActionHandlesError";
var expectedSource = filterHandlesModelStateError ? "filter" : "action"; var expectedSource = filterHandlesModelStateError ? "filter" : "action";
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
var input = "{\"SampleInt\":10}"; var input = "{\"SampleInt\":10}";
var content = new StringContent(input);
content.Headers.Clear();
content.Headers.TryAddWithoutValidation("Content-Type", requestContentType);
// Act // Act
var response = await client.PostAsync("http://localhost/InputFormatter/" + actionName, var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/InputFormatter/" + actionName);
input, request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
requestContentType, request.Content = content;
(request) => request.Accept = "application/json"); var response = await client.SendAsync(request);
var responseBody = await response.ReadBodyAsStringAsync(); var responseBody = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<FormatterWebSite.ErrorInfo>(responseBody); var result = JsonConvert.DeserializeObject<FormatterWebSite.ErrorInfo>(responseBody);
// Assert // Assert

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
@ -11,27 +12,22 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
public class ModelBindingTests public class ModelBindingTests
{ {
private readonly IServiceProvider _services; private readonly IServiceProvider _services = TestHelper.CreateServices("ModelBindingWebSite");
private readonly Action<IBuilder> _app = new ModelBindingWebSite.Startup().Configure; private readonly Action<IBuilder> _app = new ModelBindingWebSite.Startup().Configure;
public ModelBindingTests()
{
_services = TestHelper.CreateServices("ModelBindingWebSite");
}
[Fact] [Fact]
public async Task ModelBindingBindsBase64StringsToByteArrays() public async Task ModelBindingBindsBase64StringsToByteArrays()
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Home/Index?byteValues=SGVsbG9Xb3JsZA=="); var response = await client.GetAsync("http://localhost/Home/Index?byteValues=SGVsbG9Xb3JsZA==");
//Assert //Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("HelloWorld", await response.ReadBodyAsStringAsync()); Assert.Equal("HelloWorld", await response.Content.ReadAsStringAsync());
} }
[Fact] [Fact]
@ -39,14 +35,14 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Home/Index?byteValues="); var response = await client.GetAsync("http://localhost/Home/Index?byteValues=");
//Assert //Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("\0", await response.ReadBodyAsStringAsync()); Assert.Equal("\0", await response.Content.ReadAsStringAsync());
} }
} }
} }

View File

@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Net;
using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using ConnegWebsite; using ConnegWebsite;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
@ -24,16 +26,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "text/plain;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("text/plain;charset=utf-8");
var expectedBody = actionName; var expectedBody = actionName;
// Act // Act
var result = await client.GetAsync("http://localhost/TextPlain/" + actionName); var response = await client.GetAsync("http://localhost/TextPlain/" + actionName);
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); Assert.Equal(expectedBody, body);
} }
@ -44,41 +46,36 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedContentType = "application/json;charset=utf-8"; var expectedContentType = MediaTypeHeaderValue.Parse("application/json;charset=utf-8");
var expectedBody = actionName;
// Act // Act
var result = await client.GetAsync("http://localhost/TextPlain/" + actionName); var response = await client.GetAsync("http://localhost/TextPlain/" + actionName);
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Equal(expectedContentType, response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
} }
[Theory] [Theory]
[InlineData("ReturnTaskOfString_NullValue")] [InlineData("ReturnTaskOfString_NullValue")]
[InlineData("ReturnTaskOfObject_NullValue")] [InlineData("ReturnTaskOfObject_NullValue")]
[InlineData("ReturnObject_NullValue")] [InlineData("ReturnObject_NullValue")]
public async Task NoContentFormatter_ForNullValue_GetsSelectedAndWritesResponse(string actionName) public async Task NoContentFormatter_ForNullValue_GetsSelectedAndWritesResponse(string actionName)
{ {
// Arrange // Arrange
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
string expectedContentType = null;
// ReadBodyAsString returns empty string instead of null.
string expectedBody = "";
// Act // Act
var result = await client.GetAsync("http://localhost/NoContent/" + actionName); var response = await client.GetAsync("http://localhost/NoContent/" + actionName);
// Assert // Assert
Assert.Equal(expectedContentType, result.HttpContext.Response.ContentType); Assert.Null(response.Content.Headers.ContentType);
var body = await result.HttpContext.Response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body); // Response body is empty instead of null.
Assert.Equal(204, result.HttpContext.Response.StatusCode); Assert.Empty(body);
Assert.Equal(0, result.HttpContext.Response.ContentLength); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
Assert.Equal(0, response.Content.Headers.ContentLength);
} }
} }
} }

View File

@ -4,40 +4,36 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
using Newtonsoft.Json;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.FunctionalTests namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
public class RoutingTests public class RoutingTests
{ {
private readonly IServiceProvider _services; private readonly IServiceProvider _services = TestHelper.CreateServices("RoutingWebSite");
private readonly Action<IBuilder> _app = new RoutingWebSite.Startup().Configure; private readonly Action<IBuilder> _app = new RoutingWebSite.Startup().Configure;
public RoutingTests()
{
_services = TestHelper.CreateServices("RoutingWebSite");
}
[Fact] [Fact]
public async Task ConventionRoutedController_ActionIsReachable() public async Task ConventionRoutedController_ActionIsReachable()
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Home/Index"); var response = await client.GetAsync("http://localhost/Home/Index");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/Home/Index", result.ExpectedUrls); Assert.Contains("/Home/Index", result.ExpectedUrls);
@ -57,15 +53,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/"); var response = await client.GetAsync("http://localhost/");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/", result.ExpectedUrls); Assert.Contains("/", result.ExpectedUrls);
@ -85,13 +81,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Home/NotAnAction"); var response = await client.GetAsync("http://localhost/Home/NotAnAction");
// Assert // Assert
Assert.Equal(404, response.StatusCode); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
} }
[Fact] [Fact]
@ -99,15 +95,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Travel/Flight/Index"); var response = await client.GetAsync("http://localhost/Travel/Flight/Index");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/Travel/Flight/Index", result.ExpectedUrls); Assert.Contains("/Travel/Flight/Index", result.ExpectedUrls);
@ -128,13 +124,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Travel/Flight/BuyTickets"); var response = await client.GetAsync("http://localhost/Travel/Flight/BuyTickets");
// Assert // Assert
Assert.Equal(404, response.StatusCode); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
} }
[Fact] [Fact]
@ -142,15 +138,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Store/Shop/Products"); var response = await client.GetAsync("http://localhost/Store/Shop/Products");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/Store/Shop/Products", result.ExpectedUrls); Assert.Contains("/Store/Shop/Products", result.ExpectedUrls);
@ -172,13 +168,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Store/ListProducts"); var response = await client.GetAsync("http://localhost/Store/ListProducts");
// Assert // Assert
Assert.Equal(404, response.StatusCode); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
} }
// There's two actions at this URL - but attribute routes go in the route table // There's two actions at this URL - but attribute routes go in the route table
@ -188,15 +184,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Home/About"); var response = await client.GetAsync("http://localhost/Home/About");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/Home/About", result.ExpectedUrls); Assert.Contains("/Home/About", result.ExpectedUrls);
@ -209,15 +205,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Blog/Edit/5"); var response = await client.GetAsync("http://localhost/Blog/Edit/5");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/Blog/Edit/5", result.ExpectedUrls); Assert.Contains("/Blog/Edit/5", result.ExpectedUrls);
@ -243,15 +239,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/api/Employee"); var response = await client.GetAsync("http://localhost/api/Employee");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -268,15 +264,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.SendAsync(verb, "http://localhost/api/Employee"); var message = new HttpRequestMessage(new HttpMethod(verb), "http://localhost/api/Employee");
var response = await client.SendAsync(message);
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -290,15 +287,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.SendAsync("MERGE", "http://localhost/api/Employee/5"); var message = new HttpRequestMessage(new HttpMethod("MERGE"), "http://localhost/api/Employee/5");
var response = await client.SendAsync(message);
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -315,15 +313,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.SendAsync(verb, "http://localhost/api/Employee/5/Administrator"); var message = new HttpRequestMessage(new HttpMethod(verb), "http://localhost/api/Employee/5/Administrator");
var response = await client.SendAsync(message);
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/api/Employee/5/Administrator", result.ExpectedUrls); Assert.Contains("/api/Employee/5/Administrator", result.ExpectedUrls);
@ -340,15 +339,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Manager/5"); var response = await client.GetAsync("http://localhost/Manager/5");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/Manager/5", result.ExpectedUrls); Assert.Contains("/Manager/5", result.ExpectedUrls);
@ -365,15 +364,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Team/5"); var response = await client.GetAsync("http://localhost/Team/5");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/Team/5", result.ExpectedUrls); Assert.Contains("/Team/5", result.ExpectedUrls);
@ -390,15 +389,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Teams"); var response = await client.GetAsync("http://localhost/Teams");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/Teams", result.ExpectedUrls); Assert.Contains("/Teams", result.ExpectedUrls);
@ -411,7 +410,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetStringAsync("http://localhost/Organization/5"); var response = await client.GetStringAsync("http://localhost/Organization/5");
@ -426,7 +425,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetStringAsync("http://localhost/Teams/AllTeams"); var response = await client.GetStringAsync("http://localhost/Teams/AllTeams");
@ -441,15 +440,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/api/Employee").To(new { }); var url = LinkFrom("http://localhost/api/Employee").To(new { });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -464,15 +463,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/api/Employee").To(new { action = "Get", id = 5 }); var url = LinkFrom("http://localhost/api/Employee").To(new { action = "Get", id = 5 });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -487,16 +486,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/api/Employee").To(new { action = "ShowPosts", controller = "Blog" }); var url = LinkFrom("http://localhost/api/Employee").To(new { action = "ShowPosts", controller = "Blog" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -511,15 +510,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/api/Employee").To(new { action = "Index", controller = "Home" }); var url = LinkFrom("http://localhost/api/Employee").To(new { action = "Index", controller = "Home" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -536,14 +535,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.SendAsync(method, "http://localhost/api/Company/5"); var message = new HttpRequestMessage(new HttpMethod(method), "http://localhost/api/Company/5");
Assert.Equal(200, response.StatusCode); var response = await client.SendAsync(message);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -559,14 +559,14 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.SendAsync("DELETE", "http://localhost/api/Company/5"); var response = await client.DeleteAsync("http://localhost/api/Company/5");
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -582,17 +582,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
var url = LinkFrom("http://localhost") var url = LinkFrom("http://localhost")
.To(new { id = 5 }); .To(new { id = 5 });
// Act // Act
var response = await client.SendAsync("GET", "http://localhost/api/Company/5/Employees"); var response = await client.GetAsync("http://localhost/api/Company/5/Employees");
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -608,14 +608,14 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.SendAsync("GET", "http://localhost/api/Company/5/Departments"); var response = await client.GetAsync("http://localhost/api/Company/5/Departments");
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -635,13 +635,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
var expectedMessage = "The supplied route name 'DuplicateRoute' is ambiguous and matched more than one route."; var expectedMessage = "The supplied route name 'DuplicateRoute' is ambiguous and matched more than one route.";
// Act // Act
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () => var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await client.SendAsync("GET", url)); await client.GetAsync(url));
// Assert // Assert
Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedMessage, ex.Message);
@ -652,16 +652,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/") var url = LinkFrom("http://localhost/")
.To(new { action = "BuyTickets", controller = "Flight", area = "Travel" }); .To(new { action = "BuyTickets", controller = "Flight", area = "Travel" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -676,15 +676,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/Travel/Flight").To(new { action = "BuyTickets" }); var url = LinkFrom("http://localhost/Travel/Flight").To(new { action = "BuyTickets" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -699,15 +699,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/Travel/Flight").To(new { action = "Index", controller = "Home", area = "" }); var url = LinkFrom("http://localhost/Travel/Flight").To(new { action = "Index", controller = "Home", area = "" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -722,15 +722,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/Travel/Flight").To(new { action = "Contact", controller = "Home", }); var url = LinkFrom("http://localhost/Travel/Flight").To(new { action = "Contact", controller = "Home", });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -745,16 +745,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/api/Employee") var url = LinkFrom("http://localhost/api/Employee")
.To(new { action = "Schedule", controller = "Rail", area = "Travel" }); .To(new { action = "Schedule", controller = "Rail", area = "Travel" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -769,15 +769,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/ContosoCorp/Trains/CheckSchedule").To(new { action = "Index" }); var url = LinkFrom("http://localhost/ContosoCorp/Trains/CheckSchedule").To(new { action = "Index" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -792,16 +792,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/ContosoCorp/Trains/CheckSchedule") var url = LinkFrom("http://localhost/ContosoCorp/Trains/CheckSchedule")
.To(new { action = "Index", controller = "Home", area = "" }); .To(new { action = "Index", controller = "Home", area = "" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -816,16 +816,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/ContosoCorp/Trains") var url = LinkFrom("http://localhost/ContosoCorp/Trains")
.To(new { action = "Contact", controller = "Home", }); .To(new { action = "Contact", controller = "Home", });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -840,17 +840,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/ContosoCorp/Trains") var url = LinkFrom("http://localhost/ContosoCorp/Trains")
.To(new { action = "Index", controller = "Flight", }); .To(new { action = "Index", controller = "Flight", });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -865,17 +865,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/Travel/Flight") var url = LinkFrom("http://localhost/Travel/Flight")
.To(new { action = "Index", controller = "Rail", }); .To(new { action = "Index", controller = "Rail", });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -890,17 +890,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/Travel/Flight") var url = LinkFrom("http://localhost/Travel/Flight")
.To(new { action = "ListUsers", controller = "UserManagement", area = "Admin" }); .To(new { action = "ListUsers", controller = "UserManagement", area = "Admin" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -915,17 +915,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = LinkFrom("http://localhost/ContosoCorp/Trains") var url = LinkFrom("http://localhost/ContosoCorp/Trains")
.To(new { action = "ListUsers", controller = "UserManagement", area = "Admin" }); .To(new { action = "ListUsers", controller = "UserManagement", area = "Admin" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Assert // Assert
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -940,15 +940,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/api/Products/US/GetProducts"); var response = await client.GetAsync("http://localhost/api/Products/US/GetProducts");
// Assert // Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
Assert.Contains("/api/Products/US/GetProducts", result.ExpectedUrls); Assert.Contains("/api/Products/US/GetProducts", result.ExpectedUrls);
@ -970,13 +970,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Products/GetProducts"); var response = await client.GetAsync("http://localhost/Products/GetProducts");
// Assert // Assert
Assert.Equal(404, response.StatusCode); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
} }
[Fact] [Fact]
@ -984,7 +984,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = var url =
@ -992,7 +992,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
.To(new { action = "GetProducts", controller = "Products", country = "US" }); .To(new { action = "GetProducts", controller = "Products", country = "US" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -1004,7 +1004,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = var url =
@ -1012,7 +1012,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
.To(new { action = "GetProducts", controller = "Products", country = "CA" }); .To(new { action = "GetProducts", controller = "Products", country = "CA" });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert
@ -1024,7 +1024,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var url = var url =
@ -1032,7 +1032,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
.To(new { action = "GetProducts", controller = "Products", country = (string)null }); .To(new { action = "GetProducts", controller = "Products", country = (string)null });
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
var body = await response.ReadBodyAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RoutingResult>(body); var result = JsonConvert.DeserializeObject<RoutingResult>(body);
// Assert // Assert

View File

@ -3,35 +3,29 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using ValueProvidersSite;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
using ValueProvidersSite;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.FunctionalTests namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
public class ValueProviderTest public class ValueProviderTest
{ {
private readonly IServiceProvider _services; private readonly IServiceProvider _services = TestHelper.CreateServices("ValueProvidersSite");
private readonly Action<IBuilder> _app = new Startup().Configure; private readonly Action<IBuilder> _app = new Startup().Configure;
public ValueProviderTest()
{
_services = TestHelper.CreateServices("ValueProvidersSite");
}
[Fact] [Fact]
public async Task ValueProviderFactories_AreVisitedInSequentialOrder_ForValueProviders() public async Task ValueProviderFactories_AreVisitedInSequentialOrder_ForValueProviders()
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Home/TestValueProvider?test=not-test-value"); var body = await client.GetStringAsync("http://localhost/Home/TestValueProvider?test=not-test-value");
// Assert // Assert
var body = await response.ReadBodyAsStringAsync();
Assert.Equal("custom-value-provider-value", body.Trim()); Assert.Equal("custom-value-provider-value", body.Trim());
} }
@ -40,13 +34,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Home/DefaultValueProviders?test=query-value"); var body = await client.GetStringAsync("http://localhost/Home/DefaultValueProviders?test=query-value");
// Assert // Assert
var body = await response.ReadBodyAsStringAsync();
Assert.Equal("query-value", body.Trim()); Assert.Equal("query-value", body.Trim());
} }
@ -55,13 +48,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/RouteTest/route-value"); var body = await client.GetStringAsync("http://localhost/RouteTest/route-value");
// Assert // Assert
var body = await response.ReadBodyAsStringAsync();
Assert.Equal("route-value", body.Trim()); Assert.Equal("route-value", body.Trim());
} }
} }

View File

@ -57,13 +57,12 @@ ViewWithNestedLayout-Content
public async Task RazorView_ExecutesPageAndLayout(string actionName, string expected) public async Task RazorView_ExecutesPageAndLayout(string actionName, string expected)
{ {
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var result = await client.GetAsync("http://localhost/ViewEngine/" + actionName); var body = await client.GetStringAsync("http://localhost/ViewEngine/" + actionName);
// Assert // Assert
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
Assert.Equal(expected, body.Trim()); Assert.Equal(expected, body.Trim());
} }
@ -76,13 +75,12 @@ ViewWithNestedLayout-Content
</partial> </partial>
test-value"; test-value";
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var result = await client.GetAsync("http://localhost/ViewEngine/ViewWithPartial"); var body = await client.GetStringAsync("http://localhost/ViewEngine/ViewWithPartial");
// Assert // Assert
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
Assert.Equal(expected, body.Trim()); Assert.Equal(expected, body.Trim());
} }
@ -95,13 +93,12 @@ test-value";
partial-content partial-content
component-content"; component-content";
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.Handler; var client = server.CreateClient();
// Act // Act
var result = await client.GetAsync("http://localhost/ViewEngine/ViewPassesViewDataToLayout"); var body = await client.GetStringAsync("http://localhost/ViewEngine/ViewPassesViewDataToLayout");
// Assert // Assert
var body = await result.HttpContext.Response.ReadBodyAsStringAsync();
Assert.Equal(expected, body.Trim()); Assert.Equal(expected, body.Trim());
} }
} }

View File

@ -3,8 +3,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.Net;
using System.Text; using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
@ -27,20 +28,19 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
var headers = new Dictionary<string, string[]>();
headers.Add("Accept", new string[] { "application/xml;charset=utf-8" });
// Act // Act
var response = await client.SendAsync("POST", var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Home/GetDummyClass?sampleInput=10");
"http://localhost/Home/GetDummyClass?sampleInput=10", headers, null, null); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
var response = await client.SendAsync(request);
//Assert //Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" " + Assert.Equal("<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns=\"http://schemas.datacontract.org/2004/07/FormatterWebSite\">" + "xmlns=\"http://schemas.datacontract.org/2004/07/FormatterWebSite\">" +
"<SampleInt>10</SampleInt></DummyClass>", "<SampleInt>10</SampleInt></DummyClass>",
new StreamReader(response.Body, Encoding.UTF8).ReadToEnd()); await response.Content.ReadAsStringAsync());
} }
[Fact] [Fact]
@ -48,19 +48,18 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
var headers = new Dictionary<string, string[]>();
headers.Add("Accept", new string[] { "application/xml;charset=utf-8" });
// Act // Act
var response = await client.SendAsync("POST", var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/XmlSerializer/GetDummyClass?sampleInput=10");
"http://localhost/XmlSerializer/GetDummyClass?sampleInput=10", headers, null, null); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
var response = await client.SendAsync(request);
//Assert //Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + Assert.Equal("<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>", "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>",
new StreamReader(response.Body, Encoding.UTF8).ReadToEnd()); await response.Content.ReadAsStringAsync());
} }
[Fact] [Fact]
@ -68,20 +67,20 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
// Arrange // Arrange
var server = TestServer.Create(_services, _app); var server = TestServer.Create(_services, _app);
var client = server.Handler; var client = server.CreateClient();
var headers = new Dictionary<string, string[]>();
headers.Add("Accept", new string[] { "application/xml;charset=utf-8" });
// Act // Act
var response = await client.SendAsync("POST", var request = new HttpRequestMessage(HttpMethod.Post,
"http://localhost/DataContractSerializer/GetPerson?name=HelloWorld", headers, null, null); "http://localhost/DataContractSerializer/GetPerson?name=HelloWorld");
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
var response = await client.SendAsync(request);
//Assert //Assert
Assert.Equal(200, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("<Person xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" " + Assert.Equal("<Person xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns=\"http://schemas.datacontract.org/2004/07/FormatterWebSite\">" + "xmlns=\"http://schemas.datacontract.org/2004/07/FormatterWebSite\">" +
"<Name>HelloWorld</Name></Person>", "<Name>HelloWorld</Name></Person>",
new StreamReader(response.Body, Encoding.UTF8).ReadToEnd()); await response.Content.ReadAsStringAsync());
} }
} }
} }

View File

@ -26,6 +26,9 @@ namespace BasicWebSite.Controllers
public async Task ActionReturningTask() public async Task ActionReturningTask()
{ {
// TODO: #1077. With HttpResponseMessage, there seems to be a race between the write operation setting the
// header to 200 and NoContentResult returned by the action invoker setting it to 204.
Context.Response.StatusCode = 204;
await Context.Response.WriteAsync("Hello world"); await Context.Response.WriteAsync("Hello world");
} }
} }