protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage HttpRequestMessage, CancellationToken CancellationToken) { bool Valid = false; IEnumerable <string> RequestHeaders; var ApiKeyExists = HttpRequestMessage.Headers.TryGetValues("APIKey", out RequestHeaders); if (ApiKeyExists) { if (RequestHeaders.FirstOrDefault().Equals(APIKey)) { Valid = true; } } if (!Valid) { return(HttpRequestMessage.CreateResponse(HttpStatusCode.Forbidden, "APIKey invalide")); } var Response = await base.SendAsync(HttpRequestMessage, CancellationToken); return(Response); }
public void Should_ignore_accept_language_header_values_with_invalid_quality() { // Given var values = new[] { "en-US, sv-SE;q=a" }; var rawHeaders = new Dictionary<string, IEnumerable<string>> { { "Accept-Language", values } }; // When var headers = new RequestHeaders(rawHeaders).AcceptLanguage.ToList(); // Then headers.Count.ShouldEqual(2); headers.FirstOrDefault(t => t.Item1 == "en-US" && t.Item2 == 1.0m).ShouldNotBeNull(); headers.FirstOrDefault(t => t.Item1 == "sv-SE" && t.Item2 == 1.0m).ShouldNotBeNull(); }
public void Should_ignore_accept_header_values_with_invalid_quality() { // Given var values = new[] { "text/plain, text/ninja;q=a" }; var rawHeaders = new Dictionary<string, IEnumerable<string>> { { "Accept", values } }; // When var headers = new RequestHeaders(rawHeaders).Accept.ToList(); // Then headers.Count.ShouldEqual(2); headers.FirstOrDefault(t => t.Item1 == "text/plain" && t.Item2 == 1.0m).ShouldNotBeNull(); headers.FirstOrDefault(t => t.Item1 == "text/ninja" && t.Item2 == 1.0m).ShouldNotBeNull(); }
public void Should_ignore_accept_charset_header_values_with_invalid_quality() { // Given var values = new[] { "utf-8, iso-8859-5;q=a" }; var rawHeaders = new Dictionary<string, IEnumerable<string>> { { "Accept-Charset", values } }; // When var headers = new RequestHeaders(rawHeaders).AcceptCharset.ToList(); // Then headers.Count.ShouldEqual(2); headers.FirstOrDefault(t => t.Item1 == "utf-8" && t.Item2 == 1.0m).ShouldNotBeNull(); headers.FirstOrDefault(t => t.Item1 == "iso-8859-5" && t.Item2 == 1.0m).ShouldNotBeNull(); }
private string GetValueFromHeader(RequestHeaders headers, string key) { var headerKVP = headers.FirstOrDefault(_ => CommonString.AreEquals(_.Key, key)); return(headerKVP.Value.FirstOrDefault()); }