Skip to content

Commit

Permalink
feat: headers as a map instead of marshaled json bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Pikeev committed Dec 1, 2024
1 parent 5fea96c commit f8c937a
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build/tests/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -xe

# Testing Go client.
gotenberg --api-enable-basic-auth &
gotenberg --api-enable-basic-auth --chromium-auto-start &
sleep 10
export CGO_ENABLED=1
go test -race -cover -covermode=atomic github.com/runatal/gotenberg-go-client/v8
Expand Down
205 changes: 205 additions & 0 deletions document/document_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package document

import (
"bytes"
"io"
"os"
"strings"
"testing"
)

func TestFromPath(t *testing.T) {
t.Run("FileExists", func(t *testing.T) {
tmpFile, err := os.CreateTemp("", "testfile-*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer func(name string) {
_ = os.Remove(name)
}(tmpFile.Name())

doc, err := FromPath(tmpFile.Name(), tmpFile.Name())
if err != nil {
t.Fatalf("FromPath failed for existing file: %v", err)
}

if doc.Filename() != tmpFile.Name() {
t.Errorf("expected filename %s, got %s", tmpFile.Name(), doc.Filename())
}

reader, err := doc.Reader()
if err != nil {
t.Fatalf("Reader failed: %v", err)
}
defer func(reader io.ReadCloser) {
err = reader.Close()
}(reader)

if _, err = io.ReadAll(reader); err != nil {
t.Errorf("failed to read from reader: %v", err)
}
})

t.Run("FileDoesNotExists", func(t *testing.T) {
_, err := FromPath("nonexistent.txt", "/path/does/not/exist")
if err == nil {
t.Fatalf("expected error for nonexistent file, got nil")
}
})

t.Run("FileCannotBeOpened", func(t *testing.T) {
tmpFile, err := os.CreateTemp("", "testfile-*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer func(name string) {
_ = os.Remove(name)
}(tmpFile.Name())

_ = tmpFile.Close()
if err = os.Chmod(tmpFile.Name(), 0000); err != nil {
t.Fatalf("failed to change file permissions: %v", err)
}
defer func(name string, mode os.FileMode) {
_ = os.Chmod(name, mode)
}(tmpFile.Name(), 0600)

doc, err := FromPath(tmpFile.Name(), tmpFile.Name())
if err != nil {
t.Fatalf("FromPath failed for existing file: %v", err)
}

_, err = doc.Reader()
if err == nil {
t.Fatalf("expected error when opening file with no permissions, got nil")
}
})
}

func TestFromString(t *testing.T) {
t.Run("NonEmptyString", func(t *testing.T) {
data := "this is test content"
filename := "testfile.txt"

doc, err := FromString(filename, data)
if err != nil {
t.Fatalf("FromString failed for non-empty string: %v", err)
}

if doc.Filename() != filename {
t.Errorf("expected filename %s, got %s", filename, doc.Filename())
}

reader, err := doc.Reader()
if err != nil {
t.Fatalf("Reader failed: %v", err)
}
defer func(reader io.ReadCloser) {
_ = reader.Close()
}(reader)

readData, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("failed to read from reader: %v", err)
}

if string(readData) != data {
t.Errorf("expected data %q, got %q", data, string(readData))
}
})

t.Run("EmptyString", func(t *testing.T) {
filename := "emptyfile.txt"

_, err := FromString(filename, "")
if err == nil {
t.Fatalf("expected error for empty string, got nil")
}
})
}

func TestFromBytes(t *testing.T) {
t.Run("NonEmptyBytes", func(t *testing.T) {
data := []byte("this is test content")
filename := "testfile.txt"

doc, err := FromBytes(filename, data)
if err != nil {
t.Fatalf("FromBytes failed for non-empty bytes: %v", err)
}

if doc.Filename() != filename {
t.Errorf("expected filename %s, got %s", filename, doc.Filename())
}

reader, err := doc.Reader()
if err != nil {
t.Fatalf("Reader failed: %v", err)
}
defer func(reader io.ReadCloser) {
_ = reader.Close()
}(reader)

readData, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("failed to read from reader: %v", err)
}

if !bytes.Equal(readData, data) {
t.Errorf("expected data %q, got %q", data, readData)
}
})

t.Run("EmptyBytes", func(t *testing.T) {
filename := "emptyfile.txt"

_, err := FromBytes(filename, []byte{})
if err == nil {
t.Fatalf("expected error for empty bytes, got nil")
}
})
}

func TestFromReader(t *testing.T) {
t.Run("ValidReader", func(t *testing.T) {
data := "this is test content"
filename := "testfile.txt"

reader := strings.NewReader(data)

doc, err := FromReader(filename, reader)
if err != nil {
t.Fatalf("FromReader failed for valid reader: %v", err)
}

if doc.Filename() != filename {
t.Errorf("expected filename %s, got %s", filename, doc.Filename())
}

readCloser, err := doc.Reader()
if err != nil {
t.Fatalf("Reader failed: %v", err)
}
defer func(readCloser io.ReadCloser) {
_ = readCloser.Close()
}(readCloser)

readData, err := io.ReadAll(readCloser)
if err != nil {
t.Fatalf("failed to read from reader: %v", err)
}

if string(readData) != data {
t.Errorf("expected data %q, got %q", data, string(readData))
}
})

t.Run("NilReader", func(t *testing.T) {
filename := "nilreader.txt"

_, err := FromReader(filename, nil)
if err == nil {
t.Fatalf("expected error for nil reader, got nil")
}
})
}

0 comments on commit f8c937a

Please sign in to comment.