-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_test.go
More file actions
76 lines (53 loc) · 1.7 KB
/
Copy pathbinary_test.go
File metadata and controls
76 lines (53 loc) · 1.7 KB
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
// Copyright 2026 The Nanoninja Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package render
import (
"bytes"
"context"
"strings"
"testing"
"time"
"github.com/nanoninja/assert"
)
var (
_ Renderer = (*binaryRenderer)(nil)
_ Renderer = Binary()
)
func TestBinaryRenderer(t *testing.T) {
t.Run("RendersByteSlice", func(t *testing.T) {
var w bytes.Buffer
data := []byte{0x89, 0x50, 0x4E, 0x47}
err := Binary().Render(context.Background(), &w, data, NoOptions)
assert.NoError(t, err)
assert.Equal(t, data, w.Bytes())
})
t.Run("RendersReader", func(t *testing.T) {
var w bytes.Buffer
data := strings.NewReader("binary content")
err := Binary().Render(context.Background(), &w, data, NoOptions)
assert.NoError(t, err)
assert.Equal(t, "binary content", w.String())
})
t.Run("ReturnsErrorForUnsupportedType", func(t *testing.T) {
var w bytes.Buffer
err := Binary().Render(context.Background(), &w, 42, NoOptions)
assert.Error(t, err)
assert.StringContains(t, err.Error(), "binary: unsupported data type")
})
t.Run("SetsDefaultContentType", func(t *testing.T) {
assert.Equal(t, "application/octet-stream", Binary().ContentType())
})
t.Run("RespectsContextCancellation", func(t *testing.T) {
var w bytes.Buffer
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := Binary().Render(ctx, &w, []byte("data"), NoOptions)
assert.ErrorIs(t, err, context.Canceled)
})
t.Run("RespectsTimeout", func(t *testing.T) {
var w bytes.Buffer
err := Binary().Render(context.Background(), &w, []byte("data"), Options{Timeout: 1 * time.Hour})
assert.NoError(t, err)
})
}