summaryrefslogtreecommitdiffstats
path: root/tests/TinyPng.Tests/TinyPngTests.cs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/TinyPng.Tests/TinyPngTests.cs')
-rw-r--r--tests/TinyPng.Tests/TinyPngTests.cs282
1 files changed, 200 insertions, 82 deletions
diff --git a/tests/TinyPng.Tests/TinyPngTests.cs b/tests/TinyPng.Tests/TinyPngTests.cs
index 8ac0054..ce5e6ff 100644
--- a/tests/TinyPng.Tests/TinyPngTests.cs
+++ b/tests/TinyPng.Tests/TinyPngTests.cs
@@ -1,153 +1,225 @@
-using Newtonsoft.Json;
-using System;
+using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
+using TinyPng.ResizeOperations;
namespace TinyPng.Tests
{
- static class Extensions
+ public class TinyPngTests
{
- public static FakeResponseHandler Compress(this FakeResponseHandler fakeResponse)
+ const string apiKey = "lolwat";
+
+ internal const string Cat = "Resources/cat.jpg";
+ internal const string CompressedCat = "Resources/compressedcat.jpg";
+ internal const string ResizedCat = "Resources/resizedcat.jpg";
+
+ [Fact]
+ public void TinyPngClientThrowsWhenNoApiKeySupplied()
{
+ Assert.Throws<ArgumentNullException>(() => new TinyPngClient(null));
+ }
- var content = new TinyPngApiResult();
- content.Input = new TinyPngApiInput
- {
- Size = 18031,
- Type = "image/jpeg"
- };
- content.Output = new TinyPngApiOutput
- {
- Width = 400,
- Height = 400,
- Size = 16646,
- Type = "image/jpeg",
- Ratio = 0.9232f,
- Url = "https://api.tinify.com/output"
- };
-
- var compressResponseMessage = new HttpResponseMessage
- {
- StatusCode = System.Net.HttpStatusCode.Created,
- Content = new StringContent(JsonConvert.SerializeObject(content)),
- };
- compressResponseMessage.Headers.Location = new Uri("https://api.tinify.com/output");
- compressResponseMessage.Headers.Add("Compression-Count", "99");
+ [Fact]
+ public void TinyPngClientThrowsWhenNoValidS3ConfigurationSupplied()
+ {
+ Assert.Throws<ArgumentNullException>(() => new TinyPngClient(null));
+ Assert.Throws<ArgumentNullException>(() => new TinyPngClient(null, null));
+ Assert.Throws<ArgumentNullException>(() => new TinyPngClient("apiKey", null));
+ Assert.Throws<ArgumentNullException>(() => new TinyPngClient(null, new AmazonS3Configuration("a", "b", "c", "d")));
+ }
+
+ [Fact]
+ public async Task Compression()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler().Compress());
+
+ var result = await pngx.Compress(Cat);
- fakeResponse.AddFakePostResponse(new Uri("https://api.tinify.com/shrink"), compressResponseMessage);
- return fakeResponse;
+ Assert.Equal("image/jpeg", result.Input.Type);
+ Assert.Equal(400, result.Output.Width);
+ Assert.Equal(400, result.Output.Height);
}
- public static FakeResponseHandler Download(this FakeResponseHandler fakeResponse)
+
+ [Fact]
+ public async Task CompressionWithBytes()
{
- var compressedCatStream = File.OpenRead(TinyPngTests.CompressedCat);
- var outputResponseMessage = new HttpResponseMessage
- {
- Content = new StreamContent(compressedCatStream),
- StatusCode = System.Net.HttpStatusCode.OK
- };
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler().Compress());
+
+ var bytes = File.ReadAllBytes(Cat);
+ var result = await pngx.Compress(bytes);
- fakeResponse.AddFakeGetResponse(new Uri("https://api.tinify.com/output"), outputResponseMessage);
- return fakeResponse;
+ Assert.Equal("image/jpeg", result.Input.Type);
+ Assert.Equal(400, result.Output.Width);
+ Assert.Equal(400, result.Output.Height);
}
- public static FakeResponseHandler Resize(this FakeResponseHandler fakeResponse)
+ [Fact]
+ public async Task CompressionWithStreams()
{
- var resizedCatStream = File.OpenRead(TinyPngTests.ResizedCat);
- var resizeMessage = new HttpResponseMessage
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler().Compress());
+
+ using (var fileStream = File.OpenRead(Cat))
{
- StatusCode = System.Net.HttpStatusCode.OK,
- Content = new StreamContent(resizedCatStream)
- };
- resizeMessage.Headers.Add("Image-Width", "150");
- resizeMessage.Headers.Add("Image-Height", "150");
+ var result = await pngx.Compress(fileStream);
- fakeResponse.AddFakePostResponse(new Uri("https://api.tinify.com/output"), resizeMessage);
- return fakeResponse;
+ Assert.Equal("image/jpeg", result.Input.Type);
+ Assert.Equal(400, result.Output.Width);
+ Assert.Equal(400, result.Output.Height);
+ }
}
- public static FakeResponseHandler S3(this FakeResponseHandler fakeResponse)
+ [Fact]
+ public async Task CompressionShouldThrowIfNoPathToFile()
{
- var amazonMessage = new HttpResponseMessage
- {
- StatusCode = System.Net.HttpStatusCode.OK
- };
- amazonMessage.Headers.Add("Location", "https://s3-ap-southeast-2.amazonaws.com/tinypng-test-bucket/path.jpg");
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler().Compress());
- fakeResponse.AddFakePostResponse(new Uri("https://api.tinify.com/output"), amazonMessage);
- return fakeResponse;
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await pngx.Compress(string.Empty));
}
- }
- public class TinyPngTests
- {
- const string apiKey = "lolwat";
+ [Fact]
+ public async Task CompressionShouldThrowIfNoNonSuccessStatusCode()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler().CompressAndFail());
- internal const string Cat = "Resources/cat.jpg";
- internal const string CompressedCat = "Resources/compressedcat.jpg";
- internal const string ResizedCat = "Resources/resizedcat.jpg";
+ await Assert.ThrowsAsync<TinyPngApiException>(async () => await pngx.Compress(Cat));
+ }
[Fact]
- public async Task Compression()
+ public async Task CompressionAndDownload()
{
+
var pngx = new TinyPngClient(apiKey);
- pngx.httpClient = new HttpClient(new FakeResponseHandler().Compress());
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .Download());
- var result = await pngx.Compress(Cat);
+ var downloadResult = await pngx.Compress(Cat)
+ .Download()
+ .GetImageByteData();
- Assert.Equal("image/jpeg", result.Input.Type);
- Assert.Equal(400, result.Output.Width);
- Assert.Equal(400, result.Output.Height);
+ Assert.Equal(16646, downloadResult.Length);
+ }
+
+ [Fact]
+ public async Task ResizingOperationThrows()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .Resize());
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await pngx.Compress((string)null).Resize(150, 150));
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await pngx.Compress((string)null).Resize(null));
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await pngx.Compress(Cat).Resize(null));
+ await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await pngx.Compress(Cat).Resize(0, 150));
+ await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await pngx.Compress(Cat).Resize(150, 0));
}
[Fact]
- public async Task CompressionAndDownload()
+ public async Task ResizingOperation()
{
var pngx = new TinyPngClient(apiKey);
- pngx.httpClient = new HttpClient(new FakeResponseHandler()
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
.Compress()
- .Download());
+ .Resize());
- var result = await pngx.Compress(Cat);
+ var resizedImageByteData = await pngx.Compress(Cat).Resize(150, 150).GetImageByteData();
- var downloadResult = await pngx.Download(result);
+ Assert.Equal(5970, resizedImageByteData.Length);
+ }
- Assert.Equal(16646, (await downloadResult.GetImageByteData()).Length);
+ [Fact]
+ public async Task ResizingScaleHeightOperation()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .Resize());
+
+ var resizedImageByteData = await pngx.Compress(Cat).Resize(new ScaleHeightResizeOperation(150)).GetImageByteData();
+
+ Assert.Equal(5970, resizedImageByteData.Length);
}
[Fact]
- public async Task Resizing()
+ public async Task ResizingScaleWidthOperation()
{
var pngx = new TinyPngClient(apiKey);
- pngx.httpClient = new HttpClient(new FakeResponseHandler()
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .Resize());
+
+ var resizedImageByteData = await pngx.Compress(Cat).Resize(new ScaleWidthResizeOperation(150)).GetImageByteData();
+
+ Assert.Equal(5970, resizedImageByteData.Length);
+ }
+
+ [Fact]
+ public async Task ResizingFitResizeOperation()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
.Compress()
.Resize());
var result = await pngx.Compress(Cat);
- var resized = await pngx.Resize(result, new ScaleHeightResizeOperation(150));
+ var resizedImageByteData = await pngx.Compress(Cat).Resize(new FitResizeOperation(150, 150)).GetImageByteData();
+
+ Assert.Equal(5970, resizedImageByteData.Length);
+ }
+
+ [Fact]
+ public async Task ResizingCoverResizeOperation()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .Resize());
- var resizedImageByteData = await resized.GetImageByteData();
+ var resizedImageByteData = await pngx.Compress(Cat).Resize(new CoverResizeOperation(150, 150)).GetImageByteData();
Assert.Equal(5970, resizedImageByteData.Length);
+ }
+ [Fact]
+ public async Task ResizingCoverResizeOperationThrowsWithInvalidParams()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .Resize());
+
+ await Assert.ThrowsAsync<ArgumentException>(async () => await pngx.Compress(Cat).Resize(new CoverResizeOperation(0, 150)));
+ await Assert.ThrowsAsync<ArgumentException>(async () => await pngx.Compress(Cat).Resize(new CoverResizeOperation(150, 0)));
}
+ [Fact]
+ public void CompressAndStoreToS3ShouldThrowIfNoApiKeyProvided()
+ {
+ Assert.Throws<ArgumentNullException>(() => new TinyPngClient(string.Empty, new AmazonS3Configuration("a", "b", "c", "d")));
+ }
[Fact]
public async Task CompressAndStoreToS3ShouldThrowIfS3HasNotBeenConfigured()
{
var pngx = new TinyPngClient(apiKey);
- pngx.httpClient = new HttpClient(new FakeResponseHandler()
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
.Compress()
.S3());
var result = await pngx.Compress(Cat);
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await pngx.SaveCompressedImageToAmazonS3(null, "bucket/path.jpg"));
+ await Assert.ThrowsAsync<InvalidOperationException>(async () => await pngx.SaveCompressedImageToAmazonS3(result, string.Empty));
await Assert.ThrowsAsync<InvalidOperationException>(async () => await pngx.SaveCompressedImageToAmazonS3(result, "bucket/path.jpg"));
-
}
private const string ApiKey = "lolwat";
@@ -157,7 +229,7 @@ namespace TinyPng.Tests
public async Task CompressAndStoreToS3()
{
var pngx = new TinyPngClient(apiKey);
- pngx.httpClient = new HttpClient(new FakeResponseHandler()
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
.Compress()
.S3());
@@ -168,7 +240,53 @@ namespace TinyPng.Tests
"path.jpg")).ToString();
Assert.Equal("https://s3-ap-southeast-2.amazonaws.com/tinypng-test-bucket/path.jpg", sendToAmazon);
+ }
+
+ [Fact]
+ public async Task CompressAndStoreToS3Throws()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .S3());
+ var result = await pngx.Compress(Cat);
+
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await pngx.SaveCompressedImageToAmazonS3(result, null, string.Empty));
+
+ //S3 configuration has not been set
+ await Assert.ThrowsAsync<InvalidOperationException>(async () => await pngx.SaveCompressedImageToAmazonS3(result, path: string.Empty));
+ }
+
+ [Fact]
+ public async Task CompressAndStoreToS3WithOptionsPassedIntoConstructor()
+ {
+ var pngx = new TinyPngClient(apiKey, new AmazonS3Configuration(ApiKey, ApiAccessKey, "tinypng-test-bucket", "ap-southeast-2"));
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .S3());
+
+ var result = await pngx.Compress(Cat);
+ var sendToAmazon = (await pngx.SaveCompressedImageToAmazonS3(result, "path.jpg")).ToString();
+
+ Assert.Equal("https://s3-ap-southeast-2.amazonaws.com/tinypng-test-bucket/path.jpg", sendToAmazon);
+ }
+
+ [Fact]
+ public void TinyPngExceptionPopulatesCorrectData()
+ {
+ var StatusCode = 200;
+ var StatusReasonPhrase = "status";
+ var ErrorTitle = "title";
+ var ErrorMessage = "message";
+ var e = new TinyPngApiException(StatusCode, StatusReasonPhrase, ErrorTitle, "message");
+
+ var msg = $"Api Service returned a non-success status code when attempting an operation on an image: {StatusCode} - {StatusReasonPhrase}. {ErrorTitle}, {ErrorMessage}";
+
+ Assert.Equal(StatusCode, e.StatusCode);
+ Assert.Equal(StatusReasonPhrase, e.StatusReasonPhrase);
+ Assert.Equal(ErrorTitle, e.ErrorTitle);
+ Assert.Equal(msg, e.Message);
}
}
}