diff options
author | Chad Tolkien <chad@tolkien.id.au> | 2017-05-08 20:08:28 +1000 |
---|---|---|
committer | Chad Tolkien <chad@tolkien.id.au> | 2017-05-08 20:08:28 +1000 |
commit | ca7b662947bfa71048f5e2989a15da1584ab5a27 (patch) | |
tree | 7a2eceb1bee1aff0301968591526076b14c19312 | |
parent | df65bab457dbfaa91b5898c0282293a01128da29 (diff) | |
download | TinyPNG-ca7b662947bfa71048f5e2989a15da1584ab5a27.zip TinyPNG-ca7b662947bfa71048f5e2989a15da1584ab5a27.tar.gz TinyPNG-ca7b662947bfa71048f5e2989a15da1584ab5a27.tar.bz2 |
fig bug with disposed httpclient, update appveyor
-rw-r--r-- | appveyor.yml | 38 | ||||
-rw-r--r-- | src/TinyPNG/TinyPNG.csproj | 2 | ||||
-rw-r--r-- | src/TinyPNG/TinyPng.cs | 28 | ||||
-rw-r--r-- | tests/TinyPng.Tests/TinyPngTests.cs | 28 |
4 files changed, 75 insertions, 21 deletions
diff --git a/appveyor.yml b/appveyor.yml index 514bcaf..1e8d940 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,5 +1,5 @@ os: Visual Studio 2017
-version: 3.0.{build}
+version: 3.1.{build}
configuration: Release
environment:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
@@ -7,22 +7,29 @@ environment: pull_requests:
do_not_increment_build_number: true
init:
-- ps: $Env:LABEL = $Env:APPVEYOR_BUILD_NUMBER
-- ps: iex ((new-object net.webclient).DownloadString('https://gist.githubusercontent.com/PureKrome/0f79e25693d574807939/raw/f5b40256fc2ca77d49f1c7773d28406152544c1e/appveyor-build-info.ps'))
-build:
- verbosity: minimal
- publish_nuget: true
+- ps: |
+ if ($env:APPVEYOR_REPO_TAG -eq $true -and $env:APPVEYOR_REPO_TAG_NAME -match "^v\d+\.\d+\.\d+(-(?<prerelease>[\w\d]+))?$")
+ {
+ $env:TaggedForRelease = $true
+ $env:TaggedForPreRelease = $matches.prerelease -ne $null
+ $relName = if ($env:TaggedForPreRelease) { "PreRelease" } else { "Release" }
+ Add-AppveyorMessage "Tagged For $($relName): $($env:APPVEYOR_REPO_TAG_NAME)" -details $env:APPVEYOR_REPO_COMMIT
+ Update-AppveyorBuild -Version "$env:APPVEYOR_REPO_TAG_NAME".Substring(1)
+ }
+before_build:
+- dotnet --info
+- dotnet restore
build_script:
-- dotnet restore -v Minimal
- dotnet build -c %CONFIGURATION%
-- dotnet pack -c %CONFIGURATION% .\src\TinyPng\TinyPNG.csproj -o .\artifacts
+after_build:
+- dotnet pack -c %CONFIGURATION% .\src\TinyPng\TinyPNG.csproj -o .\artifacts
artifacts:
- path: .\src\TinyPng\artifacts\**\*.*
before_test:
- cmd: choco install opencover.portable
test_script:
- cmd: pushd
-- cmd: cd .\tests\TinyPng.Tests\
+- cmd: cd .\tests\TinyPng.Tests\
- cmd: opencover.console -target:"dotnet.exe" -targetargs:"test -c %CONFIGURATION% --no-build" -register:user -filter:"+[*]* -[xunit*]*" -output:"../../coverage.xml" -log:All -oldstyle -skipautoprops
after_test:
- "SET PATH=C:\\Python34;C:\\Python34\\Scripts;%PATH%"
@@ -36,4 +43,15 @@ deploy: skip_symbols: false
artifact: /.*\.nupkg/
on:
- branch: release
+ TaggedForRelease: true
+ TaggedForPreRelease : false
+
+- provider: GitHub
+ release: $(APPVEYOR_REPO_TAG_NAME)
+ prerelease: $(TaggedForPreRelease)
+ description: 'Release of $(APPVEYOR_REPO_TAG_NAME)'
+ auth_token:
+ secure: uLTEBGoagOuXDvbSbUqHe2k/7Oyk4lttpWKHBn4mRdSiZP6aCB9MMybins2dQLCZ
+ artifact: /.*\.nupkg/
+ on:
+ TaggedForRelease: true
diff --git a/src/TinyPNG/TinyPNG.csproj b/src/TinyPNG/TinyPNG.csproj index 389c146..0bf548f 100644 --- a/src/TinyPNG/TinyPNG.csproj +++ b/src/TinyPNG/TinyPNG.csproj @@ -3,7 +3,7 @@ <PropertyGroup>
<Copyright>Copyright 2016 Chad Tolkien</Copyright>
<AssemblyTitle>TinyPNG</AssemblyTitle>
- <VersionPrefix>3.0.0</VersionPrefix>
+ <VersionPrefix>3.1.0</VersionPrefix>
<Authors>Chad Tolkien</Authors>
<TargetFrameworks>net45;netstandard1.3</TargetFrameworks>
<AssemblyName>TinyPNG</AssemblyName>
diff --git a/src/TinyPNG/TinyPng.cs b/src/TinyPNG/TinyPng.cs index bfd06ef..927bf20 100644 --- a/src/TinyPNG/TinyPng.cs +++ b/src/TinyPNG/TinyPng.cs @@ -14,10 +14,9 @@ namespace TinyPng {
public class TinyPngClient : IDisposable
{
- private readonly string _apiKey;
private const string ApiEndpoint = "https://api.tinify.com/shrink";
- internal static HttpClient HttpClient = new HttpClient();
+ internal static HttpClient HttpClient;
internal static JsonSerializerSettings JsonSettings;
/// <summary>
@@ -29,13 +28,7 @@ namespace TinyPng if (string.IsNullOrEmpty(apiKey))
throw new ArgumentNullException(nameof(apiKey));
- //configure basic auth api key formatting.
- var auth = $"api:{apiKey}";
- var authByteArray = System.Text.Encoding.ASCII.GetBytes(auth);
- _apiKey = Convert.ToBase64String(authByteArray);
-
- //add auth to the default outgoing headers.
- HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", _apiKey);
+ ConfigureHttpClient(apiKey);
//configure json settings for camelCase.
JsonSettings = new JsonSerializerSettings
@@ -46,6 +39,22 @@ namespace TinyPng }
+ private static void ConfigureHttpClient(string apiKey)
+ {
+ //configure basic auth api key formatting.
+ var auth = $"api:{apiKey}";
+ var authByteArray = System.Text.Encoding.ASCII.GetBytes(auth);
+ var apiKeyEncoded = Convert.ToBase64String(authByteArray);
+
+ if (HttpClient == null)
+ {
+ HttpClient = new HttpClient();
+ }
+
+ //add auth to the default outgoing headers.
+ HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", apiKeyEncoded);
+ }
+
/// <summary>
/// Wrapper for the tinypng.com API
/// </summary>
@@ -202,6 +211,7 @@ namespace TinyPng if (disposing)
{
HttpClient?.Dispose();
+ HttpClient = null;
}
}
#endregion
diff --git a/tests/TinyPng.Tests/TinyPngTests.cs b/tests/TinyPng.Tests/TinyPngTests.cs index 5ca2923..703e826 100644 --- a/tests/TinyPng.Tests/TinyPngTests.cs +++ b/tests/TinyPng.Tests/TinyPngTests.cs @@ -45,6 +45,32 @@ namespace TinyPng.Tests }
[Fact]
+ public async Task CanBeCalledMultipleTimesWihtoutExploding()
+ {
+ using (var pngx = new TinyPngClient(apiKey))
+ {
+ //TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler().Compress());
+
+ var result = await pngx.Compress(Cat);
+
+ Assert.Equal("image/jpeg", result.Input.Type);
+ Assert.Equal(400, result.Output.Width);
+ Assert.Equal(400, result.Output.Height);
+ }
+
+ using (var pngx = new TinyPngClient(apiKey))
+ {
+ //TinyPngClient.HttpClient = new HttpClient(new FakeResponseHandler().Compress());
+
+ var result = await pngx.Compress(Cat);
+
+ Assert.Equal("image/jpeg", result.Input.Type);
+ Assert.Equal(400, result.Output.Width);
+ Assert.Equal(400, result.Output.Height);
+ }
+ }
+
+ [Fact]
public async Task CompressionCount()
{
var pngx = new TinyPngClient(apiKey);
@@ -281,7 +307,7 @@ namespace TinyPng.Tests [Fact]
public void CompressAndStoreToS3ShouldThrowIfNoApiKeyProvided()
{
- Assert.Throws<ArgumentNullException>(() => new TinyPngClient(string.Empty, new AmazonS3Configuration("a", "b", "c", "d")));
+ Assert.Throws<ArgumentNullException>(() => new TinyPngClient(string.Empty, new AmazonS3Configuration("a", "b", "c", "d")));
}
[Fact]
|