<commit_before>namespace Nancy.Tests.Unit
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using Nancy.IO;
    using Xunit;

    public class RequestFixture
    {
        [Fact]
        public void Should_throw_argumentnullexception_when_initialized_with_null_method()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request(null, "/", "http"));

            // Then
            exception.ShouldBeOfType<ArgumentNullException>();
        }

        [Fact]
        public void Should_throw_argumentoutofrangeexception_when_initialized_with_empty_method()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request(string.Empty, "/", "http"));

            // Then
            exception.ShouldBeOfType<ArgumentOutOfRangeException>();
        }

        [Fact]
        public void Should_throw_argumentnullexception_when_initialized_with_null_uri()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request("GET", null, "http"));

            // Then
            exception.ShouldBeOfType<ArgumentNullException>();
        }

        [Fact]
        public void Should_throw_argumentoutofrangeexception_when_initialized_with_empty_uri()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request("GET", string.Empty, "http"));

            // Then
            exception.ShouldBeOfType<ArgumentOutOfRangeException>();
        }

        [Fact]
        public void Should_throw_argumentnullexception_when_initialized_with_null_headers()
        {
            // Given
            var stream = CreateRequestStream();

            // When
            var exception =
                Record.Exception(() => new Request("GET", "/", null, stream, "http"));

            // Then
            exception.ShouldBeOfType<ArgumentNullException>();
        }

        [Fact]
        public void Should_throw_argumentnullexception_when_initialized_with_null_body()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request("GET", "/", new Dictionary<string, IEnumerable<string>>(), null, "http"));

            // Then
            exception.ShouldBeOfType<ArgumentNullException>();
        }

        [Fact]
        public void Should_set_method_parameter_value_to_method_property_when_initialized()
        {
            // Given
            const string method = "GET";

            // When
            var request = new Request(method, "/", "http");

            // Then
            request.Method.ShouldEqual(method);
        }

        [Fact]
        public void Should_set_uri_parameter_value_to_uri_property_when_initialized()
        {
            // Given
            const string uri = "/";
            
            // When
            var request = new Request("GET", uri, "http");

            // Then
            request.Uri.ShouldEqual(uri);
        }

        [Fact]
        public void Should_set_header_parameter_value_to_header_property_when_initialized()
        {
            // Given
            var headers = new Dictionary<string, IEnumerable<string>>()
                {
                    { "content-type", new[] {"foo"} }
                };

            // When
            var request = new Request("GET", "/", headers, CreateRequestStream(), "http");

            // Then
            request.Headers.Keys.Contains("content-type").ShouldBeTrue();
        }

        [Fact]
        public void Should_set_body_parameter_value_to_body_property_when_initialized()
        {
            // Given
            var body = CreateRequestStream();

            // When
            var request = new Request("GET", "/", new Dictionary<string, IEnumerable<string>>(), body, "http");

            // Then
            request.Body.ShouldBeSameAs(body);
        }

        [Fact]
        public void Should_set_extract_form_data_from_body_when_content_type_is_x_www_form_urlencoded()
        {
            // Given
            const string bodyContent = "name=John+Doe&gender=male&family=5&city=kent&city=miami&other=abc%0D%0Adef&nickname=J%26D";
            var memory = CreateRequestStream();
            var writer = new StreamWriter(memory);
            writer.Write(bodyContent);
            writer.Flush();
            memory.Position = 0;

            var headers = 
                new Dictionary<string, IEnumerable<string>>
                {
                    { "content-type", new[] { "application/x-www-form-urlencoded; charset=UTF-8" } }
                };<commit_msg>Added additional test to make sure it handles with and without charset<commit_after>namespace Nancy.Tests.Unit
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using Nancy.IO;
    using Xunit;

    public class RequestFixture
    {
        [Fact]
        public void Should_throw_argumentnullexception_when_initialized_with_null_method()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request(null, "/", "http"));

            // Then
            exception.ShouldBeOfType<ArgumentNullException>();
        }

        [Fact]
        public void Should_throw_argumentoutofrangeexception_when_initialized_with_empty_method()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request(string.Empty, "/", "http"));

            // Then
            exception.ShouldBeOfType<ArgumentOutOfRangeException>();
        }

        [Fact]
        public void Should_throw_argumentnullexception_when_initialized_with_null_uri()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request("GET", null, "http"));

            // Then
            exception.ShouldBeOfType<ArgumentNullException>();
        }

        [Fact]
        public void Should_throw_argumentoutofrangeexception_when_initialized_with_empty_uri()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request("GET", string.Empty, "http"));

            // Then
            exception.ShouldBeOfType<ArgumentOutOfRangeException>();
        }

        [Fact]
        public void Should_throw_argumentnullexception_when_initialized_with_null_headers()
        {
            // Given
            var stream = CreateRequestStream();

            // When
            var exception =
                Record.Exception(() => new Request("GET", "/", null, stream, "http"));

            // Then
            exception.ShouldBeOfType<ArgumentNullException>();
        }

        [Fact]
        public void Should_throw_argumentnullexception_when_initialized_with_null_body()
        {
            // Given, When
            var exception =
                Record.Exception(() => new Request("GET", "/", new Dictionary<string, IEnumerable<string>>(), null, "http"));

            // Then
            exception.ShouldBeOfType<ArgumentNullException>();
        }

        [Fact]
        public void Should_set_method_parameter_value_to_method_property_when_initialized()
        {
            // Given
            const string method = "GET";

            // When
            var request = new Request(method, "/", "http");

            // Then
            request.Method.ShouldEqual(method);
        }

        [Fact]
        public void Should_set_uri_parameter_value_to_uri_property_when_initialized()
        {
            // Given
            const string uri = "/";
            
            // When
            var request = new Request("GET", uri, "http");

            // Then
            request.Uri.ShouldEqual(uri);
        }

        [Fact]
        public void Should_set_header_parameter_value_to_header_property_when_initialized()
        {
            // Given
            var headers = new Dictionary<string, IEnumerable<string>>()
                {
                    { "content-type", new[] {"foo"} }
                };

            // When
            var request = new Request("GET", "/", headers, CreateRequestStream(), "http");

            // Then
            request.Headers.Keys.Contains("content-type").ShouldBeTrue();
        }

        [Fact]
        public void Should_set_body_parameter_value_to_body_property_when_initialized()
        {
            // Given
            var body = CreateRequestStream();

            // When
            var request = new Request("GET", "/", new Dictionary<string, IEnumerable<string>>(), body, "http");

            // Then
            request.Body.ShouldBeSameAs(body);
        }

        [Fact]
        public void Should_set_extract_form_data_from_body_when_content_type_is_x_www_form_urlencoded()
        {
            // Given
            const string bodyContent = "name=John+Doe&gender=male&family=5&city=kent&city=miami&other=abc%0D%0Adef&nickname=J%26D";
            var memory = CreateRequestStream();
            var writer = new StreamWriter(memory);
            writer.Write(bodyContent);
            writer.Flush();
            memory.Position = 0;

            var headers = 
                new Dictionary<string, IEnumerable<string>>
                {
                    { "content-type", new[] { "application/x-www-form-urlencoded" } }
                };

            // When
            var request = new Request("POST", "/", headers, memory, "http");

            // Then
            ((string)request.Form.name).ShouldEqual("<NAME>");
        }

        [Fact]
        public void Should_set_extract_form_data_from_body_when_content_type_is_x_www_form_urlencoded_with_character_set()
        {
            // Given
            const string bodyContent = "name=John+Doe&gender=male&family=5&city=kent&city=miami&other=abc%0D%0Adef&nickname=J%26D";
            var memory = CreateRequestStream();
            var writer = new StreamWriter(memory);
            writer.Write(bodyContent);
            writer.Flush();
            memory.Position = 0;

            var headers =
                new Dictionary<string, IEnumerable<string>>
                {
                    { "content-type", new[] { "application/x-www-form-urlencoded; charset=UTF-8" } }
                };
{'fraction_non_alphanumeric': 0.10507177033492823, 'fraction_numerical': 0.0019138755980861245, 'mean_word_length': 1.730146290491118, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1995678', 'n_tokens_mistral': 2702, 'n_tokens_neox': 2623, 'n_words': 489}