예제 #1
0
        public async Task VerifyHeadersForAlbumsEndpoint()
        {
            for (int i = 0; i < repetitions; i++)
            {
                Console.WriteLine($"Test will run {repetitions} time(s)");
                //Arrange
                //Act
                var apiAlbumsResponse = await _albumApi.GetAllAlbumsCollection();

                Console.WriteLine($"Request execution time {apiAlbumsResponse.ElapsedMiliseconds} ms");
                //Assert
                Assert.Multiple(() =>
                {
                    apiAlbumsResponse.StatusCode.Should().Be(HttpStatusCode.OK);
                    apiAlbumsResponse.ContentType.Should().Be("application/json; charset=utf-8");
                    apiAlbumsResponse.ContentEncoding.Should().BeNull();
                    apiAlbumsResponse.Headers.Should().NotBeNull();
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Set-Cookie").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(apiAlbumsResponse, "CF-RAY").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(apiAlbumsResponse, "cf-request-id").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(apiAlbumsResponse, "NEL").Value.Should().Be("{\"report_to\":\"cf-nel\",\"max_age\":604800}");
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Report-To").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(apiAlbumsResponse, "CF-Cache-Status").Value.Should().Be("HIT");
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Via").Value.Should().Be("1.1 vegur");
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Etag").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(apiAlbumsResponse, "X-Content-Type-Options").Value.Should().Be("nosniff");
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Expires").Value.Should().Be("-1");
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Pragma").Value.Should().Be("no-cache");
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Cache-Control").Value.Should().Be("max-age=43200");
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Access-Control-Allow-Credentials").Value.Should().Be("true");
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Vary").Value.Should().Be("Origin, Accept-Encoding");
                    HeadersHelper.HeadersList(apiAlbumsResponse, "Connection").Value.Should().Be("keep-alive");
                });
            }
        }
예제 #2
0
        private static (HttpResponseMessage, IHeaderDictionary) DecodeResponse(TextReader textReader)
        {
            var response       = new HttpResponseMessage();
            var contentHeaders = new HeaderDictionary();

            var line       = textReader.ReadLine();
            var statusCode = int.Parse(line);

            response.StatusCode = (HttpStatusCode)statusCode;

            var headers = new HeaderDictionary();

            DecodeHeaders(headers, textReader);
            foreach (var header in headers)
            {
                if (HeadersHelper.IsContentHeader(header.Key))
                {
                    contentHeaders.Add(header);
                }
                else
                {
                    response.Headers.Add(header.Key, (string[])header.Value);
                }
            }

            return(response, contentHeaders);
        }
예제 #3
0
        private static HttpRequestMessage DecodeHeaders(HttpContent httpContent, StreamReader streamReader)
        {
            var request = new HttpRequestMessage();

            var firstLine = streamReader.ReadLine();
            var split     = firstLine.Split(new[] { ' ' }, 2);

            request.Method     = new HttpMethod(split[0]);
            request.RequestUri = new Uri(split[1], UriKind.RelativeOrAbsolute);

            var headers = new HeaderDictionary();

            DecodeHeaders(headers, streamReader);

            foreach (var header in headers)
            {
                if (HeadersHelper.IsContentHeader(header.Key))
                {
                    httpContent.Headers.Add(header.Key, header.Value.ToList());
                }
                else
                {
                    request.Headers.Add(header.Key, header.Value.ToList());
                }
            }

            request.Content = httpContent;

            return(request);
        }
예제 #4
0
        public static HttpRequestMessage ToHttpRequestMessage(this HttpRequest httpRequest, string path)
        {
            var requestMessage =
                new HttpRequestMessage(new HttpMethod(httpRequest.Method),
                                       new Uri(DummyBaseUri, path + httpRequest.QueryString))
            {
                Content = new RawStreamContent(httpRequest.Body)
            };

            foreach (var header in httpRequest.Headers)
            {
                if (header.Key.Equals(HeaderNames.Authorization, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (HeadersHelper.IsContentHeader(header.Key))
                {
                    requestMessage.Content.Headers.Add(header.Key, (IEnumerable <string>)header.Value);
                }
                else
                {
                    requestMessage.Headers.Add(header.Key, (IEnumerable <string>)header.Value);
                }
            }

            return(requestMessage);
        }
        private Task RequestModifier(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var authHeaders = HeadersHelper.CreateAuthorizationHeaders(_apiKey, _apiSecret, DateTimeOffset.UtcNow);

            foreach (var header in authHeaders)
            {
                request.Headers.Add(header.Key, header.Value);
            }

            return(Task.CompletedTask);
        }
예제 #6
0
        public bool TryParseAndAdd(string header)
        {
            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }

            var separatorIndex = header.IndexOf(':', 0);

            if (separatorIndex == -1 || separatorIndex == 0)
            {
                return(false); // header is invalid
            }
            var headerStartIndex = 0;
            var headerLength     = separatorIndex - headerStartIndex;

            HeadersHelper.TrimInPlace(header, ref headerStartIndex, ref headerLength);

            var valueStartIndex = separatorIndex + 1;
            var valueLength     = header.Length - valueStartIndex;

            HeadersHelper.TrimInPlace(header, ref valueStartIndex, ref valueLength);

            if (headerLength == 0 || valueLength == 0)
            {
                return(false); // header name or value is empty
            }
            var headerName       = header.Substring(headerStartIndex, headerLength);
            var knownHeaderValue = GetKnownHeaderValue(headerName);

            if (knownHeaderValue >= 0)
            {
                var value = header.Substring(valueStartIndex, valueLength);
                if ((GetFlags(knownHeaderValue) & HeaderFlags.Singleton) == HeaderFlags.Singleton) // it is atomic header, so don't split it
                {
                    this.Add(FromInt(knownHeaderValue), value);
                }
                else
                {
                    foreach (var headerValue in TrimAndSplit(header, valueStartIndex, valueLength))
                    {
                        this.Add(headerName, headerValue);
                    }
                }
            }
            else
            {
                foreach (var headerValue in TrimAndSplit(header, valueStartIndex, valueLength))
                {
                    this.Add(headerName, headerValue);
                }
            }
            return(true);
        }
예제 #7
0
        public ActionResult SaveHeaders(IList <Header> headers)
        {
            HeadersRepository repository = new HeadersRepository();

            foreach (Header header in headers)
            {
                repository.UpdateHeader(header.HeaderKey, header.HeaderValue);
            }

            HeadersHelper.Refresh();
            TempData["message"] = "Zmiany zosta³y zapisane";
            return(RedirectToAction("Headers"));
        }
예제 #8
0
        public void Creates_valid_hash_header()
        {
            var          apiKeyFake      = Autofixture.Create <string>();
            const string apiSecretFake   = "c9b5625f-9834-4ff8-baba-4ed5f32cae55";
            var          requestDateFake = new DateTimeOffset(2011, 2, 12, 20, 59, 04, TimeSpan.FromHours(0));

            // Act
            var result = HeadersHelper.CreateAuthorizationHeaders(apiKeyFake, apiSecretFake, requestDateFake).ToList();

            // Assert
            result.Should().Contain(x => x.Key == "x-dnsme-hmac")
            .Which.Value.Should().Be("b3502e6116a324f3cf4a8ed693d78bcee8d8fe3c");
        }
예제 #9
0
        public void Creates_valid_api_key_header()
        {
            var apiKeyFake      = Autofixture.Create <string>();
            var apiSecretFake   = Autofixture.Create <string>();
            var requestDateFake = Autofixture.Create <DateTimeOffset>();

            // Act
            var result = HeadersHelper.CreateAuthorizationHeaders(apiKeyFake, apiSecretFake, requestDateFake).ToList();

            // Assert
            result.Should().Contain(x => x.Key == "x-dnsme-apiKey")
            .Which.Value.Should().Be(apiKeyFake);
        }
예제 #10
0
        public void Creates_valid_date_header()
        {
            var apiKeyFake      = Autofixture.Create <string>();
            var apiSecretFake   = Autofixture.Create <string>();
            var requestDateFake = new DateTimeOffset(2011, 2, 12, 20, 59, 04, TimeSpan.FromHours(0));

            // Act
            var result = HeadersHelper.CreateAuthorizationHeaders(apiKeyFake, apiSecretFake, requestDateFake).ToList();

            // Assert
            result.Should().Contain(x => x.Key == "x-dnsme-requestDate")
            .Which.Value.Should().Be("Sat, 12 Feb 2011 20:59:04 GMT");
        }
예제 #11
0
 public static void CopyHeadersTo(this IHeaderDictionary headerDictionary, HttpHeaders httpHeaders,
                                  HttpContentHeaders contentHeaders)
 {
     foreach (var requestHeader in headerDictionary)
     {
         if (HeadersHelper.IsContentHeader(requestHeader.Key))
         {
             contentHeaders.Add(requestHeader.Key, (IEnumerable <string>)requestHeader.Value);
         }
         else
         {
             httpHeaders.Add(requestHeader.Key, (IEnumerable <string>)requestHeader.Value);
         }
     }
 }
        private void ParseWebSocketExtensions(WebSocketHandshake handshake)
        {
            if (handshake == null)
            {
                throw new ArgumentNullException(nameof(handshake));
            }

            var requestHeaders = handshake.Response.Headers;

            if (!requestHeaders.Contains(ResponseHeader.WebSocketExtensions))
            {
                return;
            }

            foreach (var extension in requestHeaders.GetValues(ResponseHeader.WebSocketExtensions))
            {
                var extensionOptions = new List <WebSocketExtensionOption>();
                var extensionName    = default(string);
                foreach (var option in HeadersHelper.SplitAndTrimKeyValue(extension, options: StringSplitOptions.RemoveEmptyEntries))
                {
                    if (extensionName == default(string))
                    {
                        extensionName = option.Value;
                        continue;
                    }

                    if (string.IsNullOrEmpty(option.Key))
                    {
                        extensionOptions.Add(new WebSocketExtensionOption(option.Value, clientAvailableOption: true));
                    }
                    else
                    {
                        extensionOptions.Add(new WebSocketExtensionOption(option.Key, option.Value));
                    }
                }

                if (string.IsNullOrEmpty(extensionName))
                {
                    throw new WebSocketException(
                              $"Wrong value '{requestHeaders[ResponseHeader.WebSocketExtensions]}' of {Headers<ResponseHeader>.GetHeaderName(ResponseHeader.WebSocketExtensions)} header in request.");
                }

                handshake.Response.WebSocketExtensions.Add(new WebSocketExtension(extensionName, extensionOptions));
            }
        }
예제 #13
0
        public async Task <IEnumerable <Review> > GetReviewsAsync(int productId)
        {
            try
            {
                var userAgent = httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString();
                client.DefaultRequestHeaders.Add("User-Agent", userAgent);
                HeadersHelper.AddTracingHeaders(client, httpContextAccessor);

                var reviewsResponse = await client.GetAsync($"api/reviews/{productId}");

                var reviews = await reviewsResponse.Content.ReadAsAsync <List <Review> >();

                return(reviews);
            }
            catch (System.Exception)
            {
                return(null);
            }
        }
예제 #14
0
        private static ValueCollection TrimAndSplit(string valueString, int startIndex, int count)
        {
            if (valueString == null)
            {
                throw new ArgumentNullException(nameof(valueString));
            }
            if (startIndex < 0 || startIndex >= valueString.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(startIndex));
            }
            if (count < 0 || startIndex + count > valueString.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            const char VALUE_SEPARATOR = ',';

            var values = new ValueCollection();

            var valueStartIndex     = startIndex;
            var valueLength         = count;
            var valueSeparatorIndex = valueString.IndexOf(VALUE_SEPARATOR, valueStartIndex, count);

            while (valueSeparatorIndex >= 0)
            {
                valueLength = valueSeparatorIndex - valueStartIndex;
                HeadersHelper.TrimInPlace(valueString, ref valueStartIndex, ref valueLength);
                if (valueLength > 0)
                {
                    values += new ValueCollection(valueString.Substring(valueStartIndex, valueLength));
                }
                valueStartIndex     = valueSeparatorIndex + 1;
                valueLength         = startIndex + count - valueStartIndex;
                valueSeparatorIndex = valueString.IndexOf(VALUE_SEPARATOR, valueStartIndex, valueLength);
            }
            HeadersHelper.TrimInPlace(valueString, ref valueStartIndex, ref valueLength);
            if (valueLength > 0)
            {
                values += new ValueCollection(valueString.Substring(valueStartIndex, valueLength));
            }

            return(values);
        }
        public async Task <IEnumerable <Product> > GetProductsAsync()
        {
            try
            {
                var userAgent = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString();
                _client.DefaultRequestHeaders.Add("User-Agent", userAgent);
                HeadersHelper.AddTracingHeaders(_client, _httpContextAccessor);

                var productsResponse = await _client.GetAsync("api/products");

                var products = await productsResponse.Content.ReadAsAsync <List <Product> >();

                return(products);
            }
            catch (System.Exception)
            {
                return(null);
            }
        }
        public async Task <Product> GetProductAsync(int id)
        {
            try
            {
                _logger.LogInformation("Loading product with {ProductId}", id);
                var userAgent = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString();
                _client.DefaultRequestHeaders.Add("User-Agent", userAgent);
                HeadersHelper.AddTracingHeaders(_client, _httpContextAccessor);

                var productsResponse = await _client.GetAsync($"api/products/{id}");

                var product = await productsResponse.Content.ReadAsAsync <Product>();

                return(product);
            }
            catch (System.Exception)
            {
                return(null);
            }
        }
예제 #17
0
        public async Task VerifyCreatePostResponseHeaders()
        {
            for (int i = 0; i < repetitions; i++)
            {
                Console.WriteLine($"Test will run {repetitions} time(s)");
                //Arrange

                //Act
                var createPostResponse = await _postsApi.CreatePost(post);

                Console.WriteLine($"Request execution time {createPostResponse.ElapsedMiliseconds} ms");
                var responseBody = createPostResponse.Data;

                //Assert
                Assert.Multiple(() =>
                {
                    createPostResponse.ContentType.Should().Be("application/json; charset=utf-8");
                    createPostResponse.ContentEncoding.Should().BeNull();
                    createPostResponse.Headers.Should().NotBeNull();
                    HeadersHelper.HeadersList(createPostResponse, "CF-RAY").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(createPostResponse, "Set-Cookie").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(createPostResponse, "cf-request-id").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(createPostResponse, "NEL").Value.Should().Be("{\"report_to\":\"cf-nel\",\"max_age\":604800}");
                    HeadersHelper.HeadersList(createPostResponse, "Report-To").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(createPostResponse, "CF-Cache-Status").Value.Should().Be("DYNAMIC");
                    HeadersHelper.HeadersList(createPostResponse, "Via").Value.Should().Be("1.1 vegur");
                    HeadersHelper.HeadersList(createPostResponse, "Etag").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(createPostResponse, "X-Content-Type-Options").Value.Should().Be("nosniff");
                    HeadersHelper.HeadersList(createPostResponse, "Location").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(createPostResponse, "Access-Control-Expose-Headers").Value.Should().NotBeNull();
                    HeadersHelper.HeadersList(createPostResponse, "Expires").Value.Should().Be("-1");
                    HeadersHelper.HeadersList(createPostResponse, "Pragma").Value.Should().Be("no-cache");
                    HeadersHelper.HeadersList(createPostResponse, "Cache-Control").Value.Should().Be("no-cache");
                    HeadersHelper.HeadersList(createPostResponse, "Access-Control-Allow-Credentials").Value.Should().Be("true");
                    HeadersHelper.HeadersList(createPostResponse, "Vary").Value.Should().Be("Origin, X-HTTP-Method-Override, Accept-Encoding");
                    HeadersHelper.HeadersList(createPostResponse, "Connection").Value.Should().Be("keep-alive");
                });
            }
        }
예제 #18
0
        public static async Task <HttpResponseMessage> DeserializeResponse(this Stream stream)
        {
            var response = new HttpResponseMessage();
            var headers  = new HeaderDictionary();

            using (var streamReader = new StreamReader(stream, Encoding.UTF8, false, 8192, true))
            {
                var statusLine = await streamReader.ReadLineAsync();

                response.SetStatusProperties(statusLine);

                string line;
                while (!string.IsNullOrEmpty(line = await streamReader.ReadLineAsync()))
                {
                    var split = line.Split(new[] { ": " }, 2, StringSplitOptions.None);
                    headers.Add(split[0], split[1]);
                }
            }

            var contentLength = headers.ContentLength;

            if (contentLength > 0)
            {
                var buffer = await stream.ReadBytes((int)contentLength.Value);

                response.Content = new ByteArrayPoolContent(buffer.Array, 0, buffer.Count);
            }

            foreach (var header in headers)
            {
                if (!HeadersHelper.IsContentHeader(header.Key)) //ignore content headers
                {
                    response.Headers.Add(header.Key, (IEnumerable <string>)header.Value);
                }
            }

            return(response);
        }
예제 #19
0
        public async Task SkipPolicy_ChunkedJsonDeserializationError_SequenceSkipped()
        {
            var message = new TestEventOne {
                Content = "Hello E2E!"
            };

            byte[] rawMessage = (await Endpoint.DefaultSerializer.SerializeAsync(
                                     message,
                                     new MessageHeaderCollection(),
                                     MessageSerializationContext.Empty)).ReadAll() ??
                                throw new InvalidOperationException("Serializer returned null");

            byte[] invalidRawMessage = Encoding.UTF8.GetBytes("<what?!><what?!><what?!>");

            Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .WithConnectionToMessageBroker(
                    options => options.AddMockedKafka(
                        mockedKafkaOptions => mockedKafkaOptions.WithDefaultPartitionsCount(1)))
                .AddEndpoints(
                    endpoints => endpoints
                    .AddInbound(
                        new KafkaConsumerEndpoint(DefaultTopicName)
            {
                Configuration =
                {
                    GroupId          = "consumer1",
                    EnableAutoCommit = false,
                    CommitOffsetEach = 1
                },
                ErrorPolicy = ErrorPolicy.Skip()
            }))
                .AddSingletonSubscriber <OutboundInboundSubscriber>())
            .Run();

            var producer = Broker.GetProducer(new KafkaProducerEndpoint(DefaultTopicName));
            await producer.RawProduceAsync(
                invalidRawMessage.Take(10).ToArray(),
                HeadersHelper.GetChunkHeaders("1", 0, typeof(TestEventOne)));

            await producer.RawProduceAsync(
                invalidRawMessage.Skip(10).Take(10).ToArray(),
                HeadersHelper.GetChunkHeaders("1", 1, typeof(TestEventOne)));

            await producer.RawProduceAsync(
                invalidRawMessage.Skip(20).ToArray(),
                HeadersHelper.GetChunkHeaders("1", 2, true, typeof(TestEventOne)));

            await KafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            Subscriber.InboundEnvelopes.Should().BeEmpty();
            DefaultTopic.GetCommittedOffsetsCount("consumer1").Should().Be(3);

            await producer.RawProduceAsync(
                rawMessage.Take(10).ToArray(),
                HeadersHelper.GetChunkHeaders("2", 0, typeof(TestEventOne)));

            await producer.RawProduceAsync(
                rawMessage.Skip(10).Take(10).ToArray(),
                HeadersHelper.GetChunkHeaders("2", 1, typeof(TestEventOne)));

            await producer.RawProduceAsync(
                rawMessage.Skip(20).ToArray(),
                HeadersHelper.GetChunkHeaders("2", 2, true, typeof(TestEventOne)));

            await KafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            Subscriber.InboundEnvelopes.Should().HaveCount(1);
            DefaultTopic.GetCommittedOffsetsCount("consumer1").Should().Be(6);
        }
예제 #20
0
 public static DataPart Create()
 {
     return(Create(IStreamContainerHelper.Create(), HeadersHelper.Create(), PropertiesHelper.Create()));
 }
예제 #21
0
 public static DataPack Create()
 {
     return(Create(NextString(), NextDateTime(), NextGuid(), NextString(), HeadersHelper.Create(), PropertiesHelper.Create(), DataPartHelper.CreateEnum()));
 }