Skip to content

Commit 661b721

Browse files
Refactor tests and add new integration tests
1 parent 97852c1 commit 661b721

12 files changed

+448
-1
lines changed

src/ImageSharp.Web/DependencyInjection/ImageSharpCoreBuilderExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Linq;
67
using System.Reflection;
78
using Microsoft.Extensions.Configuration;
@@ -180,6 +181,18 @@ public static IImageSharpBuilder RemoveProvider<TProvider>(this IImageSharpBuild
180181
return builder;
181182
}
182183

184+
/// <summary>
185+
/// Removes all <see cref="IImageProvider"/> instances from the provider collection within the service collection.
186+
/// </summary>
187+
/// <param name="builder">The core builder.</param>
188+
/// <returns>The <see cref="IImageSharpBuilder"/>.</returns>
189+
public static IImageSharpBuilder ClearProviders(this IImageSharpBuilder builder)
190+
{
191+
builder.Services.RemoveAll(typeof(IImageProvider));
192+
193+
return builder;
194+
}
195+
183196
/// <summary>
184197
/// Adds the given <see cref="IImageWebProcessor"/> to the processor collection within the service collection.
185198
/// </summary>

tests/ImageSharp.Web.Tests/ImageSharpTestServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void ConfigureServices(IServiceCollection services)
170170
return CreateTestServer(DefaultConfig, ConfigureServices);
171171
}
172172

173-
private static TestServer CreateTestServer(
173+
public static TestServer CreateTestServer(
174174
Action<IApplicationBuilder> configureApp,
175175
Action<IServiceCollection> configureServices)
176176
{
File renamed without changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
using System.IO;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
using SixLabors.ImageSharp.Formats;
9+
using SixLabors.ImageSharp.Web.Tests.TestUtilities;
10+
using Xunit;
11+
12+
namespace SixLabors.ImageSharp.Web.Tests.Processing
13+
{
14+
public class AzureBlobStorageCacheServerTests : ServerTestBase<AzureBlobStorageCacheTestServerFixture>
15+
{
16+
private const int Width = 20;
17+
private static readonly string Command = "?width=" + Width + "&v=" + Guid.NewGuid().ToString();
18+
19+
public AzureBlobStorageCacheServerTests(AzureBlobStorageCacheTestServerFixture fixture)
20+
: base(fixture)
21+
{
22+
}
23+
24+
[Theory]
25+
[InlineData(TestConstants.PhysicalTestImage)]
26+
[InlineData(TestConstants.AzureTestImage)]
27+
public async Task CanProcessAndResolveImage(string url)
28+
{
29+
string ext = Path.GetExtension(url);
30+
IImageFormat format = Configuration.Default.ImageFormatsManager.FindFormatByFileExtension(ext);
31+
32+
// First response
33+
HttpResponseMessage response = await this.HttpClient.GetAsync(url + Command);
34+
35+
Assert.NotNull(response);
36+
Assert.True(response.IsSuccessStatusCode);
37+
38+
(Image Image, IImageFormat Format) actual = await Image.LoadWithFormatAsync(await response.Content.ReadAsStreamAsync());
39+
using Image image = actual.Image;
40+
41+
Assert.Equal(Width, image.Width);
42+
Assert.Equal(format, actual.Format);
43+
44+
// Cached Response
45+
response = await this.HttpClient.GetAsync(url + Command);
46+
47+
Assert.NotNull(response);
48+
Assert.True(response.IsSuccessStatusCode);
49+
50+
(Image Image, IImageFormat Format) cachedActual = await Image.LoadWithFormatAsync(await response.Content.ReadAsStreamAsync());
51+
using Image cached = cachedActual.Image;
52+
53+
Assert.Equal(Width, cached.Width);
54+
Assert.Equal(format, actual.Format);
55+
}
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
using System.IO;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
using SixLabors.ImageSharp.Formats;
9+
using SixLabors.ImageSharp.Web.Tests.TestUtilities;
10+
using Xunit;
11+
12+
namespace SixLabors.ImageSharp.Web.Tests.Processing
13+
{
14+
public class PhysicalFileSystemCacheServerTests : ServerTestBase<PhysicalFileSystemCacheTestServerFixture>
15+
{
16+
private const int Width = 20;
17+
private static readonly string Command = "?width=" + Width + "&v=" + Guid.NewGuid().ToString();
18+
19+
public PhysicalFileSystemCacheServerTests(PhysicalFileSystemCacheTestServerFixture fixture)
20+
: base(fixture)
21+
{
22+
}
23+
24+
[Theory]
25+
[InlineData(TestConstants.PhysicalTestImage)]
26+
[InlineData(TestConstants.AzureTestImage)]
27+
public async Task CanProcessAndResolveImage(string url)
28+
{
29+
string ext = Path.GetExtension(url);
30+
IImageFormat format = Configuration.Default.ImageFormatsManager.FindFormatByFileExtension(ext);
31+
32+
// First response
33+
HttpResponseMessage response = await this.HttpClient.GetAsync(url + Command);
34+
35+
Assert.NotNull(response);
36+
Assert.True(response.IsSuccessStatusCode);
37+
38+
(Image Image, IImageFormat Format) actual = await Image.LoadWithFormatAsync(await response.Content.ReadAsStreamAsync());
39+
using Image image = actual.Image;
40+
41+
Assert.Equal(Width, image.Width);
42+
Assert.Equal(format, actual.Format);
43+
44+
// Cached Response
45+
response = await this.HttpClient.GetAsync(url + Command);
46+
47+
Assert.NotNull(response);
48+
Assert.True(response.IsSuccessStatusCode);
49+
50+
(Image Image, IImageFormat Format) cachedActual = await Image.LoadWithFormatAsync(await response.Content.ReadAsStreamAsync());
51+
using Image cached = cachedActual.Image;
52+
53+
Assert.Equal(Width, cached.Width);
54+
Assert.Equal(format, actual.Format);
55+
}
56+
}
57+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using SixLabors.ImageSharp.Web.Caching.Azure;
7+
using SixLabors.ImageSharp.Web.DependencyInjection;
8+
using SixLabors.ImageSharp.Web.Providers;
9+
using SixLabors.ImageSharp.Web.Providers.Azure;
10+
using Xunit;
11+
12+
namespace SixLabors.ImageSharp.Web.Tests.TestUtilities
13+
{
14+
public class AzureBlobStorageCacheTestServerFixture : TestServerFixture
15+
{
16+
protected override void ConfigureServices(IServiceCollection services)
17+
{
18+
services.AddImageSharp(options =>
19+
{
20+
options.OnParseCommands = context =>
21+
{
22+
Assert.NotNull(context);
23+
Assert.NotNull(context.Context);
24+
Assert.NotNull(context.Commands);
25+
Assert.NotNull(context.Parser);
26+
};
27+
28+
options.OnProcessed = context =>
29+
{
30+
Assert.NotNull(context);
31+
Assert.NotNull(context.Commands);
32+
Assert.NotNull(context.ContentType);
33+
Assert.NotNull(context.Context);
34+
Assert.NotNull(context.Extension);
35+
Assert.NotNull(context.Stream);
36+
};
37+
38+
options.OnBeforeSave = context =>
39+
{
40+
Assert.NotNull(context);
41+
Assert.NotNull(context.Format);
42+
Assert.NotNull(context.Image);
43+
};
44+
45+
options.OnPrepareResponse = context =>
46+
{
47+
Assert.NotNull(context);
48+
Assert.NotNull(context.Response);
49+
};
50+
})
51+
.ClearProviders()
52+
.Configure<AzureBlobStorageImageProviderOptions>(options =>
53+
{
54+
options.BlobContainers.Add(new AzureBlobContainerClientOptions
55+
{
56+
ConnectionString = TestConstants.AzureConnectionString,
57+
ContainerName = TestConstants.AzureContainerName
58+
});
59+
})
60+
.AddProvider(AzureBlobStorageImageProviderFactory.Create)
61+
.AddProvider<PhysicalFileSystemProvider>()
62+
.AddProcessor<CacheBusterWebProcessor>()
63+
.Configure<AzureBlobStorageCacheOptions>(options =>
64+
{
65+
options.ConnectionString = TestConstants.AzureConnectionString;
66+
options.ContainerName = TestConstants.AzureCacheContainerName;
67+
})
68+
.SetCache<AzureBlobStorageCache>();
69+
}
70+
71+
protected override void Configure(IApplicationBuilder app)
72+
{
73+
app.UseImageSharp();
74+
}
75+
}
76+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
using System.IO;
6+
using System.Linq;
7+
using Azure.Storage.Blobs;
8+
using Azure.Storage.Blobs.Models;
9+
using Microsoft.AspNetCore.Hosting;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.FileProviders;
12+
using Microsoft.Extensions.Options;
13+
using SixLabors.ImageSharp.Web.Providers.Azure;
14+
15+
namespace SixLabors.ImageSharp.Web.Tests.TestUtilities
16+
{
17+
public static class AzureBlobStorageImageProviderFactory
18+
{
19+
public static AzureBlobStorageImageProvider Create(IServiceProvider services)
20+
{
21+
IOptions<AzureBlobStorageImageProviderOptions> options = services.GetRequiredService<IOptions<AzureBlobStorageImageProviderOptions>>();
22+
FormatUtilities utilities = services.GetRequiredService<FormatUtilities>();
23+
InitializeAzureStorage(services, options.Value);
24+
25+
return new AzureBlobStorageImageProvider(options, utilities);
26+
}
27+
28+
private static void InitializeAzureStorage(IServiceProvider services, AzureBlobStorageImageProviderOptions options)
29+
{
30+
// Upload an image to the Azure Test Storage;
31+
AzureBlobContainerClientOptions containerOptions = options.BlobContainers.First();
32+
var container = new BlobContainerClient(containerOptions.ConnectionString, containerOptions.ContainerName);
33+
container.CreateIfNotExists(PublicAccessType.Blob);
34+
35+
#if NETCOREAPP2_1
36+
IHostingEnvironment environment = services.GetRequiredService<IHostingEnvironment>();
37+
#else
38+
IWebHostEnvironment environment = services.GetRequiredService<IWebHostEnvironment>();
39+
#endif
40+
41+
BlobClient blob = container.GetBlobClient(TestConstants.ImagePath);
42+
43+
if (!blob.Exists())
44+
{
45+
IFileInfo file = environment.WebRootFileProvider.GetFileInfo(TestConstants.ImagePath);
46+
using Stream stream = file.CreateReadStream();
47+
blob.Upload(stream, true);
48+
}
49+
}
50+
}
51+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Collections.Generic;
5+
using Microsoft.Extensions.Logging;
6+
using SixLabors.ImageSharp.Web.Processors;
7+
8+
namespace SixLabors.ImageSharp.Web.Tests.TestUtilities
9+
{
10+
public class CacheBusterWebProcessor : IImageWebProcessor
11+
{
12+
public const string Command = "v";
13+
14+
public IEnumerable<string> Commands { get; } = new[] { Command };
15+
16+
public FormattedImage Process(FormattedImage image, ILogger logger, IDictionary<string, string> commands)
17+
{
18+
return image;
19+
}
20+
}
21+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using SixLabors.ImageSharp.Web.DependencyInjection;
7+
using SixLabors.ImageSharp.Web.Providers;
8+
using SixLabors.ImageSharp.Web.Providers.Azure;
9+
using Xunit;
10+
11+
namespace SixLabors.ImageSharp.Web.Tests.TestUtilities
12+
{
13+
public class PhysicalFileSystemCacheTestServerFixture : TestServerFixture
14+
{
15+
protected override void ConfigureServices(IServiceCollection services)
16+
{
17+
services.AddImageSharp(options =>
18+
{
19+
options.OnParseCommands = context =>
20+
{
21+
Assert.NotNull(context);
22+
Assert.NotNull(context.Context);
23+
Assert.NotNull(context.Commands);
24+
Assert.NotNull(context.Parser);
25+
};
26+
27+
options.OnProcessed = context =>
28+
{
29+
Assert.NotNull(context);
30+
Assert.NotNull(context.Commands);
31+
Assert.NotNull(context.ContentType);
32+
Assert.NotNull(context.Context);
33+
Assert.NotNull(context.Extension);
34+
Assert.NotNull(context.Stream);
35+
};
36+
37+
options.OnBeforeSave = context =>
38+
{
39+
Assert.NotNull(context);
40+
Assert.NotNull(context.Format);
41+
Assert.NotNull(context.Image);
42+
};
43+
44+
options.OnPrepareResponse = context =>
45+
{
46+
Assert.NotNull(context);
47+
Assert.NotNull(context.Response);
48+
};
49+
})
50+
.ClearProviders()
51+
.Configure<AzureBlobStorageImageProviderOptions>(options =>
52+
{
53+
options.BlobContainers.Add(new AzureBlobContainerClientOptions
54+
{
55+
ConnectionString = TestConstants.AzureConnectionString,
56+
ContainerName = TestConstants.AzureContainerName
57+
});
58+
})
59+
.AddProvider(AzureBlobStorageImageProviderFactory.Create)
60+
.AddProvider<PhysicalFileSystemProvider>()
61+
.AddProcessor<CacheBusterWebProcessor>();
62+
}
63+
64+
protected override void Configure(IApplicationBuilder app)
65+
{
66+
app.UseImageSharp();
67+
}
68+
}
69+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Net.Http;
5+
using Xunit;
6+
7+
namespace SixLabors.ImageSharp.Web.Tests.TestUtilities
8+
{
9+
public abstract class ServerTestBase<TFixture> : IClassFixture<TFixture>
10+
where TFixture : TestServerFixture
11+
{
12+
protected ServerTestBase(TFixture fixture)
13+
{
14+
this.HttpClient = fixture.HttpClient;
15+
}
16+
17+
public HttpClient HttpClient { get; }
18+
}
19+
}

0 commit comments

Comments
 (0)