<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Octokit.Internal;
using Xunit;

namespace Octokit.Tests.Http
{
    public class HttpClientAdapterTests
    {
        public class TheBuildRequestMessageMethod
        {
            [Fact]
            public void AddsHeadersToRequestMessage()
            {
                var request = new Request
                {
                    Method = HttpMethod.Post,
                    Headers =
                    {
                        { "foo", "bar" },
                        { "blah", "blase" }
                    }
                };
                var tester = new HttpClientAdapterTester();
                
                var requestMessage = tester.BuildRequestMessageTester(request);

                Assert.Equal(2, requestMessage.Headers.Count());
                var firstHeader = requestMessage.Headers.First();
                Assert.Equal("foo", firstHeader.Key);
                Assert.Equal("bar", firstHeader.Value.First());
                var lastHeader = requestMessage.Headers.Last();
                Assert.Equal("blah", lastHeader.Key);
                Assert.Equal("blase", lastHeader.Value.First());
                Assert.Null(requestMessage.Content);
            }

            [Fact]
            public void SetsBodyAndContentType()
            {
                var request = new Request
                {
                    Method = HttpMethod.Post,
                    Body = "{}",
                    ContentType = "text/plain"
                };
                var tester = new HttpClientAdapterTester();

                var requestMessage = tester.BuildRequestMessageTester(request);

                Assert.NotNull(requestMessage.Content);
                Assert.Equal("text/plain", requestMessage.Content.Headers.ContentType.MediaType);
            }

            [Fact]
            public void SetsStreamBodyAndContentType()
            {
                var request = new Request
                {
                    Method = HttpMethod.Post,
                    Body = new MemoryStream(),
                    ContentType = "text/plain"
                };
                var tester = new HttpClientAdapterTester();

                var requestMessage = tester.BuildRequestMessageTester(request);

                Assert.NotNull(requestMessage.Content);
                Assert.IsType<StreamContent>(requestMessage.Content);
                Assert.Equal("text/plain", requestMessage.Content.Headers.ContentType.MediaType);
            }

            [Fact]
            public void SetsHttpContentBody()
            {
                var request = new Request
                {
                    Method = HttpMethod.Post,
                    Body = new FormUrlEncodedContent(new Dictionary<string, string> {{"foo", "bar"}})
                };
                var tester = new HttpClientAdapterTester();

                var requestMessage = tester.BuildRequestMessageTester(request);

                Assert.NotNull(requestMessage.Content);
                Assert.IsType<FormUrlEncodedContent>(requestMessage.Content);
                Assert.Equal("application/x-www-form-urlencoded", requestMessage.Content.Headers.ContentType.MediaType);
            }

            [Fact]
            public void EnsuresArguments()
            {
                var tester = new HttpClientAdapterTester();
                Assert.Throws<ArgumentNullException>(() => tester.BuildRequestMessageTester(null));
            }
        }

        public class TheBuildResponseMethod
        {
            [Theory]
            [InlineData(HttpStatusCode.OK)]
            [InlineData(HttpStatusCode.NotFound)]
            public async Task BuildsResponseFromResponseMessage(HttpStatusCode httpStatusCode)
            {
                var responseMessage = new HttpResponseMessage {<commit_msg>update the tests based on the more rigorous requirements<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Octokit.Internal;
using Xunit;

namespace Octokit.Tests.Http
{
    public class HttpClientAdapterTests
    {
        public class TheBuildRequestMessageMethod
        {
            readonly Uri _endpoint = new Uri("/ha-ha-business", UriKind.Relative);

            [Fact]
            public void AddsHeadersToRequestMessage()
            {
                var request = new Request
                {
                    BaseAddress = GitHubClient.GitHubApiUrl,
                    Endpoint = _endpoint,
                    Method = HttpMethod.Post,
                    Headers =
                    {
                        { "foo", "bar" },
                        { "blah", "blase" }
                    }
                };
                var tester = new HttpClientAdapterTester();
                
                var requestMessage = tester.BuildRequestMessageTester(request);

                Assert.Equal(2, requestMessage.Headers.Count());
                var firstHeader = requestMessage.Headers.First();
                Assert.Equal("foo", firstHeader.Key);
                Assert.Equal("bar", firstHeader.Value.First());
                var lastHeader = requestMessage.Headers.Last();
                Assert.Equal("blah", lastHeader.Key);
                Assert.Equal("blase", lastHeader.Value.First());
                Assert.Null(requestMessage.Content);
            }

            [Fact]
            public void SetsBodyAndContentType()
            {
                var request = new Request
                {
                    BaseAddress = GitHubClient.GitHubApiUrl,
                    Endpoint = _endpoint,
                    Method = HttpMethod.Post,
                    Body = "{}",
                    ContentType = "text/plain"
                };
                var tester = new HttpClientAdapterTester();

                var requestMessage = tester.BuildRequestMessageTester(request);

                Assert.NotNull(requestMessage.Content);
                Assert.Equal("text/plain", requestMessage.Content.Headers.ContentType.MediaType);
            }

            [Fact]
            public void SetsStreamBodyAndContentType()
            {
                var request = new Request
                {
                    BaseAddress = GitHubClient.GitHubApiUrl,
                    Endpoint = _endpoint,
                    Method = HttpMethod.Post,
                    Body = new MemoryStream(),
                    ContentType = "text/plain"
                };
                var tester = new HttpClientAdapterTester();

                var requestMessage = tester.BuildRequestMessageTester(request);

                Assert.NotNull(requestMessage.Content);
                Assert.IsType<StreamContent>(requestMessage.Content);
                Assert.Equal("text/plain", requestMessage.Content.Headers.ContentType.MediaType);
            }

            [Fact]
            public void SetsHttpContentBody()
            {
                var request = new Request
                {
                    BaseAddress = GitHubClient.GitHubApiUrl,
                    Endpoint = _endpoint,
                    Method = HttpMethod.Post,
                    Body = new FormUrlEncodedContent(new Dictionary<string, string> {{"foo", "bar"}})
                };
                var tester = new HttpClientAdapterTester();

                var requestMessage = tester.BuildRequestMessageTester(request);

                Assert.NotNull(requestMessage.Content);
                Assert.IsType<FormUrlEncodedContent>(requestMessage.Content);
                Assert.Equal("application/x-www-form-urlencoded", requestMessage.Content.Headers.ContentType.MediaType);
            }

            [Fact]
            public void EnsuresArguments()
            {
                var tester = new HttpClientAdapterTester();
                Assert.Throws<ArgumentNullException>(() => tester.BuildRequestMessageTester(null));
            }
        }

        public class TheBuildResponseMethod
        {
            [Theory]
            [InlineData(HttpStatusCode.OK)]
            [InlineData(HttpStatusCode.NotFound)]
            public async Task BuildsResponseFromResponseMessage(HttpStatusCode httpStatusCode)
            {
                var responseMessage = new HttpResponseMessage {
{'fraction_non_alphanumeric': 0.08403848382983656, 'fraction_numerical': 0.0002318303002202388, 'mean_word_length': 1.5235448961684703, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4813522', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1879, 'n_words': 372}