diff --git a/README.md b/README.md index a14530e..46fddd6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![PkgGoDev](https://pkg.go.dev/badge/github.com/SebastiaanKlippert/go-wkhtmltopdf)](https://pkg.go.dev/github.com/SebastiaanKlippert/go-wkhtmltopdf) -[![Build Status](https://travis-ci.org/SebastiaanKlippert/go-wkhtmltopdf.svg?branch=master)](https://travis-ci.org/SebastiaanKlippert/go-wkhtmltopdf) +[![Build Status](https://travis-ci.com/SebastiaanKlippert/go-wkhtmltopdf.svg?branch=master)](https://travis-ci.com/SebastiaanKlippert/go-wkhtmltopdf) [![Go Report Card](https://goreportcard.com/badge/SebastiaanKlippert/go-wkhtmltopdf)](https://goreportcard.com/report/SebastiaanKlippert/go-wkhtmltopdf) [![codebeat badge](https://codebeat.co/badges/a6bb7f66-7ae2-4de8-8b61-623ef68096c9)](https://codebeat.co/projects/github-com-sebastiaanklippert-go-wkhtmltopdf-master) [![codecov](https://codecov.io/gh/SebastiaanKlippert/go-wkhtmltopdf/branch/master/graph/badge.svg)](https://codecov.io/gh/SebastiaanKlippert/go-wkhtmltopdf) @@ -125,7 +125,7 @@ Use `NewPDFPreparer` to create a PDF Generator object on the client and `NewPDFG ```go // Client code pdfg := NewPDFPreparer() -htmlfile, err := ioutil.ReadFile("./testfiles/htmlsimple.html") +htmlfile, err := ioutil.ReadFile("testdata/htmlsimple.html") if err != nil { log.Fatal(err) } diff --git a/go.mod b/go.mod index 6922631..a9da1d4 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/SebastiaanKlippert/go-wkhtmltopdf go 1.12 + +require github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..acb88a4 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/json_test.go b/json_test.go index c77fa84..d4063f0 100644 --- a/json_test.go +++ b/json_test.go @@ -7,13 +7,15 @@ import ( "os" "reflect" "testing" + + "github.com/stretchr/testify/assert" ) func TestPDFGenerator_ToJSON(t *testing.T) { pdfg := newTestPDFGenerator(t) // add a reader page as well - htmlfile, err := os.Open("./testfiles/htmlsimple.html") + htmlfile, err := os.Open("testdata/htmlsimple.html") if err != nil { t.Fatal(err) } @@ -26,15 +28,12 @@ func TestPDFGenerator_ToJSON(t *testing.T) { t.Fatal(err) } - l := 16810 - if len(jb) != l { - t.Errorf("Want %d JSON bytes, have %d", l, len(jb)) + expected, err := ioutil.ReadFile("testdata/expected.json") + if err != nil { + t.Fatal(err) } - want := `{"GlobalOptions":{"CookieJar":{"Option":"cookie-jar","Value":""},"Copies":{"Option":"copies","IsSet":false,"Value":0},"Dpi":{"Option":"dpi","IsSet":true,"Value":600},"ExtendedHelp":{"Option":"extended-help","Value":false},"Grayscale":{"Option":"grayscale","Value":false},"Help":{"Option":"help","Value":false},"HTMLDoc":{"Option":"htmldoc","Value":false},"ImageDpi":{"Option":"image-dpi","IsSet":false,"Value":0},"ImageQuality":{"Option":"image-quality","IsSet":false,"Value":0},"License":{"Option":"license","Value":false},"LogLevel":{"Option":"log-level","Value":""},"LowQuality":{"Option":"lowquality","Value":false},"ManPage":{"Option":"manpage","Value":false},"MarginBottom":{"Option":"margin-bottom","IsSet":true,"Value":40},"MarginLeft":{"Option":"margin-left","IsSet":true,"Value":0},"MarginRight":{"Option":"margin-right","IsSet":false,"Value":0},"MarginTop":{"Option":"margin-top","IsSet":false,"Value":0},"NoCollate":{"Option":"no-collate","Value":false},"NoPdfCompression":{"Option":"no-pdf-compression","Value":false},"Orientation":{"Option":"orientation","Value":""},"PageHeight":{"Option":"page-height","IsSet":false,"Value":0},"PageSize":{"Option":"page-size","Value":"A4"},"PageWidth":{"Option":"page-width","IsSet":false,"Value":0},"Quiet":{"Option":"quiet","Value":false},"ReadArgsFromStdin":{"Option":"read-args-from-stdin","Value":false},"Readme":{"Option":"readme","Value":false},"Title":{"Option":"title","Value":""},"Version":{"Option":"version","Value":false}},"OutlineOptions":{"DumpDefaultTocXsl":{"Option":"dump-default-toc-xsl","Value":false},"DumpOutline":{"Option":"dump-outline","Value":""},"NoOutline":{"Option":"no-outline","Value":false},"OutlineDepth":{"Option":"outline-depth","IsSet":false,"Value":0}},"Cover":{"Input":"https://wkhtmltopdf.org/index.html","Allow":{"Option":"allow","Value":null},"BypassProxyFor":{"Option":"bypass-proxy-for","Value":null},"CacheDir":{"Option":"cache-dir","Value":""},"CheckboxCheckedSvg":{"Option":"checkbox-checked-svg","Value":""},"CheckboxSvg":{"Option":"checkbox-svg","Value":""},"Cookie":{"Option":"cookie","Value":null},"CustomHeader":{"Option":"custom-header","Value":null},"CustomHeaderPropagation":{"Option":"custom-header-propagation","Value":false},"DebugJavascript":{"Option":"debug-javascript","Value":false},"DefaultHeader":{"Option":"default-header","Value":false},"DisableExternalLinks":{"Option":"disable-external-links","Value":false},"DisableInternalLinks":{"Option":"disable-internal-links","Value":false},"DisableJavascript":{"Option":"disable-javascript","Value":false},"DisableLocalFileAccess":{"Option":"disable-local-file-access","Value":false},"DisableSmartShrinking":{"Option":"disable-smart-shrinking","Value":false},"EnableForms":{"Option":"enable-forms","Value":false},"EnableLocalFileAccess":{"Option":"enable-local-file-access","Value":false},"EnablePlugins":{"Option":"enable-plugins","Value":false},"EnableTocBackLinks":{"Option":"enable-toc-back-links","Value":false},"Encoding":{"Option":"encoding","Value":""},"ExcludeFromOutline":{"Option":"exclude-from-outline","Value":false},"JavascriptDelay":{"Option":"javascript-delay","IsSet":false,"Value":0},"KeepRelativeLinks":{"Option":"keep-relative-links","Value":false},"LoadErrorHandling":{"Option":"load-error-handling","Value":""},"LoadMediaErrorHandling":{"Option":"load-media-error-handling","Value":""},"MinimumFontSize":{"Option":"minimum-font-size","IsSet":false,"Value":0},"NoBackground":{"Option":"no-background","Value":false},"NoCustomHeaderPropagation":{"Option":"no-custom-header-propagation","Value":false},"NoImages":{"Option":"no-images","Value":false},"NoStopSlowScripts":{"Option":"no-stop-slow-scripts","Value":false},"PageOffset":{"Option":"page-offset","IsSet":false,"Value":0},"Password":{"Option":"password","Value":""},"Post":{"Option":"post","Value":null},"PostFile":{"Option":"post-file","Value":null},"PrintMediaType":{"Option":"print-media-type","Value":false},"Proxy":{"Option":"proxy","Value":""},"ProxyHostnameLookup":{"Option":"proxy-hostname-lookup","Value":false},"RadiobuttonCheckedSvg":{"Option":"radiobutton-checked-svg","Value":""},"RadiobuttonSvg":{"Option":"radiobutton-svg","Value":""},"RunScript":{"Option":"run-script","Value":null},"SslCrtPath":{"Option":"ssl-crt-path","Value":""},"SslKeyPassword":{"Option":"ssl-key-password","Value":""},"SslKeyPath":{"Option":"ssl-key-path","Value":""},"Username":{"Option":"username","Value":""},"UserStyleSheet":{"Option":"user-style-sheet","Value":""},"ViewportSize":{"Option":"viewport-size","Value":""},"WindowStatus":{"Option":"window-status","Value":""},"Zoom":{"Option":"zoom","IsSet":true,"Value":0.75}},"TOC":{"Include":true,"Allow":{"Option":"allow","Value":null},"BypassProxyFor":{"Option":"bypass-proxy-for","Value":null},"CacheDir":{"Option":"cache-dir","Value":""},"CheckboxCheckedSvg":{"Option":"checkbox-checked-svg","Value":""},"CheckboxSvg":{"Option":"checkbox-svg","Value":""},"Cookie":{"Option":"cookie","Value":null},"CustomHeader":{"Option":"custom-header","Value":null},"CustomHeaderPropagation":{"Option":"custom-header-propagation","Value":false},"DebugJavascript":{"Option":"debug-javascript","Value":false},"DefaultHeader":{"Option":"default-header","Value":false},"DisableExternalLinks":{"Option":"disable-external-links","Value":false},"DisableInternalLinks":{"Option":"disable-internal-links","Value":false},"DisableJavascript":{"Option":"disable-javascript","Value":false},"DisableLocalFileAccess":{"Option":"disable-local-file-access","Value":false},"DisableSmartShrinking":{"Option":"disable-smart-shrinking","Value":false},"EnableForms":{"Option":"enable-forms","Value":false},"EnableLocalFileAccess":{"Option":"enable-local-file-access","Value":false},"EnablePlugins":{"Option":"enable-plugins","Value":false},"EnableTocBackLinks":{"Option":"enable-toc-back-links","Value":false},"Encoding":{"Option":"encoding","Value":""},"ExcludeFromOutline":{"Option":"exclude-from-outline","Value":false},"JavascriptDelay":{"Option":"javascript-delay","IsSet":false,"Value":0},"KeepRelativeLinks":{"Option":"keep-relative-links","Value":false},"LoadErrorHandling":{"Option":"load-error-handling","Value":""},"LoadMediaErrorHandling":{"Option":"load-media-error-handling","Value":""},"MinimumFontSize":{"Option":"minimum-font-size","IsSet":false,"Value":0},"NoBackground":{"Option":"no-background","Value":false},"NoCustomHeaderPropagation":{"Option":"no-custom-header-propagation","Value":false},"NoImages":{"Option":"no-images","Value":false},"NoStopSlowScripts":{"Option":"no-stop-slow-scripts","Value":false},"PageOffset":{"Option":"page-offset","IsSet":false,"Value":0},"Password":{"Option":"password","Value":""},"Post":{"Option":"post","Value":null},"PostFile":{"Option":"post-file","Value":null},"PrintMediaType":{"Option":"print-media-type","Value":false},"Proxy":{"Option":"proxy","Value":""},"ProxyHostnameLookup":{"Option":"proxy-hostname-lookup","Value":false},"RadiobuttonCheckedSvg":{"Option":"radiobutton-checked-svg","Value":""},"RadiobuttonSvg":{"Option":"radiobutton-svg","Value":""},"RunScript":{"Option":"run-script","Value":null},"SslCrtPath":{"Option":"ssl-crt-path","Value":""},"SslKeyPassword":{"Option":"ssl-key-password","Value":""},"SslKeyPath":{"Option":"ssl-key-path","Value":""},"Username":{"Option":"username","Value":""},"UserStyleSheet":{"Option":"user-style-sheet","Value":""},"ViewportSize":{"Option":"viewport-size","Value":""},"WindowStatus":{"Option":"window-status","Value":""},"Zoom":{"Option":"zoom","IsSet":false,"Value":0},"DisableDottedLines":{"Option":"disable-dotted-lines","Value":true},"DisableTocLinks":{"Option":"disable-toc-links","Value":false},"TocHeaderText":{"Option":"toc-header-text","Value":""},"TocLevelIndentation":{"Option":"toc-level-indentation","IsSet":false,"Value":0},"TocTextSizeShrink":{"Option":"toc-text-size-shrink","IsSet":false,"Value":0},"XslStyleSheet":{"Option":"xsl-style-sheet","Value":""},"FooterCenter":{"Option":"footer-center","Value":""},"FooterFontName":{"Option":"footer-font-name","Value":""},"FooterFontSize":{"Option":"footer-font-size","IsSet":false,"Value":0},"FooterHTML":{"Option":"footer-html","Value":""},"FooterLeft":{"Option":"footer-left","Value":""},"FooterLine":{"Option":"footer-line","Value":false},"FooterRight":{"Option":"footer-right","Value":""},"FooterSpacing":{"Option":"footer-spacing","IsSet":false,"Value":0},"HeaderCenter":{"Option":"header-center","Value":""},"HeaderFontName":{"Option":"header-font-name","Value":""},"HeaderFontSize":{"Option":"header-font-size","IsSet":false,"Value":0},"HeaderHTML":{"Option":"header-html","Value":""},"HeaderLeft":{"Option":"header-left","Value":""},"HeaderLine":{"Option":"header-line","Value":false},"HeaderRight":{"Option":"header-right","Value":""},"HeaderSpacing":{"Option":"header-spacing","IsSet":false,"Value":0},"Replace":{"Option":"replace","Value":null}},"Pages":[{"PageOptions":{"Allow":{"Option":"allow","Value":["/usr/local/html","/usr/local/images"]},"BypassProxyFor":{"Option":"bypass-proxy-for","Value":null},"CacheDir":{"Option":"cache-dir","Value":""},"CheckboxCheckedSvg":{"Option":"checkbox-checked-svg","Value":""},"CheckboxSvg":{"Option":"checkbox-svg","Value":""},"Cookie":{"Option":"cookie","Value":null},"CustomHeader":{"Option":"custom-header","Value":{"X-AppKey":"abcdef"}},"CustomHeaderPropagation":{"Option":"custom-header-propagation","Value":false},"DebugJavascript":{"Option":"debug-javascript","Value":false},"DefaultHeader":{"Option":"default-header","Value":false},"DisableExternalLinks":{"Option":"disable-external-links","Value":false},"DisableInternalLinks":{"Option":"disable-internal-links","Value":false},"DisableJavascript":{"Option":"disable-javascript","Value":false},"DisableLocalFileAccess":{"Option":"disable-local-file-access","Value":false},"DisableSmartShrinking":{"Option":"disable-smart-shrinking","Value":true},"EnableForms":{"Option":"enable-forms","Value":false},"EnableLocalFileAccess":{"Option":"enable-local-file-access","Value":false},"EnablePlugins":{"Option":"enable-plugins","Value":false},"EnableTocBackLinks":{"Option":"enable-toc-back-links","Value":false},"Encoding":{"Option":"encoding","Value":""},"ExcludeFromOutline":{"Option":"exclude-from-outline","Value":false},"JavascriptDelay":{"Option":"javascript-delay","IsSet":false,"Value":0},"KeepRelativeLinks":{"Option":"keep-relative-links","Value":false},"LoadErrorHandling":{"Option":"load-error-handling","Value":""},"LoadMediaErrorHandling":{"Option":"load-media-error-handling","Value":""},"MinimumFontSize":{"Option":"minimum-font-size","IsSet":false,"Value":0},"NoBackground":{"Option":"no-background","Value":false},"NoCustomHeaderPropagation":{"Option":"no-custom-header-propagation","Value":false},"NoImages":{"Option":"no-images","Value":false},"NoStopSlowScripts":{"Option":"no-stop-slow-scripts","Value":false},"PageOffset":{"Option":"page-offset","IsSet":false,"Value":0},"Password":{"Option":"password","Value":""},"Post":{"Option":"post","Value":null},"PostFile":{"Option":"post-file","Value":null},"PrintMediaType":{"Option":"print-media-type","Value":false},"Proxy":{"Option":"proxy","Value":""},"ProxyHostnameLookup":{"Option":"proxy-hostname-lookup","Value":false},"RadiobuttonCheckedSvg":{"Option":"radiobutton-checked-svg","Value":""},"RadiobuttonSvg":{"Option":"radiobutton-svg","Value":""},"RunScript":{"Option":"run-script","Value":null},"SslCrtPath":{"Option":"ssl-crt-path","Value":""},"SslKeyPassword":{"Option":"ssl-key-password","Value":""},"SslKeyPath":{"Option":"ssl-key-path","Value":""},"Username":{"Option":"username","Value":""},"UserStyleSheet":{"Option":"user-style-sheet","Value":""},"ViewportSize":{"Option":"viewport-size","Value":"3840x2160"},"WindowStatus":{"Option":"window-status","Value":""},"Zoom":{"Option":"zoom","IsSet":false,"Value":0},"FooterCenter":{"Option":"footer-center","Value":""},"FooterFontName":{"Option":"footer-font-name","Value":""},"FooterFontSize":{"Option":"footer-font-size","IsSet":false,"Value":0},"FooterHTML":{"Option":"footer-html","Value":""},"FooterLeft":{"Option":"footer-left","Value":""},"FooterLine":{"Option":"footer-line","Value":false},"FooterRight":{"Option":"footer-right","Value":""},"FooterSpacing":{"Option":"footer-spacing","IsSet":false,"Value":0},"HeaderCenter":{"Option":"header-center","Value":""},"HeaderFontName":{"Option":"header-font-name","Value":""},"HeaderFontSize":{"Option":"header-font-size","IsSet":false,"Value":0},"HeaderHTML":{"Option":"header-html","Value":""},"HeaderLeft":{"Option":"header-left","Value":""},"HeaderLine":{"Option":"header-line","Value":false},"HeaderRight":{"Option":"header-right","Value":""},"HeaderSpacing":{"Option":"header-spacing","IsSet":true,"Value":10.01},"Replace":{"Option":"replace","Value":null}},"InputFile":"https://www.google.com","Base64PageData":""},{"PageOptions":{"Allow":{"Option":"allow","Value":null},"BypassProxyFor":{"Option":"bypass-proxy-for","Value":null},"CacheDir":{"Option":"cache-dir","Value":""},"CheckboxCheckedSvg":{"Option":"checkbox-checked-svg","Value":""},"CheckboxSvg":{"Option":"checkbox-svg","Value":""},"Cookie":{"Option":"cookie","Value":null},"CustomHeader":{"Option":"custom-header","Value":null},"CustomHeaderPropagation":{"Option":"custom-header-propagation","Value":false},"DebugJavascript":{"Option":"debug-javascript","Value":false},"DefaultHeader":{"Option":"default-header","Value":false},"DisableExternalLinks":{"Option":"disable-external-links","Value":false},"DisableInternalLinks":{"Option":"disable-internal-links","Value":false},"DisableJavascript":{"Option":"disable-javascript","Value":false},"DisableLocalFileAccess":{"Option":"disable-local-file-access","Value":false},"DisableSmartShrinking":{"Option":"disable-smart-shrinking","Value":false},"EnableForms":{"Option":"enable-forms","Value":false},"EnableLocalFileAccess":{"Option":"enable-local-file-access","Value":false},"EnablePlugins":{"Option":"enable-plugins","Value":false},"EnableTocBackLinks":{"Option":"enable-toc-back-links","Value":false},"Encoding":{"Option":"encoding","Value":""},"ExcludeFromOutline":{"Option":"exclude-from-outline","Value":false},"JavascriptDelay":{"Option":"javascript-delay","IsSet":false,"Value":0},"KeepRelativeLinks":{"Option":"keep-relative-links","Value":false},"LoadErrorHandling":{"Option":"load-error-handling","Value":""},"LoadMediaErrorHandling":{"Option":"load-media-error-handling","Value":""},"MinimumFontSize":{"Option":"minimum-font-size","IsSet":false,"Value":0},"NoBackground":{"Option":"no-background","Value":false},"NoCustomHeaderPropagation":{"Option":"no-custom-header-propagation","Value":false},"NoImages":{"Option":"no-images","Value":false},"NoStopSlowScripts":{"Option":"no-stop-slow-scripts","Value":false},"PageOffset":{"Option":"page-offset","IsSet":false,"Value":0},"Password":{"Option":"password","Value":""},"Post":{"Option":"post","Value":null},"PostFile":{"Option":"post-file","Value":null},"PrintMediaType":{"Option":"print-media-type","Value":false},"Proxy":{"Option":"proxy","Value":""},"ProxyHostnameLookup":{"Option":"proxy-hostname-lookup","Value":false},"RadiobuttonCheckedSvg":{"Option":"radiobutton-checked-svg","Value":""},"RadiobuttonSvg":{"Option":"radiobutton-svg","Value":""},"RunScript":{"Option":"run-script","Value":null},"SslCrtPath":{"Option":"ssl-crt-path","Value":""},"SslKeyPassword":{"Option":"ssl-key-password","Value":""},"SslKeyPath":{"Option":"ssl-key-path","Value":""},"Username":{"Option":"username","Value":""},"UserStyleSheet":{"Option":"user-style-sheet","Value":""},"ViewportSize":{"Option":"viewport-size","Value":""},"WindowStatus":{"Option":"window-status","Value":""},"Zoom":{"Option":"zoom","IsSet":false,"Value":0},"FooterCenter":{"Option":"footer-center","Value":""},"FooterFontName":{"Option":"footer-font-name","Value":""},"FooterFontSize":{"Option":"footer-font-size","IsSet":false,"Value":0},"FooterHTML":{"Option":"footer-html","Value":""},"FooterLeft":{"Option":"footer-left","Value":""},"FooterLine":{"Option":"footer-line","Value":false},"FooterRight":{"Option":"footer-right","Value":""},"FooterSpacing":{"Option":"footer-spacing","IsSet":false,"Value":0},"HeaderCenter":{"Option":"header-center","Value":""},"HeaderFontName":{"Option":"header-font-name","Value":""},"HeaderFontSize":{"Option":"header-font-size","IsSet":false,"Value":0},"HeaderHTML":{"Option":"header-html","Value":""},"HeaderLeft":{"Option":"header-left","Value":""},"HeaderLine":{"Option":"header-line","Value":false},"HeaderRight":{"Option":"header-right","Value":""},"HeaderSpacing":{"Option":"header-spacing","IsSet":false,"Value":0},"Replace":{"Option":"replace","Value":null}},"InputFile":"-","Base64PageData":"PCFkb2N0eXBlIGh0bWw+PGh0bWw+PGhlYWQ+PHRpdGxlPldLSFRNTFRPUERGIFRFU1Q8L3RpdGxlPjwvaGVhZD48Ym9keT5IRUxMTyBQREY8L2JvZHk+PC9odG1sPg=="}]}` - if want != string(jb) { - t.Errorf("Want JSON:\n%s\nHave:\n%s", want, string(jb)) - } + assert.JSONEq(t, string(expected), string(jb)) } func TestNewPDFGeneratorFromJSON(t *testing.T) { @@ -49,12 +48,9 @@ func TestNewPDFGeneratorFromJSON(t *testing.T) { t.Fatal(err) } - want := wantArgString() - if pdfgFromJSON.ArgString() != want { - t.Errorf("Want argstring:\n%s\nHave:\n%s", want, pdfgFromJSON.ArgString()) - } + assert.Equal(t, expectedArgString(), pdfgFromJSON.ArgString()) - pdfgFromJSON.OutputFile = "./testfiles/TestNewPDFGeneratorFromJSON.PDF" + pdfgFromJSON.OutputFile = "testdata/TestNewPDFGeneratorFromJSON.PDF" err = pdfgFromJSON.Create() if err != nil { @@ -66,7 +62,7 @@ func TestNewPDFGeneratorFromJSON(t *testing.T) { func TestNewPDFGeneratorFromJSONWithReader(t *testing.T) { pdfg := NewPDFPreparer() - htmlfile, err := ioutil.ReadFile("./testfiles/htmlsimple.html") + htmlfile, err := ioutil.ReadFile("testdata/htmlsimple.html") if err != nil { t.Fatal(err) } diff --git a/testfiles/credits.txt b/testdata/credits.txt similarity index 100% rename from testfiles/credits.txt rename to testdata/credits.txt diff --git a/testdata/expected.json b/testdata/expected.json new file mode 100644 index 0000000..5ab87b4 --- /dev/null +++ b/testdata/expected.json @@ -0,0 +1,1196 @@ +{ + "GlobalOptions": { + "CookieJar": { + "Option": "cookie-jar", + "Value": "" + }, + "Copies": { + "Option": "copies", + "IsSet": false, + "Value": 0 + }, + "Dpi": { + "Option": "dpi", + "IsSet": true, + "Value": 600 + }, + "ExtendedHelp": { + "Option": "extended-help", + "Value": false + }, + "Grayscale": { + "Option": "grayscale", + "Value": false + }, + "Help": { + "Option": "help", + "Value": false + }, + "HTMLDoc": { + "Option": "htmldoc", + "Value": false + }, + "ImageDpi": { + "Option": "image-dpi", + "IsSet": false, + "Value": 0 + }, + "ImageQuality": { + "Option": "image-quality", + "IsSet": false, + "Value": 0 + }, + "License": { + "Option": "license", + "Value": false + }, + "LogLevel": { + "Option": "log-level", + "Value": "" + }, + "LowQuality": { + "Option": "lowquality", + "Value": false + }, + "ManPage": { + "Option": "manpage", + "Value": false + }, + "MarginBottom": { + "Option": "margin-bottom", + "IsSet": true, + "Value": 40 + }, + "MarginLeft": { + "Option": "margin-left", + "IsSet": true, + "Value": 0 + }, + "MarginRight": { + "Option": "margin-right", + "IsSet": false, + "Value": 0 + }, + "MarginTop": { + "Option": "margin-top", + "IsSet": false, + "Value": 0 + }, + "NoCollate": { + "Option": "no-collate", + "Value": false + }, + "NoPdfCompression": { + "Option": "no-pdf-compression", + "Value": false + }, + "Orientation": { + "Option": "orientation", + "Value": "" + }, + "PageHeight": { + "Option": "page-height", + "IsSet": false, + "Value": 0 + }, + "PageSize": { + "Option": "page-size", + "Value": "A4" + }, + "PageWidth": { + "Option": "page-width", + "IsSet": false, + "Value": 0 + }, + "Quiet": { + "Option": "quiet", + "Value": false + }, + "ReadArgsFromStdin": { + "Option": "read-args-from-stdin", + "Value": false + }, + "Readme": { + "Option": "readme", + "Value": false + }, + "Title": { + "Option": "title", + "Value": "" + }, + "Version": { + "Option": "version", + "Value": false + } + }, + "OutlineOptions": { + "DumpDefaultTocXsl": { + "Option": "dump-default-toc-xsl", + "Value": false + }, + "DumpOutline": { + "Option": "dump-outline", + "Value": "" + }, + "NoOutline": { + "Option": "no-outline", + "Value": false + }, + "OutlineDepth": { + "Option": "outline-depth", + "IsSet": false, + "Value": 0 + } + }, + "Cover": { + "Input": "https://wkhtmltopdf.org/index.html", + "Allow": { + "Option": "allow", + "Value": null + }, + "BypassProxyFor": { + "Option": "bypass-proxy-for", + "Value": null + }, + "CacheDir": { + "Option": "cache-dir", + "Value": "" + }, + "CheckboxCheckedSvg": { + "Option": "checkbox-checked-svg", + "Value": "" + }, + "CheckboxSvg": { + "Option": "checkbox-svg", + "Value": "" + }, + "Cookie": { + "Option": "cookie", + "Value": null + }, + "CustomHeader": { + "Option": "custom-header", + "Value": null + }, + "CustomHeaderPropagation": { + "Option": "custom-header-propagation", + "Value": false + }, + "DebugJavascript": { + "Option": "debug-javascript", + "Value": false + }, + "DefaultHeader": { + "Option": "default-header", + "Value": false + }, + "DisableExternalLinks": { + "Option": "disable-external-links", + "Value": false + }, + "DisableInternalLinks": { + "Option": "disable-internal-links", + "Value": false + }, + "DisableJavascript": { + "Option": "disable-javascript", + "Value": false + }, + "DisableLocalFileAccess": { + "Option": "disable-local-file-access", + "Value": false + }, + "DisableSmartShrinking": { + "Option": "disable-smart-shrinking", + "Value": false + }, + "EnableForms": { + "Option": "enable-forms", + "Value": false + }, + "EnableLocalFileAccess": { + "Option": "enable-local-file-access", + "Value": false + }, + "EnablePlugins": { + "Option": "enable-plugins", + "Value": false + }, + "EnableTocBackLinks": { + "Option": "enable-toc-back-links", + "Value": false + }, + "Encoding": { + "Option": "encoding", + "Value": "" + }, + "ExcludeFromOutline": { + "Option": "exclude-from-outline", + "Value": false + }, + "JavascriptDelay": { + "Option": "javascript-delay", + "IsSet": false, + "Value": 0 + }, + "KeepRelativeLinks": { + "Option": "keep-relative-links", + "Value": false + }, + "LoadErrorHandling": { + "Option": "load-error-handling", + "Value": "" + }, + "LoadMediaErrorHandling": { + "Option": "load-media-error-handling", + "Value": "" + }, + "MinimumFontSize": { + "Option": "minimum-font-size", + "IsSet": false, + "Value": 0 + }, + "NoBackground": { + "Option": "no-background", + "Value": false + }, + "NoCustomHeaderPropagation": { + "Option": "no-custom-header-propagation", + "Value": false + }, + "NoImages": { + "Option": "no-images", + "Value": false + }, + "NoStopSlowScripts": { + "Option": "no-stop-slow-scripts", + "Value": false + }, + "PageOffset": { + "Option": "page-offset", + "IsSet": false, + "Value": 0 + }, + "Password": { + "Option": "password", + "Value": "" + }, + "Post": { + "Option": "post", + "Value": null + }, + "PostFile": { + "Option": "post-file", + "Value": null + }, + "PrintMediaType": { + "Option": "print-media-type", + "Value": false + }, + "Proxy": { + "Option": "proxy", + "Value": "" + }, + "ProxyHostnameLookup": { + "Option": "proxy-hostname-lookup", + "Value": false + }, + "RadiobuttonCheckedSvg": { + "Option": "radiobutton-checked-svg", + "Value": "" + }, + "RadiobuttonSvg": { + "Option": "radiobutton-svg", + "Value": "" + }, + "RunScript": { + "Option": "run-script", + "Value": null + }, + "SslCrtPath": { + "Option": "ssl-crt-path", + "Value": "" + }, + "SslKeyPassword": { + "Option": "ssl-key-password", + "Value": "" + }, + "SslKeyPath": { + "Option": "ssl-key-path", + "Value": "" + }, + "Username": { + "Option": "username", + "Value": "" + }, + "UserStyleSheet": { + "Option": "user-style-sheet", + "Value": "" + }, + "ViewportSize": { + "Option": "viewport-size", + "Value": "" + }, + "WindowStatus": { + "Option": "window-status", + "Value": "" + }, + "Zoom": { + "Option": "zoom", + "IsSet": true, + "Value": 0.75 + } + }, + "TOC": { + "Include": true, + "Allow": { + "Option": "allow", + "Value": null + }, + "BypassProxyFor": { + "Option": "bypass-proxy-for", + "Value": null + }, + "CacheDir": { + "Option": "cache-dir", + "Value": "" + }, + "CheckboxCheckedSvg": { + "Option": "checkbox-checked-svg", + "Value": "" + }, + "CheckboxSvg": { + "Option": "checkbox-svg", + "Value": "" + }, + "Cookie": { + "Option": "cookie", + "Value": null + }, + "CustomHeader": { + "Option": "custom-header", + "Value": null + }, + "CustomHeaderPropagation": { + "Option": "custom-header-propagation", + "Value": false + }, + "DebugJavascript": { + "Option": "debug-javascript", + "Value": false + }, + "DefaultHeader": { + "Option": "default-header", + "Value": false + }, + "DisableExternalLinks": { + "Option": "disable-external-links", + "Value": false + }, + "DisableInternalLinks": { + "Option": "disable-internal-links", + "Value": false + }, + "DisableJavascript": { + "Option": "disable-javascript", + "Value": false + }, + "DisableLocalFileAccess": { + "Option": "disable-local-file-access", + "Value": false + }, + "DisableSmartShrinking": { + "Option": "disable-smart-shrinking", + "Value": false + }, + "EnableForms": { + "Option": "enable-forms", + "Value": false + }, + "EnableLocalFileAccess": { + "Option": "enable-local-file-access", + "Value": false + }, + "EnablePlugins": { + "Option": "enable-plugins", + "Value": false + }, + "EnableTocBackLinks": { + "Option": "enable-toc-back-links", + "Value": false + }, + "Encoding": { + "Option": "encoding", + "Value": "" + }, + "ExcludeFromOutline": { + "Option": "exclude-from-outline", + "Value": false + }, + "JavascriptDelay": { + "Option": "javascript-delay", + "IsSet": false, + "Value": 0 + }, + "KeepRelativeLinks": { + "Option": "keep-relative-links", + "Value": false + }, + "LoadErrorHandling": { + "Option": "load-error-handling", + "Value": "" + }, + "LoadMediaErrorHandling": { + "Option": "load-media-error-handling", + "Value": "" + }, + "MinimumFontSize": { + "Option": "minimum-font-size", + "IsSet": false, + "Value": 0 + }, + "NoBackground": { + "Option": "no-background", + "Value": false + }, + "NoCustomHeaderPropagation": { + "Option": "no-custom-header-propagation", + "Value": false + }, + "NoImages": { + "Option": "no-images", + "Value": false + }, + "NoStopSlowScripts": { + "Option": "no-stop-slow-scripts", + "Value": false + }, + "PageOffset": { + "Option": "page-offset", + "IsSet": false, + "Value": 0 + }, + "Password": { + "Option": "password", + "Value": "" + }, + "Post": { + "Option": "post", + "Value": null + }, + "PostFile": { + "Option": "post-file", + "Value": null + }, + "PrintMediaType": { + "Option": "print-media-type", + "Value": false + }, + "Proxy": { + "Option": "proxy", + "Value": "" + }, + "ProxyHostnameLookup": { + "Option": "proxy-hostname-lookup", + "Value": false + }, + "RadiobuttonCheckedSvg": { + "Option": "radiobutton-checked-svg", + "Value": "" + }, + "RadiobuttonSvg": { + "Option": "radiobutton-svg", + "Value": "" + }, + "RunScript": { + "Option": "run-script", + "Value": null + }, + "SslCrtPath": { + "Option": "ssl-crt-path", + "Value": "" + }, + "SslKeyPassword": { + "Option": "ssl-key-password", + "Value": "" + }, + "SslKeyPath": { + "Option": "ssl-key-path", + "Value": "" + }, + "Username": { + "Option": "username", + "Value": "" + }, + "UserStyleSheet": { + "Option": "user-style-sheet", + "Value": "" + }, + "ViewportSize": { + "Option": "viewport-size", + "Value": "" + }, + "WindowStatus": { + "Option": "window-status", + "Value": "" + }, + "Zoom": { + "Option": "zoom", + "IsSet": false, + "Value": 0 + }, + "DisableDottedLines": { + "Option": "disable-dotted-lines", + "Value": true + }, + "DisableTocLinks": { + "Option": "disable-toc-links", + "Value": false + }, + "TocHeaderText": { + "Option": "toc-header-text", + "Value": "" + }, + "TocLevelIndentation": { + "Option": "toc-level-indentation", + "IsSet": false, + "Value": 0 + }, + "TocTextSizeShrink": { + "Option": "toc-text-size-shrink", + "IsSet": false, + "Value": 0 + }, + "XslStyleSheet": { + "Option": "xsl-style-sheet", + "Value": "" + }, + "FooterCenter": { + "Option": "footer-center", + "Value": "" + }, + "FooterFontName": { + "Option": "footer-font-name", + "Value": "" + }, + "FooterFontSize": { + "Option": "footer-font-size", + "IsSet": false, + "Value": 0 + }, + "FooterHTML": { + "Option": "footer-html", + "Value": "" + }, + "FooterLeft": { + "Option": "footer-left", + "Value": "" + }, + "FooterLine": { + "Option": "footer-line", + "Value": false + }, + "FooterRight": { + "Option": "footer-right", + "Value": "" + }, + "FooterSpacing": { + "Option": "footer-spacing", + "IsSet": false, + "Value": 0 + }, + "HeaderCenter": { + "Option": "header-center", + "Value": "" + }, + "HeaderFontName": { + "Option": "header-font-name", + "Value": "" + }, + "HeaderFontSize": { + "Option": "header-font-size", + "IsSet": false, + "Value": 0 + }, + "HeaderHTML": { + "Option": "header-html", + "Value": "" + }, + "HeaderLeft": { + "Option": "header-left", + "Value": "" + }, + "HeaderLine": { + "Option": "header-line", + "Value": false + }, + "HeaderRight": { + "Option": "header-right", + "Value": "" + }, + "HeaderSpacing": { + "Option": "header-spacing", + "IsSet": false, + "Value": 0 + }, + "Replace": { + "Option": "replace", + "Value": null + } + }, + "Pages": [ + { + "PageOptions": { + "Allow": { + "Option": "allow", + "Value": [ + "/usr/local/html", + "/usr/local/images" + ] + }, + "BypassProxyFor": { + "Option": "bypass-proxy-for", + "Value": null + }, + "CacheDir": { + "Option": "cache-dir", + "Value": "" + }, + "CheckboxCheckedSvg": { + "Option": "checkbox-checked-svg", + "Value": "" + }, + "CheckboxSvg": { + "Option": "checkbox-svg", + "Value": "" + }, + "Cookie": { + "Option": "cookie", + "Value": null + }, + "CustomHeader": { + "Option": "custom-header", + "Value": { + "X-AppKey": "abcdef" + } + }, + "CustomHeaderPropagation": { + "Option": "custom-header-propagation", + "Value": false + }, + "DebugJavascript": { + "Option": "debug-javascript", + "Value": false + }, + "DefaultHeader": { + "Option": "default-header", + "Value": false + }, + "DisableExternalLinks": { + "Option": "disable-external-links", + "Value": false + }, + "DisableInternalLinks": { + "Option": "disable-internal-links", + "Value": false + }, + "DisableJavascript": { + "Option": "disable-javascript", + "Value": false + }, + "DisableLocalFileAccess": { + "Option": "disable-local-file-access", + "Value": false + }, + "DisableSmartShrinking": { + "Option": "disable-smart-shrinking", + "Value": true + }, + "EnableForms": { + "Option": "enable-forms", + "Value": false + }, + "EnableLocalFileAccess": { + "Option": "enable-local-file-access", + "Value": true + }, + "EnablePlugins": { + "Option": "enable-plugins", + "Value": false + }, + "EnableTocBackLinks": { + "Option": "enable-toc-back-links", + "Value": false + }, + "Encoding": { + "Option": "encoding", + "Value": "" + }, + "ExcludeFromOutline": { + "Option": "exclude-from-outline", + "Value": false + }, + "JavascriptDelay": { + "Option": "javascript-delay", + "IsSet": false, + "Value": 0 + }, + "KeepRelativeLinks": { + "Option": "keep-relative-links", + "Value": false + }, + "LoadErrorHandling": { + "Option": "load-error-handling", + "Value": "" + }, + "LoadMediaErrorHandling": { + "Option": "load-media-error-handling", + "Value": "" + }, + "MinimumFontSize": { + "Option": "minimum-font-size", + "IsSet": false, + "Value": 0 + }, + "NoBackground": { + "Option": "no-background", + "Value": false + }, + "NoCustomHeaderPropagation": { + "Option": "no-custom-header-propagation", + "Value": false + }, + "NoImages": { + "Option": "no-images", + "Value": false + }, + "NoStopSlowScripts": { + "Option": "no-stop-slow-scripts", + "Value": false + }, + "PageOffset": { + "Option": "page-offset", + "IsSet": false, + "Value": 0 + }, + "Password": { + "Option": "password", + "Value": "" + }, + "Post": { + "Option": "post", + "Value": null + }, + "PostFile": { + "Option": "post-file", + "Value": null + }, + "PrintMediaType": { + "Option": "print-media-type", + "Value": false + }, + "Proxy": { + "Option": "proxy", + "Value": "" + }, + "ProxyHostnameLookup": { + "Option": "proxy-hostname-lookup", + "Value": false + }, + "RadiobuttonCheckedSvg": { + "Option": "radiobutton-checked-svg", + "Value": "" + }, + "RadiobuttonSvg": { + "Option": "radiobutton-svg", + "Value": "" + }, + "RunScript": { + "Option": "run-script", + "Value": null + }, + "SslCrtPath": { + "Option": "ssl-crt-path", + "Value": "" + }, + "SslKeyPassword": { + "Option": "ssl-key-password", + "Value": "" + }, + "SslKeyPath": { + "Option": "ssl-key-path", + "Value": "" + }, + "Username": { + "Option": "username", + "Value": "" + }, + "UserStyleSheet": { + "Option": "user-style-sheet", + "Value": "" + }, + "ViewportSize": { + "Option": "viewport-size", + "Value": "3840x2160" + }, + "WindowStatus": { + "Option": "window-status", + "Value": "" + }, + "Zoom": { + "Option": "zoom", + "IsSet": false, + "Value": 0 + }, + "FooterCenter": { + "Option": "footer-center", + "Value": "" + }, + "FooterFontName": { + "Option": "footer-font-name", + "Value": "" + }, + "FooterFontSize": { + "Option": "footer-font-size", + "IsSet": false, + "Value": 0 + }, + "FooterHTML": { + "Option": "footer-html", + "Value": "" + }, + "FooterLeft": { + "Option": "footer-left", + "Value": "" + }, + "FooterLine": { + "Option": "footer-line", + "Value": false + }, + "FooterRight": { + "Option": "footer-right", + "Value": "" + }, + "FooterSpacing": { + "Option": "footer-spacing", + "IsSet": false, + "Value": 0 + }, + "HeaderCenter": { + "Option": "header-center", + "Value": "" + }, + "HeaderFontName": { + "Option": "header-font-name", + "Value": "" + }, + "HeaderFontSize": { + "Option": "header-font-size", + "IsSet": false, + "Value": 0 + }, + "HeaderHTML": { + "Option": "header-html", + "Value": "" + }, + "HeaderLeft": { + "Option": "header-left", + "Value": "" + }, + "HeaderLine": { + "Option": "header-line", + "Value": false + }, + "HeaderRight": { + "Option": "header-right", + "Value": "" + }, + "HeaderSpacing": { + "Option": "header-spacing", + "IsSet": true, + "Value": 10.01 + }, + "Replace": { + "Option": "replace", + "Value": null + } + }, + "InputFile": "https://www.google.com", + "Base64PageData": "" + }, + { + "PageOptions": { + "Allow": { + "Option": "allow", + "Value": null + }, + "BypassProxyFor": { + "Option": "bypass-proxy-for", + "Value": null + }, + "CacheDir": { + "Option": "cache-dir", + "Value": "" + }, + "CheckboxCheckedSvg": { + "Option": "checkbox-checked-svg", + "Value": "" + }, + "CheckboxSvg": { + "Option": "checkbox-svg", + "Value": "" + }, + "Cookie": { + "Option": "cookie", + "Value": null + }, + "CustomHeader": { + "Option": "custom-header", + "Value": null + }, + "CustomHeaderPropagation": { + "Option": "custom-header-propagation", + "Value": false + }, + "DebugJavascript": { + "Option": "debug-javascript", + "Value": false + }, + "DefaultHeader": { + "Option": "default-header", + "Value": false + }, + "DisableExternalLinks": { + "Option": "disable-external-links", + "Value": false + }, + "DisableInternalLinks": { + "Option": "disable-internal-links", + "Value": false + }, + "DisableJavascript": { + "Option": "disable-javascript", + "Value": false + }, + "DisableLocalFileAccess": { + "Option": "disable-local-file-access", + "Value": false + }, + "DisableSmartShrinking": { + "Option": "disable-smart-shrinking", + "Value": false + }, + "EnableForms": { + "Option": "enable-forms", + "Value": false + }, + "EnableLocalFileAccess": { + "Option": "enable-local-file-access", + "Value": false + }, + "EnablePlugins": { + "Option": "enable-plugins", + "Value": false + }, + "EnableTocBackLinks": { + "Option": "enable-toc-back-links", + "Value": false + }, + "Encoding": { + "Option": "encoding", + "Value": "" + }, + "ExcludeFromOutline": { + "Option": "exclude-from-outline", + "Value": false + }, + "JavascriptDelay": { + "Option": "javascript-delay", + "IsSet": false, + "Value": 0 + }, + "KeepRelativeLinks": { + "Option": "keep-relative-links", + "Value": false + }, + "LoadErrorHandling": { + "Option": "load-error-handling", + "Value": "" + }, + "LoadMediaErrorHandling": { + "Option": "load-media-error-handling", + "Value": "" + }, + "MinimumFontSize": { + "Option": "minimum-font-size", + "IsSet": false, + "Value": 0 + }, + "NoBackground": { + "Option": "no-background", + "Value": false + }, + "NoCustomHeaderPropagation": { + "Option": "no-custom-header-propagation", + "Value": false + }, + "NoImages": { + "Option": "no-images", + "Value": false + }, + "NoStopSlowScripts": { + "Option": "no-stop-slow-scripts", + "Value": false + }, + "PageOffset": { + "Option": "page-offset", + "IsSet": false, + "Value": 0 + }, + "Password": { + "Option": "password", + "Value": "" + }, + "Post": { + "Option": "post", + "Value": null + }, + "PostFile": { + "Option": "post-file", + "Value": null + }, + "PrintMediaType": { + "Option": "print-media-type", + "Value": false + }, + "Proxy": { + "Option": "proxy", + "Value": "" + }, + "ProxyHostnameLookup": { + "Option": "proxy-hostname-lookup", + "Value": false + }, + "RadiobuttonCheckedSvg": { + "Option": "radiobutton-checked-svg", + "Value": "" + }, + "RadiobuttonSvg": { + "Option": "radiobutton-svg", + "Value": "" + }, + "RunScript": { + "Option": "run-script", + "Value": null + }, + "SslCrtPath": { + "Option": "ssl-crt-path", + "Value": "" + }, + "SslKeyPassword": { + "Option": "ssl-key-password", + "Value": "" + }, + "SslKeyPath": { + "Option": "ssl-key-path", + "Value": "" + }, + "Username": { + "Option": "username", + "Value": "" + }, + "UserStyleSheet": { + "Option": "user-style-sheet", + "Value": "" + }, + "ViewportSize": { + "Option": "viewport-size", + "Value": "" + }, + "WindowStatus": { + "Option": "window-status", + "Value": "" + }, + "Zoom": { + "Option": "zoom", + "IsSet": false, + "Value": 0 + }, + "FooterCenter": { + "Option": "footer-center", + "Value": "" + }, + "FooterFontName": { + "Option": "footer-font-name", + "Value": "" + }, + "FooterFontSize": { + "Option": "footer-font-size", + "IsSet": false, + "Value": 0 + }, + "FooterHTML": { + "Option": "footer-html", + "Value": "" + }, + "FooterLeft": { + "Option": "footer-left", + "Value": "" + }, + "FooterLine": { + "Option": "footer-line", + "Value": false + }, + "FooterRight": { + "Option": "footer-right", + "Value": "" + }, + "FooterSpacing": { + "Option": "footer-spacing", + "IsSet": false, + "Value": 0 + }, + "HeaderCenter": { + "Option": "header-center", + "Value": "" + }, + "HeaderFontName": { + "Option": "header-font-name", + "Value": "" + }, + "HeaderFontSize": { + "Option": "header-font-size", + "IsSet": false, + "Value": 0 + }, + "HeaderHTML": { + "Option": "header-html", + "Value": "" + }, + "HeaderLeft": { + "Option": "header-left", + "Value": "" + }, + "HeaderLine": { + "Option": "header-line", + "Value": false + }, + "HeaderRight": { + "Option": "header-right", + "Value": "" + }, + "HeaderSpacing": { + "Option": "header-spacing", + "IsSet": false, + "Value": 0 + }, + "Replace": { + "Option": "replace", + "Value": null + } + }, + "InputFile": "-", + "Base64PageData": "PCFkb2N0eXBlIGh0bWw+PGh0bWw+PGhlYWQ+PHRpdGxlPldLSFRNTFRPUERGIFRFU1Q8L3RpdGxlPjwvaGVhZD48Ym9keT5IRUxMTyBQREY8L2JvZHk+PC9odG1sPg==" + } + ] +} \ No newline at end of file diff --git a/testfiles/footer-toc.html b/testdata/footer-toc.html similarity index 100% rename from testfiles/footer-toc.html rename to testdata/footer-toc.html diff --git a/testfiles/footer.html b/testdata/footer.html similarity index 100% rename from testfiles/footer.html rename to testdata/footer.html diff --git a/testfiles/html5.html b/testdata/html5.html similarity index 100% rename from testfiles/html5.html rename to testdata/html5.html diff --git a/testfiles/htmlsimple.html b/testdata/htmlsimple.html similarity index 100% rename from testfiles/htmlsimple.html rename to testdata/htmlsimple.html diff --git a/testfiles/js/footer.js b/testdata/js/footer.js similarity index 100% rename from testfiles/js/footer.js rename to testdata/js/footer.js diff --git a/testfiles/toc.xls b/testdata/toc.xls similarity index 100% rename from testfiles/toc.xls rename to testdata/toc.xls diff --git a/wkhtmltopdf_test.go b/wkhtmltopdf_test.go index fce1e4d..edc0e2c 100644 --- a/wkhtmltopdf_test.go +++ b/wkhtmltopdf_test.go @@ -9,10 +9,12 @@ import ( "strings" "testing" "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func newTestPDFGenerator(tb testing.TB) *PDFGenerator { - pdfg, err := NewPDFGenerator() if err != nil { tb.Fatal(err) @@ -32,6 +34,7 @@ func newTestPDFGenerator(tb testing.TB) *PDFGenerator { page1.Allow.Set("/usr/local/images") page1.CustomHeader.Set("X-AppKey", "abcdef") page1.ViewportSize.Set("3840x2160") + page1.EnableLocalFileAccess.Set(true) pdfg.AddPage(page1) @@ -44,20 +47,16 @@ func newTestPDFGenerator(tb testing.TB) *PDFGenerator { return pdfg } -func wantArgString() string { - return "--dpi 600 --margin-bottom 40 --margin-left 0 --page-size A4 cover https://wkhtmltopdf.org/index.html --zoom 0.750 toc --disable-dotted-lines page https://www.google.com --allow /usr/local/html --allow /usr/local/images --custom-header X-AppKey abcdef --disable-smart-shrinking --viewport-size 3840x2160 --header-spacing 10.010 -" +func expectedArgString() string { + return "--dpi 600 --margin-bottom 40 --margin-left 0 --page-size A4 cover https://wkhtmltopdf.org/index.html --zoom 0.750 toc --disable-dotted-lines page https://www.google.com --allow /usr/local/html --allow /usr/local/images --custom-header X-AppKey abcdef --disable-smart-shrinking --enable-local-file-access --viewport-size 3840x2160 --header-spacing 10.010 -" } func TestArgString(t *testing.T) { pdfg := newTestPDFGenerator(t) - want := wantArgString() - if pdfg.ArgString() != want { - t.Errorf("Want argstring:\n%s\nHave:\n%s", want, pdfg.ArgString()) - } + assert.Equal(t, expectedArgString(), pdfg.ArgString()) + pdfg.SetPages(pdfg.pages) - if pdfg.ArgString() != want { - t.Errorf("Want argstring:\n%s\nHave:\n%s", want, pdfg.ArgString()) - } + assert.Equal(t, expectedArgString(), pdfg.ArgString()) } func TestResetPages(t *testing.T) { @@ -117,19 +116,17 @@ func TestNoInput(t *testing.T) { func TestGeneratePDF(t *testing.T) { pdfg := newTestPDFGenerator(t) err := pdfg.Create() - if err != nil { - t.Fatal(err) - } - err = pdfg.WriteFile("./testfiles/TestGeneratePDF.pdf") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + + err = pdfg.WriteFile("testdata/TestGeneratePDF.pdf") + require.NoError(t, err) + t.Logf("PDF size %vkB", len(pdfg.Bytes())/1024) } func TestContextCancellation(t *testing.T) { pdfg := newTestPDFGenerator(t) - htmlfile, err := ioutil.ReadFile("./testfiles/htmlsimple.html") + htmlfile, err := ioutil.ReadFile("testdata/htmlsimple.html") if err != nil { t.Fatal(err) } @@ -158,7 +155,7 @@ func TestGeneratePdfFromStdinSimple(t *testing.T) { if err != nil { t.Fatal(err) } - htmlfile, err := ioutil.ReadFile("./testfiles/htmlsimple.html") + htmlfile, err := ioutil.ReadFile("testdata/htmlsimple.html") if err != nil { t.Fatal(err) } @@ -167,7 +164,7 @@ func TestGeneratePdfFromStdinSimple(t *testing.T) { if err != nil { t.Fatal(err) } - err = pdfg.WriteFile("./testfiles/TestGeneratePdfFromStdinSimple.pdf") + err = pdfg.WriteFile("testdata/TestGeneratePdfFromStdinSimple.pdf") if err != nil { t.Fatal(err) } @@ -182,13 +179,13 @@ func TestPDFGeneratorOutputFile(t *testing.T) { if err != nil { t.Fatal(err) } - htmlfile, err := os.Open("./testfiles/htmlsimple.html") + htmlfile, err := os.Open("testdata/htmlsimple.html") if err != nil { t.Fatal(err) } defer htmlfile.Close() - pdfg.OutputFile = "./testfiles/TestPDFGeneratorOutputFile.pdf" + pdfg.OutputFile = "testdata/TestPDFGeneratorOutputFile.pdf" pdfg.AddPage(NewPageReader(htmlfile)) err = pdfg.Create() @@ -196,7 +193,7 @@ func TestPDFGeneratorOutputFile(t *testing.T) { t.Fatal(err) } - pdfFile, err := os.Open("./testfiles/TestPDFGeneratorOutputFile.pdf") + pdfFile, err := os.Open("testdata/TestPDFGeneratorOutputFile.pdf") if err != nil { t.Fatal(err) } @@ -214,17 +211,20 @@ func TestPDFGeneratorOutputFile(t *testing.T) { func TestGeneratePdfFromStdinHtml5(t *testing.T) { //Use newTestPDFGenerator and append to page1 and TOC pdfg := newTestPDFGenerator(t) - htmlfile, err := ioutil.ReadFile("./testfiles/html5.html") + htmlfile, err := ioutil.ReadFile("testdata/html5.html") if err != nil { t.Fatal(err) } + page2 := NewPageReader(bytes.NewReader(htmlfile)) + page2.EnableLocalFileAccess.Set(true) pdfg.AddPage(page2) + err = pdfg.Create() if err != nil { t.Fatal(err) } - err = pdfg.WriteFile("./testfiles/TestGeneratePdfFromStdinHtml5.pdf") + err = pdfg.WriteFile("testdata/TestGeneratePdfFromStdinHtml5.pdf") if err != nil { t.Fatal(err) } @@ -247,7 +247,7 @@ func TestSetFooter(t *testing.T) { if err != nil { t.Fatal(err) } - err = pdfg.WriteFile("./testfiles/TestSetFooter.pdf") + err = pdfg.WriteFile("testdata/TestSetFooter.pdf") if err != nil { t.Fatal(err) } @@ -270,7 +270,7 @@ func TestPDFGenerator_SetOutput(t *testing.T) { t.Fatal(err) } - htmlfile, err := os.Open("./testfiles/htmlsimple.html") + htmlfile, err := os.Open("testdata/htmlsimple.html") if err != nil { t.Fatal(err) } @@ -304,7 +304,7 @@ func TestPDFGenerator_SetStderr(t *testing.T) { t.Fatal(err) } - htmlfile, err := os.Open("./testfiles/htmlsimple.html") + htmlfile, err := os.Open("testdata/htmlsimple.html") if err != nil { t.Fatal(err) } @@ -337,7 +337,7 @@ func TestTOCAndCustomFooter(t *testing.T) { t.Fatal(err) } - htmlfile, err := os.Open("./testfiles/html5.html") + htmlfile, err := os.Open("testdata/html5.html") if err != nil { t.Fatal(err) } @@ -346,13 +346,13 @@ func TestTOCAndCustomFooter(t *testing.T) { page.EnableLocalFileAccess.Set(true) //needed to include js pdfg.AddPage(page) - page.FooterHTML.Set("./testfiles/footer.html") + page.FooterHTML.Set("testdata/footer.html") page.FooterSpacing.Set(8) - pdfg.TOC.XslStyleSheet.Set("./testfiles/toc.xls") + pdfg.TOC.XslStyleSheet.Set("testdata/toc.xls") pdfg.TOC.Include = true pdfg.TOC.EnableLocalFileAccess.Set(true) //needed to include js - pdfg.TOC.FooterHTML.Set("./testfiles/footer-toc.html") + pdfg.TOC.FooterHTML.Set("testdata/footer-toc.html") pdfg.TOC.FooterSpacing.Set(8) err = pdfg.Create() @@ -361,7 +361,7 @@ func TestTOCAndCustomFooter(t *testing.T) { } // Write buffer contents to file on disk - err = pdfg.WriteFile("./testfiles/TestGeneratePdfTOCAndCustomFooter.pdf") + err = pdfg.WriteFile("testdata/TestGeneratePdfTOCAndCustomFooter.pdf") if err != nil { t.Fatal(err) }