Skip to content

Commit

Permalink
Merge pull request #53 from Petteroe/feature/add-stream-response
Browse files Browse the repository at this point in the history
Feature/add stream response
  • Loading branch information
berthertogen authored Mar 2, 2024
2 parents 812c8c0 + 57d16dc commit 676343b
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 72 deletions.
2 changes: 1 addition & 1 deletion build-on-linux.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
workingDir="./standalone-linux-64";
assets="./assets";
version=weasyprint==60
version=weasyprint==61

if [ -d "$workingDir" ]; then
echo "*** Cleaning $workingDir"
Expand Down
2 changes: 1 addition & 1 deletion build-on-windows.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$workingDir = "./standalone-windows-64";
$assets = "./assets";
$version = "weasyprint==60"
$version = "weasyprint==61"

if (Test-Path $workingDir) {
Write-Host "*** Cleaning $workingDir"
Expand Down
85 changes: 76 additions & 9 deletions src/Weasyprint.Wrapped.Tests/Tests/PrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Weasyprint.Wrapped.Tests;
[Collection("Integration")]
public class PrinterTests
{
private readonly string testingProjectRoot = new DirectoryInfo(AppContext.BaseDirectory).Parent.Parent.Parent.FullName;

public PrinterTests()
{
if (Directory.Exists("./weasyprinter"))
Expand Down Expand Up @@ -88,20 +90,40 @@ public async Task Print_RunsCommand_Simple()
Assert.Equal(0, result.ExitCode);
Assert.False(result.HasError);

var testingProjectRoot = new DirectoryInfo(AppContext.BaseDirectory).Parent.Parent.Parent.FullName;
var filename = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Print_RunsCommand_Result_Windows_Expected.pdf" : "Print_RunsCommand_Result_Linux_Expected.pdf";
var expectedOutputBytes = File.ReadAllBytes(Path.Combine(testingProjectRoot, $"Expected/{filename}"));
File.WriteAllBytes(Path.Combine(testingProjectRoot, "Expected/Print_RunsCommand_Result_Actual.pdf"), result.Bytes);
Assert.True(result.Bytes.Length > 0);
}

[Fact]
public async Task Print_RunsStreamCommand_Simple()
{
var printer = GetPrinter();
await printer.Initialize();

var result = await printer.PrintStream("<html><body><h1>TEST</h1></body></html>");

var actualOutputBytes = (result.DocumentStream as MemoryStream)?.ToArray();

Assert.NotNull(actualOutputBytes);
Assert.True(actualOutputBytes.Length > 0);
Assert.True(string.IsNullOrWhiteSpace(result.Error), $"Should have no error but found {result.Error}");
Assert.Equal(0, result.ExitCode);
Assert.False(result.HasError);

var filename = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Print_RunsCommand_Result_Windows_Expected.pdf" : "Print_RunsCommand_Result_Linux_Expected.pdf";
var expectedOutputBytes = File.ReadAllBytes(Path.Combine(testingProjectRoot, $"Expected/{filename}"));

Assert.True(actualOutputBytes.Length > 0);
}

[Fact]
public async Task Print_RunsCommand_WithFilePaths_Simple()
{
var printer = GetPrinter();
await printer.Initialize();

var testingProjectRoot = new DirectoryInfo(AppContext.BaseDirectory).Parent.Parent.Parent.FullName;
var inputFile = Path.Combine(testingProjectRoot,"Expected/Print_RunsCommand_Simple_Input.html");
var outputFile = Path.Combine(testingProjectRoot, "Expected/Print_RunsCommand_WithFilePaths_Result_Actual.pdf");
var result = await printer.Print(inputFile, outputFile, CancellationToken.None);
Expand All @@ -110,7 +132,7 @@ public async Task Print_RunsCommand_WithFilePaths_Simple()
Assert.Equal(0, result.ExitCode);
Assert.False(result.HasError);

var outputFileBytes = File.ReadAllBytes(outputFile);
var outputFileBytes = await File.ReadAllBytesAsync(outputFile);
Assert.True(outputFileBytes.Length > 0);
}

Expand All @@ -120,8 +142,7 @@ public async Task Print_RunsCommand_WithParameters()
var printer = GetPrinter();
await printer.Initialize();

var testingProjectRoot = new DirectoryInfo(AppContext.BaseDirectory).Parent.Parent.Parent.FullName;
var html = File.ReadAllText(Path.Combine(testingProjectRoot,"Expected/Print_RunsCommand_SpecialCharacters_Input.html"), System.Text.Encoding.UTF8);
var html = await File.ReadAllTextAsync(Path.Combine(testingProjectRoot,"Expected/Print_RunsCommand_SpecialCharacters_Input.html"), System.Text.Encoding.UTF8);
var resultNormal = await printer.Print(html);
Assert.True(string.IsNullOrWhiteSpace(resultNormal.Error), $"Should have no error but found {resultNormal.Error}");
Assert.Equal(0, resultNormal.ExitCode);
Expand All @@ -133,24 +154,70 @@ public async Task Print_RunsCommand_WithParameters()

Assert.True(resultNormal.Bytes.Length > resultOptimized.Bytes.Length, $"Expected {resultNormal.Bytes.Length} to be greater than {resultOptimized.Bytes.Length}");
}

[Fact]
public async Task Print_RunsStreamCommand_WithParameters()
{
var printer = GetPrinter();
await printer.Initialize();

var html = await File.ReadAllTextAsync(Path.Combine(testingProjectRoot,"Expected/Print_RunsCommand_SpecialCharacters_Input.html"), System.Text.Encoding.UTF8);

var resultNormal = await printer.PrintStream(html);

Assert.NotNull(resultNormal.DocumentStream);
Assert.True(resultNormal.DocumentStream.Length > 0);
Assert.True(string.IsNullOrWhiteSpace(resultNormal.Error), $"Should have no error but found {resultNormal.Error}");
Assert.Equal(0, resultNormal.ExitCode);
Assert.False(resultNormal.HasError);

var resultOptimized = await printer.PrintStream(html, cancellationToken: default, "--optimize-images");

Assert.NotNull(resultOptimized.DocumentStream);
Assert.True(resultOptimized.DocumentStream.Length > 0);
Assert.True(string.IsNullOrWhiteSpace(resultOptimized.Error), $"Should have no error but found {resultOptimized.Error}");
Assert.Equal(0, resultOptimized.ExitCode);
Assert.False(resultOptimized.HasError);

Assert.True(resultNormal.DocumentStream.Length > resultOptimized.DocumentStream.Length, $"Expected {resultNormal.DocumentStream.Length} to be greater than {resultOptimized.DocumentStream.Length}");
}

[Fact]
public async Task Print_RunsCommand_SpecialCharacters()
{
var printer = GetPrinter();
await printer.Initialize();

var testingProjectRoot = new DirectoryInfo(AppContext.BaseDirectory).Parent.Parent.Parent.FullName;
var html = File.ReadAllText(Path.Combine(testingProjectRoot,"Expected/Print_RunsCommand_SpecialCharacters_Input.html"), System.Text.Encoding.UTF8);
var html = await File.ReadAllTextAsync(Path.Combine(testingProjectRoot,"Expected/Print_RunsCommand_SpecialCharacters_Input.html"), System.Text.Encoding.UTF8);
var result = await printer.Print(html);

File.WriteAllBytes(Path.Combine(testingProjectRoot, "Expected/Print_RunsCommand_SpecialCharacters_Output.pdf"), result.Bytes);
await File.WriteAllBytesAsync(Path.Combine(testingProjectRoot, "Expected/Print_RunsCommand_SpecialCharacters_Output.pdf"), result.Bytes);

Assert.True(string.IsNullOrWhiteSpace(result.Error), $"Should have no error but found {result.Error}");
Assert.Equal(0, result.ExitCode);
Assert.False(result.HasError);
Assert.True(result.Bytes.Length > 0);
}

[Fact]
public async Task Print_RunsStreamCommand_SpecialCharacters()
{
var printer = GetPrinter();
await printer.Initialize();

var html = await File.ReadAllTextAsync(Path.Combine(testingProjectRoot,"Expected/Print_RunsCommand_SpecialCharacters_Input.html"), System.Text.Encoding.UTF8);
var result = await printer.PrintStream(html);

var actualBytes = (result.DocumentStream as MemoryStream)?.ToArray();
var expectedBytes = await File.ReadAllBytesAsync(Path.Combine(testingProjectRoot, "Expected/Print_RunsCommand_SpecialCharacters_Output.pdf"));

Assert.True(string.IsNullOrWhiteSpace(result.Error), $"Should have no error but found {result.Error}");
Assert.Equal(0, result.ExitCode);
Assert.False(result.HasError);
Assert.True(result.DocumentStream.Length > 0);
Assert.True(actualBytes?.Length > 0);

}

[Fact]
public async Task Version_ReturnsVersion()
Expand All @@ -160,7 +227,7 @@ public async Task Version_ReturnsVersion()

var result = await printer.Version();

Assert.Contains("WeasyPrint version: 60.0", result.Version);
Assert.Contains("WeasyPrint version: 61.0", result.Version);
Assert.Equal(0, result.ExitCode);
Assert.False(result.HasError);
}
Expand Down
18 changes: 18 additions & 0 deletions src/Weasyprint.Wrapped/PrintBaseResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

namespace Weasyprint.Wrapped;

public abstract class PrintBaseResult
{
public PrintBaseResult(string error, TimeSpan runTime, int exitCode)
{
this.Error = error;
this.RunTime = runTime;
this.ExitCode = exitCode;
}

public bool HasError => !string.IsNullOrWhiteSpace(Error);

public string Error { get; }
public TimeSpan RunTime { get; }
public int ExitCode { get; }
}
11 changes: 2 additions & 9 deletions src/Weasyprint.Wrapped/PrintResult.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@

namespace Weasyprint.Wrapped;

public class PrintResult
public class PrintResult : PrintBaseResult
{
public PrintResult(byte[] bytes, string error, TimeSpan runTime, int exitCode)
: base(error, runTime, exitCode)
{
this.Bytes = bytes;
this.Error = error;
this.RunTime = runTime;
this.ExitCode = exitCode;
}

public bool HasError => !string.IsNullOrWhiteSpace(Error);

public byte[] Bytes { get; }
public string Error { get; }
public TimeSpan RunTime { get; }
public int ExitCode { get; }
}
13 changes: 13 additions & 0 deletions src/Weasyprint.Wrapped/PrintStreamResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

namespace Weasyprint.Wrapped;

public class PrintStreamResult : PrintBaseResult
{
public PrintStreamResult(Stream documentDocumentStream, string error, TimeSpan runTime, int exitCode)
: base(error, runTime, exitCode)
{
this.DocumentStream = documentDocumentStream;
}

public Stream DocumentStream { get; }
}
Loading

0 comments on commit 676343b

Please sign in to comment.