diff --git a/src/Mindee/Parsing/BaseLocalResponse.cs b/src/Mindee/Parsing/BaseLocalResponse.cs index bfde3cb1..8b6ec116 100644 --- a/src/Mindee/Parsing/BaseLocalResponse.cs +++ b/src/Mindee/Parsing/BaseLocalResponse.cs @@ -46,6 +46,7 @@ protected BaseLocalResponse(byte[] input) /// /// Load from a Stream. + /// This method will not close the provided stream. /// /// /// Assumes UTF-8 encoding. @@ -85,7 +86,8 @@ protected BaseLocalResponse(FileInfo input) } /// - /// Read a stream, remove line endings, transform to UTF-8 bytes. + /// Read, remove line endings, transform to UTF-8 bytes. + /// Does not close the provided Reader. /// private static byte[] ReadToCleanUtf8Bytes(TextReader reader) { diff --git a/tests/Mindee.UnitTests/V2/Parsing/LocalResponseTest.cs b/tests/Mindee.UnitTests/V2/Parsing/LocalResponseTest.cs index 9afe9b00..84e26a56 100644 --- a/tests/Mindee.UnitTests/V2/Parsing/LocalResponseTest.cs +++ b/tests/Mindee.UnitTests/V2/Parsing/LocalResponseTest.cs @@ -11,6 +11,7 @@ public class LocalResponseTest { private const string Signature = "e51bdf80f1a08ed44ee161100fc30a25cb35b4ede671b0a575dc9064a3f5dbf1"; private const string DummySecretKey = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"; + private const string FilePath = "extraction/standard_field_types.json"; private static void AssertLocalResponse(LocalResponse localResponse, string fileContent) { @@ -29,22 +30,33 @@ private static void AssertLocalResponse(LocalResponse localResponse, string file Assert.True(JsonNode.DeepEquals( JsonNode.Parse(response.RawResponse), JsonNode.Parse(fileContent))); + + Assert.Equal(fileContent.Replace("\r", "").Replace("\n", ""), localResponse.ToString()); } [Fact(DisplayName = "should load a response from a JSON string")] public void JsonString_mustLoadValidLocalResponse() { - string filePath = Path.Combine(Constants.V2ProductPath, "extraction/standard_field_types.json"); + string filePath = Path.Combine(Constants.V2ProductPath, FilePath); string fileContent = File.ReadAllText(filePath); var localResponse = new LocalResponse(fileContent); AssertLocalResponse(localResponse, fileContent); } + [Fact(DisplayName = "should load a response from a buffer")] + public void Buffer_mustLoadValidLocalResponse() + { + string filePath = Path.Combine(Constants.V2ProductPath, FilePath); + var localResponse = new LocalResponse(File.ReadAllBytes(filePath)); + + AssertLocalResponse(localResponse, File.ReadAllText(filePath)); + } + [Fact(DisplayName = "should load a response from a JSON file")] public void JsonFile_mustLoadValidLocalResponse() { - string filePath = Path.Combine(Constants.V2ProductPath, "extraction/standard_field_types.json"); + string filePath = Path.Combine(Constants.V2ProductPath, FilePath); var localResponse = new LocalResponse(new FileInfo(filePath)); AssertLocalResponse(localResponse, File.ReadAllText(filePath)); @@ -53,28 +65,18 @@ public void JsonFile_mustLoadValidLocalResponse() [Fact(DisplayName = "should load a response from a stream")] public void Stream_mustLoadValidLocalResponse() { - string filePath = Path.Combine(Constants.V2ProductPath, "extraction/standard_field_types.json"); + string filePath = Path.Combine(Constants.V2ProductPath, FilePath); using (var stream = File.OpenRead(filePath)) { var localResponse = new LocalResponse(stream); AssertLocalResponse(localResponse, File.ReadAllText(filePath)); - // make sure the stream is not closed by the LocalResponse constructor - stream.Position = 10; - var someByte = stream.ReadByte(); - Assert.NotEqual(-1, someByte); + // Explicitly verify the stream is not closed by the LocalResponse constructor + stream.Position = 0; + Assert.NotEqual(-1, stream.ReadByte()); } } - [Fact(DisplayName = "should load a response from a buffer")] - public void Buffer_mustLoadValidLocalResponse() - { - string filePath = Path.Combine(Constants.V2ProductPath, "extraction/standard_field_types.json"); - var localResponse = new LocalResponse(File.ReadAllBytes(filePath)); - - AssertLocalResponse(localResponse, File.ReadAllText(filePath)); - } - [Fact(DisplayName = "should raise an exception when given an invalid JSON string")] public void InvalidJsonString_mustRaiseException() {