protected FormattedContentResult <T> Serialize <T>(T content, string contentType) { FormattedContentResult <T> result = null; if (contentType == JsonContentType) { result = new FormattedContentResult <T>( HttpStatusCode.OK, content, new JsonMediaTypeFormatter(), new MediaTypeHeaderValue(JsonMediaType), this ); } if (contentType == XmlContentType) { result = new FormattedContentResult <T>( HttpStatusCode.OK, content, new XmlMediaTypeFormatter(), new MediaTypeHeaderValue(XmlMediaType), this); } return(result); }
/// <summary> /// Helper method that creates a <see cref="HttpResponseMessage"/> with an <see cref="ObjectContent{T}"/> instance containing the provided /// <paramref name="value"/> and the given <paramref name="formatter"/>. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="request">The request.</param> /// <param name="statusCode">The status code of the created response.</param> /// <param name="value">The value to wrap. Can be <c>null</c>.</param> /// <param name="formatter">The formatter to use.</param> /// <param name="mediaType">The media type override to set on the response's content. Can be <c>null</c>.</param> /// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns> public static HttpResponseMessage CreateResponse <T>( this HttpRequestMessage request, HttpStatusCode statusCode, T value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType ) { if (request == null) { throw Error.ArgumentNull("request"); } if (formatter == null) { throw Error.ArgumentNull("formatter"); } return(FormattedContentResult <T> .Execute( statusCode, value, formatter, mediaType, request )); }
public Task <HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) { var errorContract = new ErrorContract { Message = Message, Errors = Errors }; var problemHeader = new MediaTypeHeaderValue("application/api-problem+json"); var result = new FormattedContentResult <ErrorContract>( Status, errorContract, _configuration.Formatters.JsonFormatter, problemHeader, _controller); //var contentResult = _controller != null // ? new FormattedContentResult<ErrorContract>( // Status, errorContract, _configuration.Formatters.JsonFormatter, problemHeader, _controller) // : new FormattedContentResult<ErrorContract>( // Status, errorContract, _configuration.Formatters.JsonFormatter, problemHeader, _request); return(result.ExecuteAsync(cancellationToken)); }
public async Task Test_ResendConfirmationEmail_With_FailedResult() { AppSettings.Set <ClientSettings>(new ClientSettings() { AuthenticationIssuerName = "TaskCat.Auth", ConfirmEmailPath = "confirmEmail", HostingAddress = "TestHostAddress", WebCatUrl = "WebCatUrl" }); Mock <IAccountContext> accountContextMock = new Mock <IAccountContext>(); accountContextMock.Setup(x => x.FindUser(It.IsAny <string>())).ReturnsAsync( new User(new UserRegistrationModel(), new UserProfile())); accountContextMock.Setup(x => x.NotifyUserCreationByMail(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(new SendEmailResponse(HttpStatusCode.InternalServerError, "Random mail error")); AccountController accountController = new AccountController(accountContextMock.Object); var result = await accountController.ResendConfirmationEmail("123"); Assert.IsInstanceOf <FormattedContentResult <SendEmailResponse> >(result); Assert.IsNotNull(result); FormattedContentResult <SendEmailResponse> convertedResult = result as FormattedContentResult <SendEmailResponse>; Assert.IsNotNull(convertedResult.Content); Assert.IsFalse(convertedResult.Content.Success); Assert.AreEqual(HttpStatusCode.InternalServerError, convertedResult.Content.StatusCode); }
/// <summary>Creates an <see cref="HttpResponseMessage"/> asynchronously.</summary> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A task that, when completed, contains the <see cref="HttpResponseMessage"/>.</returns> public Task <HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) { var mediaTypeHeader = new MediaTypeHeaderValue(MediaTypeHeader.MediaType); var result = new FormattedContentResult <TResponse>(StatusCode, Response, _dependencies.Formatter, mediaTypeHeader, _dependencies.RequestMessage); return(result.ExecuteAsync(cancellationToken)); }
public void ValidateMediaTypeFormatterShouldNotThrowExceptionWithSingleCorrectMediaTypeFormatter() { var actionResultWithFormatter = new FormattedContentResult<int>( HttpStatusCode.OK, 5, TestObjectFactory.GetCustomMediaTypeFormatter(), new MediaTypeHeaderValue(TestObjectFactory.MediaType), MyWebApi.Controller<WebApiController>().AndProvideTheController()); MediaTypeFormatterValidator.ValidateMediaTypeFormatter( actionResultWithFormatter, TestObjectFactory.GetCustomMediaTypeFormatter(), TestObjectFactory.GetFailingValidationAction()); }
public void ValidateMediaTypeFormatterShouldNotThrowExceptionWithSingleCorrectMediaTypeFormatter() { var actionResultWithFormatter = new FormattedContentResult <int>( HttpStatusCode.OK, 5, TestObjectFactory.GetCustomMediaTypeFormatter(), new MediaTypeHeaderValue(TestObjectFactory.MediaType), MyWebApi.Controller <WebApiController>().AndProvideTheController()); MediaTypeFormatterValidator.ValidateMediaTypeFormatter( actionResultWithFormatter, TestObjectFactory.GetCustomMediaTypeFormatter(), TestObjectFactory.GetFailingValidationAction()); }
public void Content_Returns_InstanceProvided() { // Arrange HttpStatusCode statusCode = CreateStatusCode(); object expectedContent = CreateContent(); MediaTypeFormatter formatter = CreateFormatter(); MediaTypeHeaderValue mediaType = CreateMediaType(); using (HttpRequestMessage request = CreateRequest()) { FormattedContentResult<object> result = new FormattedContentResult<object>(statusCode, expectedContent, formatter, mediaType, request); // Act object content = result.Content; // Assert Assert.Same(expectedContent, content); } }
public void StatusCode_Returns_ValueProvided() { // Arrange HttpStatusCode expectedStatusCode = CreateStatusCode(); object content = CreateContent(); MediaTypeFormatter formatter = CreateFormatter(); MediaTypeHeaderValue mediaType = CreateMediaType(); using (HttpRequestMessage request = CreateRequest()) { FormattedContentResult<object> result = new FormattedContentResult<object>(expectedStatusCode, content, formatter, mediaType, request); // Act HttpStatusCode statusCode = result.StatusCode; // Assert Assert.Equal(expectedStatusCode, statusCode); } }
public void Request_ForApiController_Throws_WhenControllerRequestIsNull() { // Arrange HttpStatusCode statusCode = CreateStatusCode(); object content = CreateContent(); MediaTypeFormatter formatter = CreateFormatter(); MediaTypeHeaderValue mediaType = CreateMediaType(); ApiController controller = CreateController(); Assert.Null(controller.Request); FormattedContentResult<object> result = new FormattedContentResult<object>(statusCode, content, formatter, mediaType, controller); // Act & Assert InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => { HttpRequestMessage ignore = result.Request; }, "ApiController.Request must not be null."); }
public void Request_ForApiController_EvaluatesOnce() { // Arrange HttpStatusCode statusCode = CreateStatusCode(); object content = CreateContent(); MediaTypeFormatter formatter = CreateFormatter(); MediaTypeHeaderValue mediaType = CreateMediaType(); ApiController controller = CreateController(); FormattedContentResult<object> result = new FormattedContentResult<object>(statusCode, content, formatter, mediaType, controller); using (HttpRequestMessage expectedRequest = CreateRequest()) { controller.Request = expectedRequest; HttpRequestMessage ignore = result.Request; using (HttpRequestMessage otherRequest = CreateRequest()) { controller.Request = otherRequest; // Act HttpRequestMessage request = result.Request; // Assert Assert.Same(expectedRequest, request); } } }
public void ExecuteAsync_ForApiController_ReturnsCorrectResponse() { // Arrange HttpStatusCode expectedStatusCode = CreateStatusCode(); object expectedContent = CreateContent(); MediaTypeFormatter expectedFormatter = CreateFormatter(); MediaTypeHeaderValue expectedMediaType = CreateMediaType(); ApiController controller = CreateController(); using (HttpRequestMessage expectedRequest = CreateRequest()) { controller.Request = expectedRequest; IHttpActionResult result = new FormattedContentResult<object>(expectedStatusCode, expectedContent, expectedFormatter, expectedMediaType, controller); // Act Task<HttpResponseMessage> task = result.ExecuteAsync(CancellationToken.None); // Assert Assert.NotNull(task); task.WaitUntilCompleted(); using (HttpResponseMessage response = task.Result) { Assert.NotNull(response); Assert.Equal(expectedStatusCode, response.StatusCode); HttpContent content = response.Content; Assert.IsType<ObjectContent<object>>(content); ObjectContent<object> typedContent = (ObjectContent<object>)content; Assert.Same(expectedContent, typedContent.Value); Assert.Same(expectedFormatter, typedContent.Formatter); Assert.NotNull(typedContent.Headers); Assert.Equal(expectedMediaType, typedContent.Headers.ContentType); Assert.Equal(expectedRequest, response.RequestMessage); } } }