<commit_before>﻿using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using AutoMoq.Helpers;
using NUnit.Framework;
using Should;
using SparkPost.RequestSenders;

namespace SparkPost.Tests.RequestSenders
{
    public class AsyncRequestSenderTests
    {
        public class SendTests : AutoMoqTestFixture<SendTests.AsyncTesting>
        {
            private HttpClient httpClient;
            private Request request;

            [SetUp]
            public void Setup()
            {
                ResetSubject();

                httpClient = new HttpClient();

                var settings = new Client.Settings();
                settings.BuildHttpClientsUsing(() => httpClient);
                Mocked<IClient>().Setup(x => x.CustomSettings).Returns(settings);
                Mocked<IClient>().Setup(x => x.ApiHost).Returns("http://test.com");
                Mocked<IClient>().Setup(x => x.ApiKey).Returns(Guid.NewGuid().ToString());

                request = new Request();
            }

            [Test]
            public async void It_should_return_the_http_response_message_info()
            {
                var content = Guid.NewGuid().ToString();
                Subject.SetupTheResponseWith((r, h) => new HttpResponseMessage(HttpStatusCode.Accepted)
                {
                    Content = new StringContent(content)
                });

                var result = await Subject.Send(request);
                result.StatusCode.ShouldEqual(HttpStatusCode.Accepted);
                result.Content.ShouldEqual(content);
            }

            [Test]
            public async void It_should_return_the_http_response_message_info_take_2()
            {
                var content = Guid.NewGuid().ToString();
                Subject.SetupTheResponseWith((r, h) => new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(content)
                });

                var result = await Subject.Send(request);
                result.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
                result.ReasonPhrase.ShouldEqual("Not Found");
                result.Content.ShouldEqual(content);
            }

            public class AsyncTesting : AsyncRequestSender
            {
                private Func<Request, HttpClient, HttpResponseMessage> responseBuilder;

                public AsyncTesting(IClient client) : base(client)
                {
                }

                public void SetupTheResponseWith(Func<Request, HttpClient, HttpResponseMessage> responseBuilder)
                {
                    this.responseBuilder = responseBuilder;
                }

                protected override Task<HttpResponseMessage> GetTheResponse(Request request, HttpClient httpClient)
                {
                    return Task.FromResult(responseBuilder(request, httpClient));
                }
            }
        }
    }
}<commit_msg>Test the use of the api key.<commit_after>﻿using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using AutoMoq.Helpers;
using NUnit.Framework;
using Should;
using SparkPost.RequestSenders;

namespace SparkPost.Tests.RequestSenders
{
    public class AsyncRequestSenderTests
    {
        public class SendTests : AutoMoqTestFixture<SendTests.AsyncTesting>
        {
            private HttpClient httpClient;
            private Request request;
            private string apiHost;
            private string apiKey;

            [SetUp]
            public void Setup()
            {
                ResetSubject();

                httpClient = new HttpClient();

                apiHost = "http://test.com";
                apiKey = Guid.NewGuid().ToString();

                var settings = new Client.Settings();
                settings.BuildHttpClientsUsing(() => httpClient);
                Mocked<IClient>().Setup(x => x.CustomSettings).Returns(settings);
                Mocked<IClient>().Setup(x => x.ApiHost).Returns(apiHost);
                Mocked<IClient>().Setup(x => x.ApiKey).Returns(apiKey);

                request = new Request();
            }

            [Test]
            public async void It_should_return_the_http_response_message_info()
            {
                var content = Guid.NewGuid().ToString();
                Subject.SetupTheResponseWith((r, h) => new HttpResponseMessage(HttpStatusCode.Accepted)
                {
                    Content = new StringContent(content)
                });

                var result = await Subject.Send(request);
                result.StatusCode.ShouldEqual(HttpStatusCode.Accepted);
                result.Content.ShouldEqual(content);
            }

            [Test]
            public async void It_should_return_the_http_response_message_info_take_2()
            {
                var content = Guid.NewGuid().ToString();
                Subject.SetupTheResponseWith((r, h) => new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(content)
                });

                var result = await Subject.Send(request);
                result.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
                result.ReasonPhrase.ShouldEqual("Not Found");
                result.Content.ShouldEqual(content);
            }

            [Test]
            public async void It_should_pass_the_api_key()
            {
                Subject.SetupTheResponseWith((r, h) =>
                {
                    h.DefaultRequestHeaders.Authorization.ToString().ShouldEqual(apiKey);
                    return new HttpResponseMessage(HttpStatusCode.NotFound)
                    {
                        Content = new StringContent(Guid.NewGuid().ToString())
                    };
                });

                await Subject.Send(request);
            }

            public class AsyncTesting : AsyncRequestSender
            {
                private Func<Request, HttpClient, HttpResponseMessage> responseBuilder;

                public AsyncTesting(IClient client) : base(client)
                {
                }

                public void SetupTheResponseWith(Func<Request, HttpClient, HttpResponseMessage> responseBuilder)
                {
                    this.responseBuilder = responseBuilder;
                }

                protected override Task<HttpResponseMessage> GetTheResponse(Request request, HttpClient httpClient)
                {
                    return Task.FromResult(responseBuilder(request, httpClient));
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08550074738415546, 'fraction_numerical': 0.00029895366218236175, 'mean_word_length': 1.6796155386463756, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6646026', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1517, 'n_words': 292}