Beispiel #1
0
        public void GivenCustomHeaders_WhenMultipleCallsUsingTheSameHttpContext_ThenHttpContextItemsIsUsed()
        {
            var headerReader = new AuditHeaderReader(_optionsAuditConfiguration);

            var headers = GenerateRandomHeaders(1, 5).ToList()[0][0] as Dictionary <string, string>;

            foreach (var header in headers)
            {
                _httpContext.Request.Headers.Add(header.Key, new StringValues(header.Value));
            }

            var result = headerReader.Read(_httpContext);

            Assert.Equal(5, result.Count);

            var changedHeaders = new Dictionary <string, string>()
            {
                ["changed"] = "changed"
            };

            _httpContext.Items[AuditConstants.CustomAuditHeaderKeyValue] = changedHeaders;

            result = headerReader.Read(_httpContext);
            Assert.Equal(changedHeaders, result);
        }
Beispiel #2
0
        public void GivenMultipleValuesOfSameHeader_WhenHeadersRead_ThenConcatenatedStringValueReturend()
        {
            var headerReader = new AuditHeaderReader(_optionsAuditConfiguration);

            _httpContext.Request.Headers.Add(_optionsAuditConfiguration.Value.CustomAuditHeaderPrefix + "repeated", new StringValues(new[] { "item1", "item2" }));

            var result = headerReader.Read(_httpContext);

            Assert.Equal("item1,item2", result[_optionsAuditConfiguration.Value.CustomAuditHeaderPrefix + "repeated"]);
        }
Beispiel #3
0
        public void GivenTooManyCustomHeaders_WhenHeadersRead_ThenAuditHeaderExceptionIsThrown()
        {
            var headerReader = new AuditHeaderReader(_optionsAuditConfiguration);

            _httpContext.Request.Headers.Clear();
            var headers = GenerateRandomHeaders(1, 11).ToList()[0][0] as Dictionary <string, string>;

            foreach (var header in headers)
            {
                _httpContext.Request.Headers.Add(header.Key, new StringValues(header.Value));
            }

            Assert.Throws <AuditHeaderException>(() => headerReader.Read(_httpContext));
        }
Beispiel #4
0
        public void GivenMixedHeaders_WhenHeadersRead_ThenOnlyCorrectCustomHeadersReturn(IReadOnlyDictionary <string, string> headers, int expectedCustomHeaderCount)
        {
            var headerReader = new AuditHeaderReader(_optionsAuditConfiguration);

            _httpContext.Request.Headers.Clear();

            foreach (var header in headers)
            {
                _httpContext.Request.Headers.Add(header.Key, new StringValues(header.Value));
            }

            var result = headerReader.Read(_httpContext);

            Assert.Equal(result.Count, expectedCustomHeaderCount);
        }
Beispiel #5
0
        public void GivenHeaderWithNoValue_WhenHeadersRead_ThenHeaderNameWithEmptyValueIsReturned()
        {
            var headerReader = new AuditHeaderReader(_optionsAuditConfiguration);
            var headers      = GenerateRandomHeaders(1, 5).ToList()[0][0] as Dictionary <string, string>;

            foreach (var header in headers)
            {
                _httpContext.Request.Headers.Add(header.Key, default);
            }

            var result = headerReader.Read(_httpContext);

            Assert.Equal(5, result.Count);

            foreach (var customHeader in result)
            {
                Assert.True(string.IsNullOrEmpty(customHeader.Value));
            }
        }
Beispiel #6
0
        public void GivenNoCustomHeaders_WhenHeadersRead_ThenEmptyDictionaryReturned()
        {
            var headerReader = new AuditHeaderReader(_optionsAuditConfiguration);

            // No headers at all
            var result = headerReader.Read(_httpContext);

            Assert.Empty(result);

            // Some non-audit headers
            var headers = GenerateRandomHeaders(1, 0).ToList()[0][0] as Dictionary <string, string>;

            foreach (var header in headers)
            {
                _httpContext.Request.Headers.Add(header.Key, new StringValues(header.Value));
            }

            result = headerReader.Read(_httpContext);
            Assert.Empty(result);
        }
Beispiel #7
0
        public void GivenAHeaderWithTooLargeValue_WhenHeadersRead_ThenAuditHeaderExceptionIsThrown()
        {
            var d = new Dictionary <string, string>()
            {
                ["a"] = "b"
            };
            var headerReader = new AuditHeaderReader(_optionsAuditConfiguration);

            _httpContext.Request.Headers.Clear();
            var headers = GenerateRandomHeaders(1, 9).ToList()[0][0] as Dictionary <string, string>;

            foreach (var header in headers)
            {
                _httpContext.Request.Headers.Add(header.Key, new StringValues(header.Value));
            }

            _httpContext.Request.Headers.Add(_optionsAuditConfiguration.Value.CustomAuditHeaderPrefix + "big", GenerateRandomString(2049));

            Assert.Throws <AuditHeaderException>(() => headerReader.Read(_httpContext));
        }