|
1 | | -// // Copyright (c) Six Labors. |
2 | | -// // Licensed under the Apache License, Version 2.0. |
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
3 | 3 |
|
4 | | -// using System; |
5 | | -// using System.IO; |
6 | | -// using System.Linq; |
7 | | -// using System.Net; |
8 | | -// using System.Net.Http; |
9 | | -// using System.Threading.Tasks; |
10 | | -// using SixLabors.ImageSharp.Formats; |
11 | | -// using SixLabors.ImageSharp.Web.Tests.TestUtilities; |
12 | | -// using Xunit; |
13 | | - |
14 | | -// namespace SixLabors.ImageSharp.Web.Tests.Processing |
15 | | -// { |
16 | | -// public class AzureBlobStorageCacheServerTests : ServerTestBase<AzureBlobStorageCacheTestServerFixture> |
17 | | -// { |
18 | | -// private const int Width = 20; |
19 | | -// private static readonly string Command = "?width=" + Width + "&v=" + Guid.NewGuid().ToString(); |
20 | | -// private static readonly string Command2 = "?width=" + (Width + 1) + "&v=" + Guid.NewGuid().ToString(); |
21 | | - |
22 | | -// public AzureBlobStorageCacheServerTests(AzureBlobStorageCacheTestServerFixture fixture) |
23 | | -// : base(fixture) |
24 | | -// { |
25 | | -// } |
26 | | - |
27 | | -// [Theory] |
28 | | -// [InlineData(TestConstants.PhysicalTestImage)] |
29 | | -// [InlineData(TestConstants.AzureTestImage)] |
30 | | -// public async Task CanProcessAndResolveImageAsync(string url) |
31 | | -// { |
32 | | -// string ext = Path.GetExtension(url); |
33 | | -// IImageFormat format = Configuration.Default.ImageFormatsManager.FindFormatByFileExtension(ext); |
34 | | - |
35 | | -// // First response |
36 | | -// HttpResponseMessage response = await this.HttpClient.GetAsync(url + Command); |
37 | | - |
38 | | -// Assert.NotNull(response); |
39 | | -// Assert.True(response.IsSuccessStatusCode); |
40 | | -// Assert.True(response.Content.Headers.ContentLength > 0); |
41 | | -// Assert.Equal(format.DefaultMimeType, response.Content.Headers.ContentType.MediaType); |
42 | | - |
43 | | -// (Image Image, IImageFormat Format) actual = await Image.LoadWithFormatAsync(await response.Content.ReadAsStreamAsync()); |
44 | | -// using Image image = actual.Image; |
45 | | - |
46 | | -// Assert.Equal(Width, image.Width); |
47 | | -// Assert.Equal(format, actual.Format); |
48 | | - |
49 | | -// response.Dispose(); |
50 | | - |
51 | | -// // Cached Response |
52 | | -// response = await this.HttpClient.GetAsync(url + Command); |
53 | | - |
54 | | -// Assert.NotNull(response); |
55 | | -// Assert.True(response.IsSuccessStatusCode); |
56 | | -// Assert.True(response.Content.Headers.ContentLength > 0); |
57 | | -// Assert.Equal(format.DefaultMimeType, response.Content.Headers.ContentType.MediaType); |
58 | | - |
59 | | -// (Image Image, IImageFormat Format) cachedActual = await Image.LoadWithFormatAsync(await response.Content.ReadAsStreamAsync()); |
60 | | -// using Image cached = cachedActual.Image; |
61 | | - |
62 | | -// Assert.Equal(Width, cached.Width); |
63 | | -// Assert.Equal(format, actual.Format); |
64 | | - |
65 | | -// response.Dispose(); |
66 | | - |
67 | | -// // 304 response |
68 | | -// var request = new HttpRequestMessage |
69 | | -// { |
70 | | -// RequestUri = new Uri(url + Command), |
71 | | -// Method = HttpMethod.Get, |
72 | | -// }; |
73 | | - |
74 | | -// request.Headers.IfModifiedSince = DateTimeOffset.UtcNow; |
75 | | - |
76 | | -// response = await this.HttpClient.SendAsync(request); |
77 | | - |
78 | | -// Assert.Equal(HttpStatusCode.NotModified, response.StatusCode); |
79 | | -// Assert.Equal(0, response.Content.Headers.ContentLength); |
80 | | -// Assert.Equal(format.DefaultMimeType, response.Content.Headers.ContentType.MediaType); |
81 | | - |
82 | | -// request.Dispose(); |
83 | | -// response.Dispose(); |
84 | | - |
85 | | -// // 412 response |
86 | | -// request = new HttpRequestMessage |
87 | | -// { |
88 | | -// RequestUri = new Uri(url + Command), |
89 | | -// Method = HttpMethod.Get, |
90 | | -// }; |
91 | | - |
92 | | -// request.Headers.IfUnmodifiedSince = DateTimeOffset.MinValue; |
93 | | - |
94 | | -// response = await this.HttpClient.SendAsync(request); |
95 | | - |
96 | | -// Assert.Equal(HttpStatusCode.PreconditionFailed, response.StatusCode); |
97 | | -// Assert.Equal(0, response.Content.Headers.ContentLength); |
98 | | - |
99 | | -// request.Dispose(); |
100 | | -// response.Dispose(); |
101 | | -// } |
102 | | - |
103 | | -// // [Theory] |
104 | | -// // [InlineData(TestConstants.PhysicalTestImage)] |
105 | | -// // [InlineData(TestConstants.AzureTestImage)] |
106 | | -// // public async Task CanProcessMultipleIdenticalQueriesAsync(string url) |
107 | | -// // { |
108 | | -// // Task[] tasks = Enumerable.Range(0, 100).Select(i => Task.Run(async () => |
109 | | -// // { |
110 | | -// // var command = i % 2 == 0 ? Command : Command2; |
111 | | -// // using HttpResponseMessage response = await this.HttpClient.GetAsync(url + command); |
112 | | -// // Assert.NotNull(response); |
113 | | -// // Assert.True(response.IsSuccessStatusCode); |
114 | | -// // })).ToArray(); |
115 | | - |
116 | | -// // var all = Task.WhenAll(tasks); |
117 | | -// // await all; |
118 | | -// // Assert.True(all.IsCompletedSuccessfully); |
119 | | -// // } |
120 | | -// } |
121 | | -// } |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Net; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using SixLabors.ImageSharp.Formats; |
| 11 | +using SixLabors.ImageSharp.Web.Tests.TestUtilities; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace SixLabors.ImageSharp.Web.Tests.Processing |
| 15 | +{ |
| 16 | + public class AzureBlobStorageCacheServerTests : ServerTestBase<AzureBlobStorageCacheTestServerFixture> |
| 17 | + { |
| 18 | + private const int Width = 20; |
| 19 | + private static readonly string Command = "?width=" + Width + "&v=" + Guid.NewGuid().ToString(); |
| 20 | + private static readonly string Command2 = "?width=" + (Width + 1) + "&v=" + Guid.NewGuid().ToString(); |
| 21 | + |
| 22 | + public AzureBlobStorageCacheServerTests(AzureBlobStorageCacheTestServerFixture fixture) |
| 23 | + : base(fixture) |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + [Theory] |
| 28 | + [InlineData(TestConstants.PhysicalTestImage)] |
| 29 | + [InlineData(TestConstants.AzureTestImage)] |
| 30 | + public async Task CanProcessAndResolveImageAsync(string url) |
| 31 | + { |
| 32 | + string ext = Path.GetExtension(url); |
| 33 | + IImageFormat format = Configuration.Default.ImageFormatsManager.FindFormatByFileExtension(ext); |
| 34 | + |
| 35 | + // First response |
| 36 | + HttpResponseMessage response = await this.HttpClient.GetAsync(url + Command); |
| 37 | + |
| 38 | + Assert.NotNull(response); |
| 39 | + Assert.True(response.IsSuccessStatusCode); |
| 40 | + Assert.True(response.Content.Headers.ContentLength > 0); |
| 41 | + Assert.Equal(format.DefaultMimeType, response.Content.Headers.ContentType.MediaType); |
| 42 | + |
| 43 | + (Image Image, IImageFormat Format) actual = await Image.LoadWithFormatAsync(await response.Content.ReadAsStreamAsync()); |
| 44 | + using Image image = actual.Image; |
| 45 | + |
| 46 | + Assert.Equal(Width, image.Width); |
| 47 | + Assert.Equal(format, actual.Format); |
| 48 | + |
| 49 | + response.Dispose(); |
| 50 | + |
| 51 | + // Cached Response |
| 52 | + response = await this.HttpClient.GetAsync(url + Command); |
| 53 | + |
| 54 | + Assert.NotNull(response); |
| 55 | + Assert.True(response.IsSuccessStatusCode); |
| 56 | + Assert.True(response.Content.Headers.ContentLength > 0); |
| 57 | + Assert.Equal(format.DefaultMimeType, response.Content.Headers.ContentType.MediaType); |
| 58 | + |
| 59 | + (Image Image, IImageFormat Format) cachedActual = await Image.LoadWithFormatAsync(await response.Content.ReadAsStreamAsync()); |
| 60 | + using Image cached = cachedActual.Image; |
| 61 | + |
| 62 | + Assert.Equal(Width, cached.Width); |
| 63 | + Assert.Equal(format, actual.Format); |
| 64 | + |
| 65 | + response.Dispose(); |
| 66 | + |
| 67 | + // 304 response |
| 68 | + var request = new HttpRequestMessage |
| 69 | + { |
| 70 | + RequestUri = new Uri(url + Command), |
| 71 | + Method = HttpMethod.Get, |
| 72 | + }; |
| 73 | + |
| 74 | + request.Headers.IfModifiedSince = DateTimeOffset.UtcNow; |
| 75 | + |
| 76 | + response = await this.HttpClient.SendAsync(request); |
| 77 | + |
| 78 | + Assert.Equal(HttpStatusCode.NotModified, response.StatusCode); |
| 79 | + Assert.Equal(0, response.Content.Headers.ContentLength); |
| 80 | + Assert.Equal(format.DefaultMimeType, response.Content.Headers.ContentType.MediaType); |
| 81 | + |
| 82 | + request.Dispose(); |
| 83 | + response.Dispose(); |
| 84 | + |
| 85 | + // 412 response |
| 86 | + request = new HttpRequestMessage |
| 87 | + { |
| 88 | + RequestUri = new Uri(url + Command), |
| 89 | + Method = HttpMethod.Get, |
| 90 | + }; |
| 91 | + |
| 92 | + request.Headers.IfUnmodifiedSince = DateTimeOffset.MinValue; |
| 93 | + |
| 94 | + response = await this.HttpClient.SendAsync(request); |
| 95 | + |
| 96 | + Assert.Equal(HttpStatusCode.PreconditionFailed, response.StatusCode); |
| 97 | + Assert.Equal(0, response.Content.Headers.ContentLength); |
| 98 | + |
| 99 | + request.Dispose(); |
| 100 | + response.Dispose(); |
| 101 | + } |
| 102 | + |
| 103 | + [Theory] |
| 104 | + [InlineData(TestConstants.PhysicalTestImage)] |
| 105 | + [InlineData(TestConstants.AzureTestImage)] |
| 106 | + public async Task CanProcessMultipleIdenticalQueriesAsync(string url) |
| 107 | + { |
| 108 | + Task[] tasks = Enumerable.Range(0, 100).Select(i => Task.Run(async () => |
| 109 | + { |
| 110 | + var command = i % 2 == 0 ? Command : Command2; |
| 111 | + using HttpResponseMessage response = await this.HttpClient.GetAsync(url + command); |
| 112 | + Assert.NotNull(response); |
| 113 | + Assert.True(response.IsSuccessStatusCode); |
| 114 | + Assert.True(response.Content.Headers.ContentLength > 0); |
| 115 | + })).ToArray(); |
| 116 | + |
| 117 | + var all = Task.WhenAll(tasks); |
| 118 | + await all; |
| 119 | + Assert.True(all.IsCompletedSuccessfully); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments