-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathServerTestBase.cs
More file actions
142 lines (105 loc) · 4.78 KB
/
ServerTestBase.cs
File metadata and controls
142 lines (105 loc) · 4.78 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Collections;
using System.Net;
using SixLabors.ImageSharp.Formats;
using Xunit.Abstractions;
namespace SixLabors.ImageSharp.Web.Tests.TestUtilities;
[Collection("Server tests")]
public abstract class ServerTestBase<TFixture> : IClassFixture<TFixture>
where TFixture : TestServerFixture
{
private const int Width = 20;
protected ServerTestBase(TFixture fixture, ITestOutputHelper outputHelper, string imageSource)
{
this.Fixture = fixture;
this.HttpClient = fixture.HttpClient;
this.OutputHelper = outputHelper;
this.ImageSource = imageSource;
this.OutputHelper.WriteLine(typeof(TFixture).Name);
//this.OutputHelper.WriteLine("EnvironmentalVariables");
//foreach (DictionaryEntry item in Environment.GetEnvironmentVariables())
//{
// this.OutputHelper.WriteLine($"Key = {item.Key}, Value = {item.Value}");
//}
}
public TFixture Fixture { get; }
public HttpClient HttpClient { get; }
public ITestOutputHelper OutputHelper { get; }
public string ImageSource { get; }
[Fact]
public async Task CanProcessAndResolveImageAsync()
{
string url = this.ImageSource;
string ext = Path.GetExtension(url);
Assert.True(Configuration.Default.ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat format));
// First response
HttpResponseMessage response = await this.HttpClient.GetAsync(url + this.AugmentCommand(this.Fixture.Commands[0]));
Assert.NotNull(response);
Assert.True(response.IsSuccessStatusCode);
Assert.True(response.Content.Headers.ContentLength > 0);
Assert.Equal(format.DefaultMimeType, response.Content.Headers.ContentType.MediaType);
using Image image = await Image.LoadAsync(await response.Content.ReadAsStreamAsync());
Assert.Equal(Width, image.Width);
Assert.Equal(format, image.Metadata.DecodedImageFormat);
response.Dispose();
// Cached Response
response = await this.HttpClient.GetAsync(url + this.AugmentCommand(this.Fixture.Commands[0]));
Assert.NotNull(response);
Assert.True(response.IsSuccessStatusCode);
Assert.True(response.Content.Headers.ContentLength > 0);
Assert.Equal(format.DefaultMimeType, response.Content.Headers.ContentType.MediaType);
using Image cached = await Image.LoadAsync(await response.Content.ReadAsStreamAsync());
Assert.Equal(Width, cached.Width);
Assert.Equal(format, image.Metadata.DecodedImageFormat);
response.Dispose();
// 304 response
HttpRequestMessage request = new()
{
RequestUri = new Uri(url + this.AugmentCommand(this.Fixture.Commands[0])),
Method = HttpMethod.Get,
};
request.Headers.IfModifiedSince = DateTimeOffset.UtcNow;
response = await this.HttpClient.SendAsync(request);
// This test is flaky in the CI environment.
if (!TestEnvironment.RunsOnCI)
{
Assert.Equal(HttpStatusCode.NotModified, response.StatusCode);
Assert.Equal(0, response.Content.Headers.ContentLength);
}
Assert.Equal(format.DefaultMimeType, response.Content.Headers.ContentType.MediaType);
request.Dispose();
response.Dispose();
// 412 response
request = new HttpRequestMessage
{
RequestUri = new Uri(url + this.AugmentCommand(this.Fixture.Commands[0])),
Method = HttpMethod.Get,
};
request.Headers.IfUnmodifiedSince = DateTimeOffset.MinValue;
response = await this.HttpClient.SendAsync(request);
Assert.Equal(HttpStatusCode.PreconditionFailed, response.StatusCode);
Assert.Equal(0, response.Content.Headers.ContentLength);
request.Dispose();
response.Dispose();
}
[Fact]
public async Task CanProcessMultipleIdenticalQueriesAsync()
{
string url = this.ImageSource;
string command1 = this.AugmentCommand(this.Fixture.Commands[0]);
string command2 = this.AugmentCommand(this.Fixture.Commands[1]);
Task[] tasks = Enumerable.Range(0, 100).Select(i => Task.Run(async () =>
{
string command = i % 2 == 0 ? command1 : command2;
using HttpResponseMessage response = await this.HttpClient.GetAsync(url + command);
Assert.NotNull(response);
Assert.True(response.IsSuccessStatusCode);
Assert.True(response.Content.Headers.ContentLength > 0);
})).ToArray();
Task all = Task.WhenAll(tasks);
await all;
Assert.True(all.IsCompletedSuccessfully);
}
protected virtual string AugmentCommand(string command) => command;
}