<commit_before>﻿using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using Xunit;

namespace Hellang.Middleware.ProblemDetails.Tests
{
    public class ProblemDetailsMiddlewareTests
    {
        [Theory]
        [InlineData(HttpStatusCode.BadRequest)]
        [InlineData(HttpStatusCode.Unauthorized)]
        [InlineData(HttpStatusCode.NotImplemented)]
        [InlineData(HttpStatusCode.ServiceUnavailable)]
        [InlineData(HttpStatusCode.InternalServerError)]
        public async Task ErrorStatusCode_IsHandled(HttpStatusCode expected)
        {
            using (var server = CreateServer())
            using (var client = server.CreateClient())
            {
                var response = await client.GetAsync($"/error/{(int)expected}");

                Assert.Equal(expected, response.StatusCode);
                Assert.Equal("application/problem+json", response.Content.Headers.ContentType.MediaType);
                Assert.NotEmpty(await response.Content.ReadAsStringAsync());
            }
        }

        [Theory]
        [InlineData("/exception", HttpStatusCode.NotImplemented)]
        [InlineData("/exception-details", (HttpStatusCode) StatusCodes.Status429TooManyRequests)]
        public async Task Exception_IsHandled(string path, HttpStatusCode expected)
        {
            using (var server = CreateServer())
            using (var client = server.CreateClient())
            {
                var response = await client.GetAsync(path);

                Assert.Equal(expected, response.StatusCode);
                Assert.Equal("application/problem+json", response.Content.Headers.ContentType.MediaType);
                Assert.NotEmpty(await response.Content.ReadAsStringAsync());
            }
        }

        [Theory]
        [InlineData("Staging", 78)]
        [InlineData("Production", 78)]
        [InlineData("Development", 2486)]
        public async Task ExceptionDetails_AreOnlyIncludedInDevelopment(string environment, int expectedLength)
        {
            using (var server = CreateServer(environment))
            using (var client = server.CreateClient())
            {
                var response = await client.GetAsync("/exception");

                var content = await response.Content.ReadAsStringAsync();

                Assert.Equal(expectedLength, content.Length);
            }
        }

        [Fact]
        public async Task StatusCode_IsMaintainedWhenStrippingExceptionDetails()
        {
            using (var server = CreateServer("Production"))
            using (var client = server.CreateClient())
            {<commit_msg>Fix failing test<commit_after>﻿using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using Xunit;

namespace Hellang.Middleware.ProblemDetails.Tests
{
    public class ProblemDetailsMiddlewareTests
    {
        [Theory]
        [InlineData(HttpStatusCode.BadRequest)]
        [InlineData(HttpStatusCode.Unauthorized)]
        [InlineData(HttpStatusCode.NotImplemented)]
        [InlineData(HttpStatusCode.ServiceUnavailable)]
        [InlineData(HttpStatusCode.InternalServerError)]
        public async Task ErrorStatusCode_IsHandled(HttpStatusCode expected)
        {
            using (var server = CreateServer())
            using (var client = server.CreateClient())
            {
                var response = await client.GetAsync($"/error/{(int)expected}");

                Assert.Equal(expected, response.StatusCode);
                Assert.Equal("application/problem+json", response.Content.Headers.ContentType.MediaType);
                Assert.NotEmpty(await response.Content.ReadAsStringAsync());
            }
        }

        [Theory]
        [InlineData("/exception", HttpStatusCode.NotImplemented)]
        [InlineData("/exception-details", (HttpStatusCode) StatusCodes.Status429TooManyRequests)]
        public async Task Exception_IsHandled(string path, HttpStatusCode expected)
        {
            using (var server = CreateServer())
            using (var client = server.CreateClient())
            {
                var response = await client.GetAsync(path);

                Assert.Equal(expected, response.StatusCode);
                Assert.Equal("application/problem+json", response.Content.Headers.ContentType.MediaType);
                Assert.NotEmpty(await response.Content.ReadAsStringAsync());
            }
        }

        [Theory]
        [InlineData("Staging", 78)]
        [InlineData("Production", 78)]
        [InlineData("Development", 2510)]
        public async Task ExceptionDetails_AreOnlyIncludedInDevelopment(string environment, int expectedLength)
        {
            using (var server = CreateServer(environment))
            using (var client = server.CreateClient())
            {
                var response = await client.GetAsync("/exception");

                var content = await response.Content.ReadAsStringAsync();

                Assert.Equal(expectedLength, content.Length);
            }
        }

        [Fact]
        public async Task StatusCode_IsMaintainedWhenStrippingExceptionDetails()
        {
            using (var server = CreateServer("Production"))
            using (var client = server.CreateClient())
            {
{'fraction_non_alphanumeric': 0.0963104105317859, 'fraction_numerical': 0.0038108435821929674, 'mean_word_length': 2.5975077881619937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3433311', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1416, 'n_words': 270}