forked from thecodingmachine/gotenberg-go-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
metadata_test.go
76 lines (65 loc) · 1.9 KB
/
metadata_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gotenberg
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/runatal/gotenberg-go-client/v8/document"
"github.com/runatal/gotenberg-go-client/v8/test"
)
func TestReadWriteMetadata(t *testing.T) {
c, err := NewClient("http://localhost:3000", &http.Client{})
require.NoError(t, err)
// Writing metadata.
pdf1, err := document.FromPath("gotenberg1.pdf", test.PDFTestFilePath(t, "gotenberg.pdf"))
require.NoError(t, err)
reqWrite := NewWriteMetadataRequest(pdf1)
reqWrite.Trace("testWriteMetadata")
reqWrite.UseBasicAuth("foo", "bar")
reqWrite.OutputFilename("foo.pdf")
writeDataStruct := struct {
Author string `json:"Author"`
Copyright string `json:"Copyright"`
}{
Author: "Alexander Pikeev",
Copyright: "Alexander Pikeev",
}
writeData, err := json.Marshal(writeDataStruct)
require.NoError(t, err)
reqWrite.Metadata(writeData)
dirPath, err := test.Rand()
require.NoError(t, err)
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
err = c.Store(context.Background(), reqWrite, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
// Reading metadata.
pdf2, err := document.FromPath("foo.pdf", dest)
require.NoError(t, err)
reqRead := NewReadMetadataRequest(pdf2)
reqRead.Trace("testReadMetadata")
reqRead.UseBasicAuth("foo", "bar")
reqRead.OutputFilename("foo.pdf")
respRead, err := c.Send(context.Background(), reqRead)
require.NoError(t, err)
assert.Equal(t, 200, respRead.StatusCode)
var readData exifData
err = json.NewDecoder(respRead.Body).Decode(&readData)
require.NoError(t, err)
expected := exifData{
FooPdf: writeDataStruct,
}
assert.Equal(t, expected, readData)
err = os.RemoveAll(dirPath)
require.NoError(t, err)
}
type exifData struct {
FooPdf struct {
Author string `json:"Author"`
Copyright string `json:"Copyright"`
} `json:"foo.pdf"`
}