public async Task ObjectResult_ExecuteResultAsync_SetsStatusCode() { // Arrange var result = new ObjectResult("Hello") { StatusCode = 404, Formatters = new List<IOutputFormatter>() { new NoOpOutputFormatter(), }, }; var actionContext = new ActionContext() { HttpContext = new DefaultHttpContext() { RequestServices = CreateServices(), } }; // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.Equal(404, actionContext.HttpContext.Response.StatusCode); }
public async Task ObjectResult_MatchAllContentType_Throws(string content, string invalidContentType) { // Arrange var contentTypes = content.Split(','); var objectResult = new ObjectResult(new Person() { Name = "John" }); objectResult.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType)) .ToList(); var actionContext = CreateMockActionContext(); // Act & Assert var exception = await Assert.ThrowsAsync <InvalidOperationException>( () => objectResult.ExecuteResultAsync(actionContext)); var expectedMessage = string.Format("The content-type '{0}' added in the 'ContentTypes' property is " + "invalid. Media types which match all types or match all subtypes are not supported.", invalidContentType); Assert.Equal(expectedMessage, exception.Message); }
public async Task ObjectResult_WithSingleContentType_TheContentTypeIsIgnoredIfTheTypeIsString() { // Arrange var contentType = "application/json;charset=utf-8"; var expectedContentType = "text/plain; charset=utf-8"; // string value. var input = "1234"; var httpResponse = GetMockHttpResponse(); var actionContext = CreateMockActionContext(httpResponse.Object); // Set the content type property explicitly to a single value. var result = new ObjectResult(input); result.ContentTypes = new List <MediaTypeHeaderValue>(); result.ContentTypes.Add(MediaTypeHeaderValue.Parse(contentType)); // Act await result.ExecuteResultAsync(actionContext); // Assert httpResponse.VerifySet(r => r.ContentType = expectedContentType); }
public async Task ObjectResult_WithMultipleContentTypes_Ignores406Formatter() { // Arrange var objectResult = new ObjectResult(new Person() { Name = "John" }); objectResult.ContentTypes.Add(new MediaTypeHeaderValue("application/foo")); objectResult.ContentTypes.Add(new MediaTypeHeaderValue("application/json")); var outputFormatters = new IOutputFormatter[] { new HttpNotAcceptableOutputFormatter(), new JsonOutputFormatter() }; var response = new Mock <HttpResponse>(); var responseStream = new MemoryStream(); response.SetupGet(r => r.Body).Returns(responseStream); var expectedData = "{\"Name\":\"John\"}"; var actionContext = CreateMockActionContext( outputFormatters, response.Object, requestAcceptHeader: "application/non-existing", requestContentType: "application/non-existing"); // Act await objectResult.ExecuteResultAsync(actionContext); // Assert response.VerifySet(r => r.ContentType = "application/json; charset=utf-8"); responseStream.Position = 0; var actual = new StreamReader(responseStream).ReadToEnd(); Assert.Equal(expectedData, actual); }
public async Task ObjectResult_WithSingleContentType_TheGivenContentTypeIsSelected() { // Arrange var expectedContentType = "application/json; charset=utf-8"; // non string value. var input = 123; var httpResponse = new DefaultHttpContext().Response; httpResponse.Body = new MemoryStream(); var actionContext = CreateMockActionContext(httpResponse); // Set the content type property explicitly to a single value. var result = new ObjectResult(input); result.ContentTypes = new List <MediaTypeHeaderValue>(); result.ContentTypes.Add(MediaTypeHeaderValue.Parse(expectedContentType)); // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.Equal(expectedContentType, httpResponse.ContentType); }
public async Task ObjectResult_WithMultipleContentTypes_Ignores406Formatter() { // Arrange var objectResult = new ObjectResult(new Person() { Name = "John" }); objectResult.ContentTypes.Add(new MediaTypeHeaderValue("application/foo")); objectResult.ContentTypes.Add(new MediaTypeHeaderValue("application/json")); var outputFormatters = new IOutputFormatter[] { new HttpNotAcceptableOutputFormatter(), new JsonOutputFormatter() }; var response = new Mock<HttpResponse>(); var responseStream = new MemoryStream(); response.SetupGet(r => r.Body).Returns(responseStream); var expectedData = "{\"Name\":\"John\"}"; var actionContext = CreateMockActionContext( outputFormatters, response.Object, requestAcceptHeader: "application/non-existing", requestContentType: "application/non-existing"); // Act await objectResult.ExecuteResultAsync(actionContext); // Assert response.VerifySet(r => r.ContentType = "application/json; charset=utf-8"); responseStream.Position = 0; var actual = new StreamReader(responseStream).ReadToEnd(); Assert.Equal(expectedData, actual); }
public async Task ObjectResult_WithStringType_WritesTextPlain_Ignoring406Formatter() { // Arrange var expectedData = "Hello World!"; var objectResult = new ObjectResult(expectedData); var outputFormatters = new IOutputFormatter[] { new HttpNotAcceptableOutputFormatter(), new StringOutputFormatter(), new JsonOutputFormatter() }; var response = new Mock<HttpResponse>(); var responseStream = new MemoryStream(); response.SetupGet(r => r.Body).Returns(responseStream); var actionContext = CreateMockActionContext( outputFormatters, response.Object, requestAcceptHeader: "application/json"); // Act await objectResult.ExecuteResultAsync(actionContext); // Assert response.VerifySet(r => r.ContentType = "text/plain; charset=utf-8"); responseStream.Position = 0; var actual = new StreamReader(responseStream).ReadToEnd(); Assert.Equal(expectedData, actual); }
public async Task ObjectResult_MatchAllContentType_Throws(string content, string invalidContentType) { // Arrange var contentTypes = content.Split(','); var objectResult = new ObjectResult(new Person() { Name = "John" }); objectResult.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType)) .ToList(); var actionContext = CreateMockActionContext(); // Act & Assert var exception = await Assert.ThrowsAsync<InvalidOperationException>( () => objectResult.ExecuteResultAsync(actionContext)); var expectedMessage = string.Format("The content-type '{0}' added in the 'ContentTypes' property is " + "invalid. Media types which match all types or match all subtypes are not supported.", invalidContentType); Assert.Equal(expectedMessage, exception.Message); }
public async Task ObjectResult_WithMultipleContentTypesAndAcceptHeaders_PerformsContentNegotiation( IEnumerable<string> contentTypes, string acceptHeader, string expectedHeader) { // Arrange var expectedContentType = expectedHeader; var input = "testInput"; var stream = new MemoryStream(); var httpResponse = new Mock<HttpResponse>(); var tempContentType = string.Empty; httpResponse.SetupProperty<string>(o => o.ContentType); httpResponse.SetupGet(r => r.Body).Returns(stream); var actionContext = CreateMockActionContext(httpResponse.Object, acceptHeader); var result = new ObjectResult(input); // Set the content type property explicitly. result.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType)).ToList(); result.Formatters = new List<IOutputFormatter> { new CannotWriteFormatter(), new JsonOutputFormatter(), }; // Act await result.ExecuteResultAsync(actionContext); // Assert // should always select the Json Output formatter even though it is second in the list. httpResponse.VerifySet(r => r.ContentType = expectedContentType); }
public async Task ObjectResult_MultipleContentTypes_PicksFirstFormatterWhichSupportsAnyOfTheContentTypes() { // Arrange var expectedContentType = "application/json; charset=utf-8"; var input = "testInput"; var httpResponse = GetMockHttpResponse(); var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: null); var result = new ObjectResult(input); // It should not select TestOutputFormatter, // This is because it should accept the first formatter which supports any of the two contentTypes. var contentTypes = new[] { "application/custom", "application/json" }; // Set the content type and the formatters property explicitly. result.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType)) .ToList(); result.Formatters = new List<IOutputFormatter> { new CannotWriteFormatter(), new JsonOutputFormatter(), }; // Act await result.ExecuteResultAsync(actionContext); // Assert // Asserts that content type is not text/custom. httpResponse.VerifySet(r => r.ContentType = expectedContentType); }
public async Task ObjectResult_WithSingleContentType_TheGivenContentTypeIsSelected() { // Arrange var expectedContentType = "application/json; charset=utf-8"; // non string value. var input = 123; var httpResponse = new DefaultHttpContext().Response; httpResponse.Body = new MemoryStream(); var actionContext = CreateMockActionContext(httpResponse); // Set the content type property explicitly to a single value. var result = new ObjectResult(input); result.ContentTypes = new List<MediaTypeHeaderValue>(); result.ContentTypes.Add(MediaTypeHeaderValue.Parse(expectedContentType)); // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.Equal(expectedContentType, httpResponse.ContentType); }
public async Task ObjectResult_Execute_NullContent_SetsStatusCode() { // Arrange var stream = new MemoryStream(); var expectedStatusCode = StatusCodes.Status201Created; var httpResponse = new Mock<HttpResponse>(); httpResponse.SetupGet(r => r.Body).Returns(stream); var formatters = new IOutputFormatter[] { new HttpNoContentOutputFormatter(), new StringOutputFormatter(), new JsonOutputFormatter() }; var actionContext = CreateMockActionContext(formatters, httpResponse.Object, requestAcceptHeader: null, requestContentType: null); var result = new ObjectResult(null); result.StatusCode = expectedStatusCode; // Act await result.ExecuteResultAsync(actionContext); // Assert httpResponse.VerifySet(r => r.StatusCode = expectedStatusCode); Assert.Equal(0, httpResponse.Object.Body.Length); }
public async Task ObjectResult_NoFormatterFound_Returns406() { // Arrange var stream = new MemoryStream(); var httpResponse = new Mock<HttpResponse>(); httpResponse.SetupProperty<string>(o => o.ContentType); httpResponse.SetupGet(r => r.Body).Returns(stream); var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: null, requestContentType: null); var input = "testInput"; var result = new ObjectResult(input); // Set more than one formatters. The test output formatter throws on write. result.Formatters = new List<IOutputFormatter> { new CannotWriteFormatter(), }; // Act await result.ExecuteResultAsync(actionContext); // Assert // Asserts that content type is not text/custom. httpResponse.VerifySet(r => r.StatusCode = StatusCodes.Status406NotAcceptable); }
public async Task ObjectResult_Execute_CallsContentResult_SetsContent() { // Arrange var expectedContentType = "text/plain; charset=utf-8"; var input = "testInput"; var stream = new MemoryStream(); var httpResponse = new Mock<HttpResponse>(); httpResponse.SetupProperty<string>(o => o.ContentType); httpResponse.SetupGet(r => r.Body).Returns(stream); var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: null, requestContentType: null); // Act var result = new ObjectResult(input); await result.ExecuteResultAsync(actionContext); // Assert httpResponse.VerifySet(r => r.ContentType = expectedContentType); // The following verifies the correct Content was written to Body Assert.Equal(input.Length, httpResponse.Object.Body.Length); }
ObjectResult_NoContentTypeSetWithNoAcceptHeadersAndNoRequestContentType_PicksFirstFormatterWhichCanWrite() { // Arrange var stream = new MemoryStream(); var expectedContentType = "application/json; charset=utf-8"; var httpResponse = new Mock<HttpResponse>(); httpResponse.SetupProperty<string>(o => o.ContentType); httpResponse.SetupGet(r => r.Body).Returns(stream); var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: null, requestContentType: null); var input = "testInput"; var result = new ObjectResult(input); // Set more than one formatters. The test output formatter throws on write. result.Formatters = new List<IOutputFormatter> { new CannotWriteFormatter(), new JsonOutputFormatter(), }; // Act await result.ExecuteResultAsync(actionContext); // Assert // Asserts that content type is not text/custom. httpResponse.VerifySet(r => r.ContentType = expectedContentType); }
public async Task ObjectResult_NoContentTypeSetWithAcceptHeaders_PicksFormatterOnAcceptHeaders() { // Arrange var expectedContentType = "application/json; charset=utf-8"; var input = "testInput"; var stream = new MemoryStream(); var httpResponse = GetMockHttpResponse(); var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: "text/custom;q=0.1,application/json;q=0.9", requestContentType: "application/custom"); var result = new ObjectResult(input); // Set more than one formatters. The test output formatter throws on write. result.Formatters = new List<IOutputFormatter> { new CannotWriteFormatter(), new JsonOutputFormatter(), }; // Act await result.ExecuteResultAsync(actionContext); // Assert // Asserts that content type is not text/custom. i.e the formatter is not TestOutputFormatter. httpResponse.VerifySet(r => r.ContentType = expectedContentType); }
public async Task ObjectResult_MultipleFormattersSupportingTheSameContentType_SelectsTheFirstFormatterInList() { // Arrange var input = "testInput"; var stream = new MemoryStream(); var httpResponse = GetMockHttpResponse(); var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: null); var result = new ObjectResult(input); // It should select the mock formatter as that is the first one in the list. var contentTypes = new[] { "application/json", "text/custom" }; var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse("text/custom"); // Get a mock formatter which supports everything. var mockFormatter = GetMockFormatter(); result.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType)).ToList(); result.Formatters = new List<IOutputFormatter> { mockFormatter.Object, new JsonOutputFormatter(), new CannotWriteFormatter() }; // Act await result.ExecuteResultAsync(actionContext); // Assert // Verify that mock formatter was chosen. mockFormatter.Verify(o => o.WriteAsync(It.IsAny<OutputFormatterContext>())); }
public async Task ObjectResult_WithStream_DoesNotSetContentType_IfNotProvided() { // Arrange var objectResult = new ObjectResult(new MemoryStream(Encoding.UTF8.GetBytes("Name=James"))); var outputFormatters = new IOutputFormatter[] { new StreamOutputFormatter(), new JsonOutputFormatter() }; var response = new Mock<HttpResponse>(); var responseStream = new MemoryStream(); response.SetupGet(r => r.Body).Returns(responseStream); var expectedData = "Name=James"; var actionContext = CreateMockActionContext( outputFormatters, response.Object, requestAcceptHeader: null, requestContentType: null); // Act await objectResult.ExecuteResultAsync(actionContext); // Assert response.VerifySet(r => r.ContentType = It.IsAny<string>(), Times.Never()); responseStream.Position = 0; var actual = new StreamReader(responseStream).ReadToEnd(); Assert.Equal(expectedData, actual); }
public async Task NoAcceptAndContentTypeHeaders_406Formatter_DoesNotTakeEffect() { // Arrange var expectedContentType = "application/json; charset=utf-8"; var input = 123; var httpResponse = new DefaultHttpContext().Response; httpResponse.Body = new MemoryStream(); var actionContext = CreateMockActionContext( outputFormatters: new IOutputFormatter[] { new HttpNotAcceptableOutputFormatter(), new JsonOutputFormatter() }, response: httpResponse, requestAcceptHeader: null, requestContentType: null, requestAcceptCharsetHeader: null); var result = new ObjectResult(input); result.ContentTypes = new List<MediaTypeHeaderValue>(); result.ContentTypes.Add(MediaTypeHeaderValue.Parse(expectedContentType)); // Act await result.ExecuteResultAsync(actionContext); // Assert Assert.Equal(expectedContentType, httpResponse.ContentType); }
public async Task ObjectResult_WithStream_SetsExplicitContentType() { // Arrange var objectResult = new ObjectResult(new MemoryStream(Encoding.UTF8.GetBytes("Name=James"))); objectResult.ContentTypes.Add(new MediaTypeHeaderValue("application/foo")); var outputFormatters = new IOutputFormatter[] { new StreamOutputFormatter(), new JsonOutputFormatter() }; var response = new Mock<HttpResponse>(); var responseStream = new MemoryStream(); response.SetupGet(r => r.Body).Returns(responseStream); var expectedData = "Name=James"; var actionContext = CreateMockActionContext( outputFormatters, response.Object, requestAcceptHeader: "application/json", requestContentType: null); // Act await objectResult.ExecuteResultAsync(actionContext); // Assert response.VerifySet(r => r.ContentType = "application/foo"); responseStream.Position = 0; var actual = new StreamReader(responseStream).ReadToEnd(); Assert.Equal(expectedData, actual); }
public async Task ObjectResult_PerformsContentNegotiation_OnAllMediaRangeAcceptHeaderMediaType( string acceptHeader, string expectedResponseContentType) { // Arrange var objectResult = new ObjectResult(new Person() { Name = "John" }); var outputFormatters = new IOutputFormatter[] { new HttpNoContentOutputFormatter(), new StringOutputFormatter(), new JsonOutputFormatter(), new XmlDataContractSerializerOutputFormatter() }; var response = GetMockHttpResponse(); var actionContext = CreateMockActionContext( outputFormatters, response.Object, requestAcceptHeader: acceptHeader, respectBrowserAcceptHeader: true); // Act await objectResult.ExecuteResultAsync(actionContext); // Assert response.VerifySet(resp => resp.ContentType = expectedResponseContentType); }
public async Task ObjectResult_FallsBackOn_FormattersInOptions() { // Arrange var formatter = GetMockFormatter(); var actionContext = CreateMockActionContext( new[] { formatter.Object }, setupActionBindingContext: false); // Set the content type property explicitly to a single value. var result = new ObjectResult("someValue"); // Act await result.ExecuteResultAsync(actionContext); // Assert formatter.Verify(o => o.WriteAsync(It.IsAny<OutputFormatterContext>())); }
public async Task ObjectResult_WildcardAcceptMediaType_AndExplicitResponseContentType( string acceptHeader, string expectedResponseContentType, bool respectBrowserAcceptHeader) { // Arrange var objectResult = new ObjectResult(new Person() { Name = "John" }); objectResult.ContentTypes.Add(MediaTypeHeaderValue.Parse("application/xml")); objectResult.ContentTypes.Add(MediaTypeHeaderValue.Parse("application/json")); var outputFormatters = new IOutputFormatter[] { new HttpNoContentOutputFormatter(), new StringOutputFormatter(), new JsonOutputFormatter(), new XmlDataContractSerializerOutputFormatter() }; var response = GetMockHttpResponse(); var actionContext = CreateMockActionContext( outputFormatters, response.Object, acceptHeader, respectBrowserAcceptHeader: respectBrowserAcceptHeader); // Act await objectResult.ExecuteResultAsync(actionContext); // Assert response.VerifySet(resp => resp.ContentType = expectedResponseContentType); }
public async Task ObjectResult_Execute_CallsJsonResult_SetsContent() { // Arrange var expectedContentType = "application/json; charset=utf-8"; var nonStringValue = new { x1 = 10, y1 = "Hello" }; var httpResponse = Mock.Of<HttpResponse>(); httpResponse.Body = new MemoryStream(); var actionContext = CreateMockActionContext(httpResponse); var tempStream = new MemoryStream(); var tempHttpContext = new Mock<HttpContext>(); var tempHttpResponse = new Mock<HttpResponse>(); tempHttpResponse.SetupGet(o => o.Body).Returns(tempStream); tempHttpResponse.SetupProperty<string>(o => o.ContentType); tempHttpContext.SetupGet(o => o.Request).Returns(new DefaultHttpContext().Request); tempHttpContext.SetupGet(o => o.Response).Returns(tempHttpResponse.Object); var tempActionContext = new ActionContext(tempHttpContext.Object, new RouteData(), new ActionDescriptor()); var formatterContext = new OutputFormatterContext() { HttpContext = tempActionContext.HttpContext, Object = nonStringValue, DeclaredType = nonStringValue.GetType() }; var formatter = new JsonOutputFormatter(); formatter.WriteResponseHeaders(formatterContext); await formatter.WriteAsync(formatterContext); // Act var result = new ObjectResult(nonStringValue); await result.ExecuteResultAsync(actionContext); // Assert Assert.Equal(expectedContentType, httpResponse.ContentType); Assert.Equal(tempStream.ToArray(), ((MemoryStream)actionContext.HttpContext.Response.Body).ToArray()); }
public async Task ObjectResult_WithSingleContentType_TheContentTypeIsIgnoredIfTheTypeIsString() { // Arrange var contentType = "application/json;charset=utf-8"; var expectedContentType = "text/plain; charset=utf-8"; // string value. var input = "1234"; var httpResponse = GetMockHttpResponse(); var actionContext = CreateMockActionContext(httpResponse.Object); // Set the content type property explicitly to a single value. var result = new ObjectResult(input); result.ContentTypes = new List<MediaTypeHeaderValue>(); result.ContentTypes.Add(MediaTypeHeaderValue.Parse(contentType)); // Act await result.ExecuteResultAsync(actionContext); // Assert httpResponse.VerifySet(r => r.ContentType = expectedContentType); }