summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChad T <chad@tolkien.id.au>2017-04-25 13:15:12 +1000
committerGitHub <noreply@github.com>2017-04-25 13:15:12 +1000
commitdf65bab457dbfaa91b5898c0282293a01128da29 (patch)
treece7d048c3801acaf9de7ffc94befd9ab2be417f8
parent1acf9e5ff67aa3c818c3e7844102f345b322b62c (diff)
downloadTinyPNG-df65bab457dbfaa91b5898c0282293a01128da29.zip
TinyPNG-df65bab457dbfaa91b5898c0282293a01128da29.tar.gz
TinyPNG-df65bab457dbfaa91b5898c0282293a01128da29.tar.bz2
Additional tests (#19)
* Additional tests * also test compression count * skip initial dotnet setup * more error tests * test more error conditions
-rw-r--r--appveyor.yml3
-rw-r--r--src/TinyPNG/AmazonS3Configuration.cs6
-rw-r--r--src/TinyPNG/Extensions/DownloadExtensions.cs2
-rw-r--r--src/TinyPNG/Extensions/ImageDataExtensions.cs4
-rw-r--r--tests/TinyPng.Tests/Extensions.cs25
-rw-r--r--tests/TinyPng.Tests/TinyPngTests.cs104
6 files changed, 136 insertions, 8 deletions
diff --git a/appveyor.yml b/appveyor.yml
index 1338ac7..514bcaf 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -1,6 +1,9 @@
os: Visual Studio 2017
version: 3.0.{build}
configuration: Release
+environment:
+ DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
+ DOTNET_CLI_TELEMETRY_OPTOUT: 1
pull_requests:
do_not_increment_build_number: true
init:
diff --git a/src/TinyPNG/AmazonS3Configuration.cs b/src/TinyPNG/AmazonS3Configuration.cs
index 723cd0f..4dfca34 100644
--- a/src/TinyPNG/AmazonS3Configuration.cs
+++ b/src/TinyPNG/AmazonS3Configuration.cs
@@ -7,9 +7,9 @@ namespace TinyPng
[JsonProperty("service")]
public const string Service = "s3";
- public AmazonS3Configuration(string awsAccessKeyId,
- string awsSecretAccessKey,
- string defaultBucket,
+ public AmazonS3Configuration(string awsAccessKeyId,
+ string awsSecretAccessKey,
+ string defaultBucket,
string defaultRegion)
{
AwsAccessKeyId = awsAccessKeyId;
diff --git a/src/TinyPNG/Extensions/DownloadExtensions.cs b/src/TinyPNG/Extensions/DownloadExtensions.cs
index c12d40e..7b55b8c 100644
--- a/src/TinyPNG/Extensions/DownloadExtensions.cs
+++ b/src/TinyPNG/Extensions/DownloadExtensions.cs
@@ -22,7 +22,7 @@ namespace TinyPng
var compressResult = await compressResponse;
return await Download(compressResult);
-
+
}
/// <summary>
diff --git a/src/TinyPNG/Extensions/ImageDataExtensions.cs b/src/TinyPNG/Extensions/ImageDataExtensions.cs
index 8ca78bc..c2f3334 100644
--- a/src/TinyPNG/Extensions/ImageDataExtensions.cs
+++ b/src/TinyPNG/Extensions/ImageDataExtensions.cs
@@ -22,7 +22,7 @@ namespace TinyPng
/// </summary>
/// <param name="result">The result from compress</param>
/// <returns>Byte array of the image data</returns>
- public async static Task<byte[]> GetImageByteData(this TinyPngImageResponse result)
+ public async static Task<byte[]> GetImageByteData(this TinyPngImageResponse result)
{
return await result.HttpResponseMessage.Content.ReadAsByteArrayAsync();
}
@@ -43,7 +43,7 @@ namespace TinyPng
/// </summary>
/// <param name="result">The result from compress</param>
/// <returns>Stream of compressed image data</returns>
- public async static Task<Stream> GetImageStreamData(TinyPngImageResponse result)
+ public async static Task<Stream> GetImageStreamData(TinyPngImageResponse result)
{
return await result.HttpResponseMessage.Content.ReadAsStreamAsync();
}
diff --git a/tests/TinyPng.Tests/Extensions.cs b/tests/TinyPng.Tests/Extensions.cs
index f9528b9..57af93d 100644
--- a/tests/TinyPng.Tests/Extensions.cs
+++ b/tests/TinyPng.Tests/Extensions.cs
@@ -64,6 +64,18 @@ namespace TinyPng.Tests
return fakeResponse;
}
+ public static FakeResponseHandler DownloadAndFail(this FakeResponseHandler fakeResponse)
+ {
+ var outputResponseMessage = new HttpResponseMessage
+ {
+ Content = new StringContent(JsonConvert.SerializeObject(new Responses.ApiErrorResponse { Error = "Stuff's on fire yo!", Message = "This is the error message" })),
+ StatusCode = System.Net.HttpStatusCode.InternalServerError
+ };
+
+ fakeResponse.AddFakeGetResponse(new Uri("https://api.tinify.com/output"), outputResponseMessage);
+ return fakeResponse;
+ }
+
public static FakeResponseHandler Resize(this FakeResponseHandler fakeResponse)
{
var resizedCatStream = File.OpenRead(TinyPngTests.ResizedCat);
@@ -90,5 +102,18 @@ namespace TinyPng.Tests
fakeResponse.AddFakePostResponse(new Uri("https://api.tinify.com/output"), amazonMessage);
return fakeResponse;
}
+
+ public static FakeResponseHandler S3AndFail(this FakeResponseHandler fakeResponse)
+ {
+ var amazonMessage = new HttpResponseMessage
+ {
+ Content = new StringContent(JsonConvert.SerializeObject(new Responses.ApiErrorResponse { Error = "Stuff's on fire yo!", Message = "This is the error message" })),
+ StatusCode = System.Net.HttpStatusCode.BadRequest
+ };
+ //amazonMessage.Headers.Add("Location", "https://s3-ap-southeast-2.amazonaws.com/tinypng-test-bucket/path.jpg");
+
+ fakeResponse.AddFakePostResponse(new Uri("https://api.tinify.com/output"), amazonMessage);
+ return fakeResponse;
+ }
}
}
diff --git a/tests/TinyPng.Tests/TinyPngTests.cs b/tests/TinyPng.Tests/TinyPngTests.cs
index ce5e6ff..5ca2923 100644
--- a/tests/TinyPng.Tests/TinyPngTests.cs
+++ b/tests/TinyPng.Tests/TinyPngTests.cs
@@ -14,6 +14,7 @@ namespace TinyPng.Tests
internal const string Cat = "Resources/cat.jpg";
internal const string CompressedCat = "Resources/compressedcat.jpg";
internal const string ResizedCat = "Resources/resizedcat.jpg";
+ internal const string SavedCatPath = "Resources/savedcat.jpg";
[Fact]
public void TinyPngClientThrowsWhenNoApiKeySupplied()
@@ -44,6 +45,17 @@ namespace TinyPng.Tests
}
[Fact]
+ public async Task CompressionCount()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler().Compress());
+
+ var result = await pngx.Compress(Cat);
+
+ Assert.Equal(99, result.CompressionCount);
+ }
+
+ [Fact]
public async Task CompressionWithBytes()
{
var pngx = new TinyPngClient(apiKey);
@@ -94,7 +106,6 @@ namespace TinyPng.Tests
[Fact]
public async Task CompressionAndDownload()
{
-
var pngx = new TinyPngClient(apiKey);
TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
.Compress()
@@ -108,6 +119,42 @@ namespace TinyPng.Tests
}
[Fact]
+ public async Task CompressionAndDownloadAndGetUnderlyingStream()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .Download());
+
+ var downloadResult = await pngx.Compress(Cat)
+ .Download()
+ .GetImageStreamData();
+
+ Assert.Equal(16646, downloadResult.Length);
+ }
+
+ [Fact]
+ public async Task CompressionAndDownloadAndWriteToDisk()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .Download());
+ try
+ {
+ await pngx.Compress(Cat)
+ .Download()
+ .SaveImageToDisk(SavedCatPath);
+ }
+ finally
+ {
+ //try cleanup any saved file
+ File.Delete(SavedCatPath);
+ }
+
+ }
+
+ [Fact]
public async Task ResizingOperationThrows()
{
var pngx = new TinyPngClient(apiKey);
@@ -118,11 +165,41 @@ namespace TinyPng.Tests
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));
+
+ Task<Responses.TinyPngCompressResponse> nullCompressResponse = null;
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await nullCompressResponse.Resize(150, 150));
+
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 DownloadingOperationThrows()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .Download());
+
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await pngx.Compress((string)null).Download());
+
+ Task<Responses.TinyPngCompressResponse> nullCompressResponse = null;
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await nullCompressResponse.Download());
+ }
+
+ [Fact]
+ public async Task DownloadingOperationThrowsOnNonSuccessStatusCode()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .DownloadAndFail());
+
+ await Assert.ThrowsAsync<TinyPngApiException>(async () => await pngx.Compress(Cat).Download());
+
+ }
+
+ [Fact]
public async Task ResizingOperation()
{
var pngx = new TinyPngClient(apiKey);
@@ -243,6 +320,21 @@ namespace TinyPng.Tests
}
[Fact]
+ public async Task CompressAndStoreToS3FooBar()
+ {
+ var pngx = new TinyPngClient(apiKey);
+ TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler()
+ .Compress()
+ .S3AndFail());
+
+ var result = await pngx.Compress(Cat);
+
+ await Assert.ThrowsAsync<TinyPngApiException>(async () =>
+ await pngx.SaveCompressedImageToAmazonS3(result,
+ new AmazonS3Configuration(ApiKey, ApiAccessKey, "tinypng-test-bucket", "ap-southeast-2"), "path"));
+ }
+
+ [Fact]
public async Task CompressAndStoreToS3Throws()
{
var pngx = new TinyPngClient(apiKey);
@@ -253,7 +345,7 @@ namespace TinyPng.Tests
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));
}
@@ -288,5 +380,13 @@ namespace TinyPng.Tests
Assert.Equal(ErrorTitle, e.ErrorTitle);
Assert.Equal(msg, e.Message);
}
+
+ [Fact]
+ public void CallingDisposeDoesNotBlowUpTheWorld()
+ {
+ var pngx = new TinyPngClient(apiKey);
+
+ pngx.Dispose();
+ }
}
}