Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Mindee/Parsing/BaseLocalResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected BaseLocalResponse(byte[] input)

/// <summary>
/// Load from a Stream.
/// This method will not close the provided stream.
/// </summary>
/// <param name="input">
/// Assumes UTF-8 encoding.
Expand Down Expand Up @@ -85,7 +86,8 @@ protected BaseLocalResponse(FileInfo input)
}

/// <summary>
/// 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.
/// </summary>
private static byte[] ReadToCleanUtf8Bytes(TextReader reader)
{
Expand Down
34 changes: 18 additions & 16 deletions tests/Mindee.UnitTests/V2/Parsing/LocalResponseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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));
Expand All @@ -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()
{
Expand Down
Loading