aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/IntegratedTests/I18nTest.cs
blob: 67bbea5cc6b3a53751864991e9b132c5749c38d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Microsoft.AspNetCore.Mvc.Testing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Timeline.Tests.Helpers;
using Xunit;
using FluentAssertions;

namespace Timeline.Tests.IntegratedTests
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1054:Uri parameters should not be strings")]
    public class I18nTest : IClassFixture<WebApplicationFactory<Startup>>, IDisposable
    {
        private readonly TestApplication _testApp;
        private readonly HttpClient _client;

        public I18nTest(WebApplicationFactory<Startup> factory)
        {
            _testApp = new TestApplication(factory);
            _client = _testApp.Factory.CreateDefaultClient();
        }

        public void Dispose()
        {
            _client.Dispose();
            _testApp.Dispose();
        }

        private const string DirectUrl = "testing/i18n/direct";
        private const string LocalizerUrl = "testing/i18n/localizer";

        [Theory]
        [InlineData(DirectUrl)]
        [InlineData(LocalizerUrl)]
        public async Task DefaultShouldReturnEnglish(string url)
        {
            (await _client.GetStringAsync(url)).Should().ContainEquivalentOf("English");
        }

        [Theory]
        [InlineData(DirectUrl, "en", true)]
        [InlineData(LocalizerUrl, "en", true)]
        [InlineData(DirectUrl, "en-US", true)]
        [InlineData(LocalizerUrl, "en-US", true)]
        [InlineData(DirectUrl, "zh", false)]
        [InlineData(LocalizerUrl, "zh", false)]
        public async Task ShouldWork(string url, string acceptLanguage, bool english)
        {
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri(url, UriKind.RelativeOrAbsolute)
            };
            request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue(acceptLanguage));
            var body = await (await _client.SendAsync(request)).Content.ReadAsStringAsync();
            body.Should().ContainEquivalentOf(english ? "English" : "中文");
            request.Dispose();
        }
    }
}