Skip to content

Commit

Permalink
maint: Fixing the use of the binary formatter in net461 so all unit t…
Browse files Browse the repository at this point in the history
…ests now pass. (octokit#2535)
  • Loading branch information
JonruAlveus authored Aug 9, 2022
1 parent eaef1ee commit bc156af
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 80 deletions.
3 changes: 0 additions & 3 deletions Octokit.Tests/Clients/UsersClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
#if NET_45
using System.Collections.ObjectModel;
#endif
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Internal;
Expand Down
20 changes: 4 additions & 16 deletions Octokit.Tests/Exceptions/ApiExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
#if !NO_SERIALIZABLE
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
#endif
using NSubstitute;
using Octokit.Tests.Helpers;
using Xunit;

using static Octokit.Internal.TestSetup;
Expand Down Expand Up @@ -92,7 +89,6 @@ public void CreatesEmptyGitHubErrorWhenResponseBodyIsNull()
Assert.Equal("message2", thirdException.ApiError.Message);
}

#if !NO_SERIALIZABLE
[Fact]
public void CanPopulateObjectFromSerializedData()
{
Expand All @@ -102,19 +98,11 @@ public void CanPopulateObjectFromSerializedData()
@"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}");

var exception = new ApiException(response);
var deserialized = BinaryFormatterExtensions.SerializeAndDeserializeObject(exception);

using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, exception);
stream.Position = 0;
var deserializedObject = formatter.Deserialize(stream);
var deserialized = (ApiException)deserializedObject;
Assert.Equal("Validation Failed", deserialized.ApiError.Message);
Assert.Equal("key is already in use", exception.ApiError.Errors.First().Message);
}
Assert.Equal("Validation Failed", deserialized.ApiError.Message);
Assert.Equal("key is already in use", exception.ApiError.Errors.First().Message);
}
#endif
}

public class TheToStringMethod
Expand Down
19 changes: 4 additions & 15 deletions Octokit.Tests/Exceptions/ApiValidationExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System.Linq;
using System.Net;
#if !NO_SERIALIZABLE
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
#endif
using Xunit;

using static Octokit.Internal.TestSetup;
using Octokit.Tests.Helpers;

namespace Octokit.Tests.Exceptions
{
Expand Down Expand Up @@ -38,7 +35,6 @@ public void ProvidesDefaultMessage()
Assert.Equal("Validation Failed", exception.Message);
}

#if !NO_SERIALIZABLE
[Fact]
public void CanPopulateObjectFromSerializedData()
{
Expand All @@ -48,18 +44,11 @@ public void CanPopulateObjectFromSerializedData()
@"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}");

var exception = new ApiValidationException(response);
var deserialized = BinaryFormatterExtensions.SerializeAndDeserializeObject(exception);

using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, exception);
stream.Position = 0;
var deserialized = (ApiValidationException)formatter.Deserialize(stream);
Assert.Equal("Validation Failed", deserialized.ApiError.Message);
Assert.Equal("key is already in use", exception.ApiError.Errors.First().Message);
}
Assert.Equal("Validation Failed", deserialized.ApiError.Message);
Assert.Equal("key is already in use", exception.ApiError.Errors.First().Message);
}
#endif
}
}
}
36 changes: 12 additions & 24 deletions Octokit.Tests/Exceptions/RateLimitExceededExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Http;
#if !NO_SERIALIZABLE
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
#endif
using Xunit;

using static Octokit.Internal.TestSetup;
using Octokit.Tests.Helpers;

namespace Octokit.Tests.Exceptions
{
Expand Down Expand Up @@ -86,7 +82,6 @@ public void HandlesMissingHeaderValues()
Assert.Equal(TimeSpan.Zero, exception.GetRetryAfterTimeSpan());
}

#if !NO_SERIALIZABLE
[Fact]
public void CanPopulateObjectFromSerializedData()
{
Expand All @@ -99,24 +94,17 @@ public void CanPopulateObjectFromSerializedData()

var exception = new RateLimitExceededException(response);

using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, exception);
stream.Position = 0;
var deserialized = (RateLimitExceededException)formatter.Deserialize(stream);

Assert.Equal(HttpStatusCode.Forbidden, deserialized.StatusCode);
Assert.Equal(100, deserialized.Limit);
Assert.Equal(42, deserialized.Remaining);
var expectedReset = DateTimeOffset.ParseExact(
"Mon 01 Jul 2013 5:47:53 PM -00:00",
"ddd dd MMM yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Assert.Equal(expectedReset, deserialized.Reset);
}
var deserialized = BinaryFormatterExtensions.SerializeAndDeserializeObject(exception);

Assert.Equal(HttpStatusCode.Forbidden, deserialized.StatusCode);
Assert.Equal(100, deserialized.Limit);
Assert.Equal(42, deserialized.Remaining);
var expectedReset = DateTimeOffset.ParseExact(
"Mon 01 Jul 2013 5:47:53 PM -00:00",
"ddd dd MMM yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Assert.Equal(expectedReset, deserialized.Reset);
}
#endif
}

public class GetRetryAfterTimeSpanMethod
Expand Down Expand Up @@ -153,7 +141,7 @@ public void ReturnsZeroIfSkewedResetInPast()
{
var beginTime = DateTimeOffset.Now;
var resetTime = beginTime - TimeSpan.FromHours(1);

var response = CreateResponse(HttpStatusCode.Forbidden,
new Dictionary<string, string>
{
Expand Down
34 changes: 34 additions & 0 deletions Octokit.Tests/Helpers/BindaryFormatterHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Octokit.Tests.Helpers
{
public class BinaryFormatterExtensions
{
public static T SerializeAndDeserializeObject<T>(T input)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, input);
stream.Position = 0;
formatter.Binder = new SerializationBinderHelper();
var deserializedObject = formatter.Deserialize(stream);
var deserialized = (T)deserializedObject;
return deserialized;
}
}

internal class SerializationBinderHelper : SerializationBinder
{
public string Name { get; set; }

public override Type BindToType(string i_AssemblyName, string i_TypeName)
{
Type typeToDeserialize = Type.GetType(String.Format(" {0}, {1}", i_TypeName, i_AssemblyName)); return typeToDeserialize;
}
}
}
}
33 changes: 11 additions & 22 deletions Octokit.Tests/Http/RateLimitTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using Octokit.Tests.Helpers;
using System;
using System.Collections.Generic;
using System.Globalization;
#if !NO_SERIALIZABLE
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
#endif
using Xunit;

namespace Octokit.Tests.Http
Expand Down Expand Up @@ -71,7 +68,6 @@ public void HandlesMissingHeaderValues()
Assert.Equal(expectedReset, rateLimit.Reset);
}

#if !NO_SERIALIZABLE
[Fact]
public void CanPopulateObjectFromSerializedData()
{
Expand All @@ -83,24 +79,17 @@ public void CanPopulateObjectFromSerializedData()
};

var rateLimit = new RateLimit(headers);
var deserialized = BinaryFormatterExtensions.SerializeAndDeserializeObject(rateLimit);

using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, rateLimit);
stream.Position = 0;
var deserialized = (RateLimit)formatter.Deserialize(stream);

Assert.Equal(100, deserialized.Limit);
Assert.Equal(42, deserialized.Remaining);
var expectedReset = DateTimeOffset.ParseExact(
"Mon 01 Jul 2013 5:47:53 PM -00:00",
"ddd dd MMM yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Assert.Equal(expectedReset, deserialized.Reset);
}
Assert.Equal(100, deserialized.Limit);
Assert.Equal(42, deserialized.Remaining);
var expectedReset = DateTimeOffset.ParseExact(
"Mon 01 Jul 2013 5:47:53 PM -00:00",
"ddd dd MMM yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Assert.Equal(expectedReset, deserialized.Reset);
}
#endif

[Fact]
public void EnsuresHeadersNotNull()
{
Expand Down

0 comments on commit bc156af

Please sign in to comment.