private HttpResponseMessage Execute() { var httpResponseMessage = new HttpResponseMessage(); try { var negotiationResult = ContentNegotiator.Negotiate(typeof(HttpError), Request, Formatters); if (negotiationResult == null) { httpResponseMessage.StatusCode = HttpStatusCode.NotAcceptable; } else { var error = new HttpError("Internal Server Error"); foreach (var property in Exception.GetCustomProperties()) { error.Add(property.Key, property.Value); } httpResponseMessage.StatusCode = HttpStatusCode.InternalServerError; httpResponseMessage.Content = new ObjectContent <HttpError>(error, negotiationResult.Formatter, negotiationResult.MediaType); } httpResponseMessage.RequestMessage = Request; } catch { httpResponseMessage.Dispose(); throw; } return(httpResponseMessage); }
public void ContentTypeParsing() { const String contentType = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8,application/vnd.checkmate+json;version=2;q=0.1"; var types = new ContentNegotiator().Negotiate(contentType); var first = types.First(); Assert.Equal(ContentNegotiator.MediaType.Parse("text/html"), first.MediaType); Assert.Equal(1.0f, first.Quality); var second = types.Skip(1).First(); Assert.Equal(ContentNegotiator.MediaType.Parse("text/x-dvi"), second.MediaType); Assert.Equal(0.8f, second.Quality); var third = types.Skip(2).First(); Assert.Equal(ContentNegotiator.MediaType.Parse("text/plain"), third.MediaType); Assert.Equal(0.5f, third.Quality); var fourth = types.Skip(3).First(); var mediaType = ContentNegotiator.MediaType.Parse("application/vnd.checkmate+json"); Assert.Equal(mediaType, fourth.MediaType); Assert.Equal(0.1f, fourth.Quality); }
public void OtherMimeTypes() { var negotiator = new ContentNegotiator(); negotiator.ResolveContentType("multipart/form-data; boundary=----------------------------952e70d7bb94").Should().Be(MimeType.FormUrlEncoded); negotiator.ResolveContentType("multipart/form-data").Should().Be(MimeType.FormUrlEncoded); negotiator.ResolveContentType("text/javascript").Should().Be(MimeType.Js); negotiator.ResolveContentType("text/html").Should().Be(MimeType.Html); }
public void OtherMimeTypes() { var negotiator = new ContentNegotiator(); negotiator.ResolveContentType("multipart/form-data; boundary=----------------------------952e70d7bb94").Should().Be(MimeType.FormUrlEncoded); negotiator.ResolveContentType("multipart/form-data").Should().Be(MimeType.FormUrlEncoded); negotiator.ResolveContentType("text/javascript").Should().Be(MimeType.Js); negotiator.ResolveContentType("text/html").Should().Be(MimeType.Html); }
public Task WriteAsync() { _responder.SetStatusCode(_response.StatusCode); foreach (var header in _response.Headers) { _responder.SetResponseHeader(header.Key, header.Value); } var negotiator = new ContentNegotiator(_responder.GetAcceptHeaders(), _responder.GetContentWriter(), _nameSwitcher); return(negotiator.SetResponseBodyAsync(_response.GetFullBody())); }
public void TypicalAppMimeTypes() { var negotiator = new ContentNegotiator(); negotiator.ResolveContentType("application/json").Should().Be(MimeType.JSon); negotiator.ResolveContentType("application/xml").Should().Be(MimeType.Xml); negotiator.ResolveContentType("application/atom+xml").Should().Be(MimeType.Atom); negotiator.ResolveContentType("application/rss+xml").Should().Be(MimeType.Rss); // negotiator.ResolveContentType("application/soap+xml").Should().Be(MimeType.); negotiator.ResolveContentType("application/xhtml+xml").Should().Be(MimeType.Html); negotiator.ResolveContentType("application/x-www-form-urlencoded").Should().Be(MimeType.FormUrlEncoded); negotiator.ResolveContentType("application/javascript").Should().Be(MimeType.Js); negotiator.ResolveContentType("application/js").Should().Be(MimeType.Js); }
public void TypicalAppMimeTypes() { var negotiator = new ContentNegotiator(); negotiator.ResolveContentType("application/json").Should().Be(MimeType.JSon); negotiator.ResolveContentType("application/xml").Should().Be(MimeType.Xml); negotiator.ResolveContentType("application/atom+xml").Should().Be(MimeType.Atom); negotiator.ResolveContentType("application/rss+xml").Should().Be(MimeType.Rss); // negotiator.ResolveContentType("application/soap+xml").Should().Be(MimeType.); negotiator.ResolveContentType("application/xhtml+xml").Should().Be(MimeType.Html); negotiator.ResolveContentType("application/x-www-form-urlencoded").Should().Be(MimeType.FormUrlEncoded); negotiator.ResolveContentType("application/javascript").Should().Be(MimeType.Js); negotiator.ResolveContentType("application/js").Should().Be(MimeType.Js); }
public void Setup() { _supported = new List <ContentType> { ContentType.ApplicationXml, ContentType.ApplicationJson }; _negotiator = new ContentNegotiator(_supported); _response = new HttpWebResponse { Headers = new WebHeaderCollection { { HttpRequestHeader.Accept, "application/json" } }, StatusCode = HttpStatusCode.UnsupportedMediaType }; }
/// Creates an HttpResponseMessage instance asynchronously. This method determines how a HttpResponseMessage content will look like. public override Task <HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) { var result = ContentNegotiator.Negotiate(typeof(HttpError), Request, Formatters); var message = new HttpResponseMessage { RequestMessage = Request, StatusCode = result != null ? HttpStatusCode.InternalServerError : HttpStatusCode.NotAcceptable, }; if (result != null) { string alert = null; if (Exception is SqlException) { // Strip the beginning of the exception message which contains the name of the stored procedure and the argument values. We do not disclose the values to the client. var index = Exception.Message.IndexOf("::"); // The magic separator used within our stored procedures. if (index >= 0) { alert = Exception.Message.Substring(index + 2).Trim(); } } else if (Exception is UserAlertException) { alert = Exception.Message; } // Until we have converted all the exceptions thrown by our code to UserAlertException else { alert = Exception.Message; } var content = new HttpError(Exception, IncludeErrorDetail); if (!String.IsNullOrEmpty(alert)) { // Define an additional content field. content.Add("Alert", alert); } // serializes the HttpError instance either to JSON or to XML depend on requested by the client MIME type. message.Content = new ObjectContent <HttpError>(content, result.Formatter, result.MediaType); } return(Task.FromResult(message)); }
private HttpResponseMessage Execute() { var httpResponseMessage = new HttpResponseMessage(); try { var negotiationResult = ContentNegotiator.Negotiate(typeof(HttpError), Request, Formatters); if (negotiationResult == null) { httpResponseMessage.StatusCode = HttpStatusCode.NotAcceptable; } else { var error = new HttpError("Validation Failed"); foreach (var err in _exception.ValidationErrors) { if (!error.ContainsKey(err.ItemName)) { error.Add(err.ItemName, new Collection <ApiError>()); } ((ICollection <ApiError>)error[err.ItemName]).Add(new ApiError { ErrorCode = err.ErrorCode, Message = err.ErrorMessage }); } httpResponseMessage.StatusCode = HttpStatusCode.BadRequest; httpResponseMessage.Content = new ObjectContent <HttpError>(error, negotiationResult.Formatter, negotiationResult.MediaType); } httpResponseMessage.RequestMessage = Request; } catch { httpResponseMessage.Dispose(); throw; } return(httpResponseMessage); }
public void ContentTypeParsing() { const String contentType = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8,application/vnd.checkmate+json;version=2;q=0.1"; var types = new ContentNegotiator().Negotiate(contentType); var first = types.First(); Assert.Equal(ContentNegotiator.MediaType.Parse("text/html"), first.MediaType); Assert.Equal(1.0f, first.Quality); var second = types.Skip(1).First(); Assert.Equal(ContentNegotiator.MediaType.Parse("text/x-dvi"), second.MediaType); Assert.Equal(0.8f, second.Quality); var third = types.Skip(2).First(); Assert.Equal(ContentNegotiator.MediaType.Parse("text/plain"), third.MediaType); Assert.Equal(0.5f, third.Quality); var fourth = types.Skip(3).First(); var mediaType = ContentNegotiator.MediaType.Parse("application/vnd.checkmate+json"); Assert.Equal(mediaType, fourth.MediaType); Assert.Equal(0.1f, fourth.Quality); }
public IssuesController(ContentNegotiator contentNegotiator, ControllerContext controllerContext) { _contentNegotiator = contentNegotiator; _ctx = controllerContext; // _contentNegotiator.Allow(); }
public void InvalidContentTypes() { var negotiator = new ContentNegotiator(); negotiator.ResolveContentType("application/rrddff"); }
public void InvalidContentTypes() { var negotiator = new ContentNegotiator(); negotiator.ResolveContentType("application/rrddff"); }
public IssuesController(ContentNegotiator contentNegotiator, ControllerContext controllerContext) { _contentNegotiator = contentNegotiator; _ctx = controllerContext; // _contentNegotiator.Allow(); }