public async Task SendRequest_WithVersionTracking_AddsApplicationVersionToResponse()
        {
            // Arrange
            string expected = $"version-{Guid.NewGuid()}";

            _testServer.AddServicesConfig(services => services.AddSingleton <IAppVersion>(provider => new StubAppVersion(expected)));
            _testServer.AddConfigure(app => app.UseVersionTracking());

            using (HttpClient client = _testServer.CreateClient())
                // Act
                using (HttpResponseMessage response = await client.GetAsync(EchoController.Route))
                {
                    // Assert
                    Assert.True(response.Headers.TryGetValues(DefaultHeaderName, out IEnumerable <string> values));
                    Assert.Equal(expected, Assert.Single(values));
                }
        }
        public async Task GetRequest_TracksRequest_ReturnsSuccess()
        {
            // Arrange
            const string headerName = "x-custom-header", headerValue = "custom header value", body = "echo me";

            _testServer.AddConfigure(app => app.UseRequestTracking());
            using (HttpClient client = _testServer.CreateClient())
            {
                var request = new HttpRequestMessage(HttpMethod.Get, EchoController.Route)
                {
                    Headers = { { headerName, headerValue } },
                    Content = new StringContent($"\"{body}\"", Encoding.UTF8, "application/json")
                };

                // Act
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    // Assert
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                    string content = await response.Content.ReadAsStringAsync();

                    Assert.Equal("echo me", content);

                    IReadOnlyDictionary <ScalarValue, LogEventPropertyValue> eventContext = GetLoggedEventContext();
                    Assert.True(ContainsRequestHeader(eventContext, headerName, headerValue), "Logged event context should contain request header");
                    Assert.False(ContainsRequestBody(eventContext, body), "Shouldn't contain request body");
                }
            }
        }
Esempio n. 3
0
        public async Task GetRequestWithInvalidEndpointFeature_TracksRequest_ReturnsSuccess()
        {
            // Arrange
            string headerName = $"x-custom-header-{Guid.NewGuid()}", headerValue = $"header-{Guid.NewGuid()}";

            _testServer.AddConfigure(app =>
            {
                app.Use((ctx, next) =>
                {
                    ctx.Features.Set(Mock.Of <IEndpointFeature>());
                    return(next());
                });
                app.UseRequestTracking();
            });

            // Act
            using (HttpClient client = _testServer.CreateClient())
                using (var request = new HttpRequestMessage(HttpMethod.Get, HealthController.Route))
                {
                    request.Headers.Add(headerName, headerValue);

                    using (HttpResponseMessage response = await client.SendAsync(request))
                    {
                        // Assert
                        IDictionary <string, string> eventContext = GetLoggedEventContext();
                        Assert.Equal(headerValue, Assert.Contains(headerName, eventContext));
                    }
                }
        }