Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

changed tests so that the method of decoding the byte array is throug…

…h StreamReader instead of Encoding*.GetBytes(*)
  • Loading branch information...
commit c9b3655c0af84581b1ae1c1a9ae1232c7550806e 1 parent 01808a1
Ron Klein authored
4 src/Nancy.Testing/BrowserResponseBodyWrapperExtensions.cs
View
@@ -1,3 +1,5 @@
+using Nancy.Extensions;
+
namespace Nancy.Testing
{
using System;
@@ -30,7 +32,7 @@ public static Stream AsStream(this BrowserResponseBodyWrapper bodyWrapper)
/// <value>A string containing the HTTP response body.</value>
public static string AsString(this BrowserResponseBodyWrapper bodyWrapper)
{
- return Encoding.UTF8.GetString(bodyWrapper.ToArray());
+ return bodyWrapper.ToArray().DecodeAsUtf8();
}
/// <summary>
8 src/Nancy.Tests/Unit/JsonFormatterExtensionsFixtures.cs
View
@@ -1,3 +1,5 @@
+using Nancy.Extensions;
+
namespace Nancy.Tests.Unit
{
using System;
@@ -41,7 +43,7 @@ public void Should_return_a_valid_model_in_json_format()
{
response.Contents(stream);
- Encoding.UTF8.GetString(stream.ToArray()).ShouldEqual("{\"FirstName\":\"Andy\",\"LastName\":\"Pike\"}");
+ stream.ToArray().DecodeAsUtf8().ShouldEqual("{\"FirstName\":\"Andy\",\"LastName\":\"Pike\"}");
}
}
@@ -52,7 +54,7 @@ public void Should_return_null_in_json_format()
using (var stream = new MemoryStream())
{
nullResponse.Contents(stream);
- Encoding.UTF8.GetString(stream.ToArray()).ShouldEqual("null");
+ stream.ToArray().DecodeAsUtf8().ShouldEqual("null");
}
}
@@ -63,7 +65,7 @@ public void Json_formatter_can_deserialize_objects_of_type_Type()
using (var stream = new MemoryStream())
{
response.Contents(stream);
- Encoding.UTF8.GetString(stream.ToArray()).ShouldEqual(@"{""type"":""System.String""}");
+ stream.ToArray().DecodeAsUtf8().ShouldEqual(@"{""type"":""System.String""}");
}
}
20 src/Nancy/Extensions/ByteArrayExtensions.cs
View
@@ -0,0 +1,20 @@
+using System.IO;
+using System.Text;
+
+namespace Nancy.Extensions
+{
+ public static class ByteArrayExtensions
+ {
+ public static string DecodeAsUtf8(this byte[] source)
+ {
+ return Decode(source, Encoding.UTF8);
+ }
+
+ public static string Decode(this byte[] source, Encoding encoding)
+ {
+ using (var memoryStream = new MemoryStream(source))
+ using (var sr = new StreamReader(memoryStream, encoding))
+ return sr.ReadToEnd();
+ }
+ }
+}
1  src/Nancy/Nancy.csproj
View
@@ -106,6 +106,7 @@
<Compile Include="Diagnostics\DiagnosticsConfiguration.cs" />
<Compile Include="Diagnostics\DiagnosticsSession.cs" />
<Compile Include="Diagnostics\ConcurrentLimitedCollection.cs" />
+ <Compile Include="Extensions\ByteArrayExtensions.cs" />
<Compile Include="Extensions\TypeExtensions.cs" />
<Compile Include="Diagnostics\DefaultRequestTracing.cs" />
<Compile Include="Diagnostics\DescriptionAttribute.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.