1 package httpretty
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 func TestIsBinary(t *testing.T) {
9 testCases := []struct {
10 desc string
11 data []byte
12 binary bool
13 }{
14 {
15 desc: "Empty",
16 binary: false,
17 },
18 {
19 desc: "Text",
20 data: []byte("plain text"),
21 binary: false,
22 },
23 {
24 desc: "More text",
25 data: []byte("plain text\n"),
26 binary: false,
27 },
28 {
29 desc: "Text with UTF16 Big Endian BOM",
30 data: []byte("\xFE\xFFevil plain text"),
31 binary: false,
32 },
33 {
34 desc: "Text with UTF16 Little Endian BOM",
35 data: []byte("\xFF\xFEevil plain text"),
36 binary: false,
37 },
38 {
39 desc: "Text with UTF8 BOM",
40 data: []byte("\xEF\xBB\xBFevil plain text"),
41 binary: false,
42 },
43 {
44 desc: "Binary",
45 data: []byte{1, 2, 3},
46 binary: true,
47 },
48 {
49 desc: "Binary over 512bytes",
50 data: bytes.Repeat([]byte{1, 2, 3, 4, 5, 6, 7, 8}, 65),
51 binary: true,
52 },
53 {
54 desc: "JPEG image",
55 data: []byte("\xFF\xD8\xFF"),
56 binary: true,
57 },
58 {
59 desc: "AVI video",
60 data: []byte("RIFF,O\n\x00AVI LISTÀ"),
61 binary: true,
62 },
63 {
64 desc: "RAR",
65 data: []byte("Rar!\x1A\x07\x00"),
66 binary: true,
67 },
68 {
69 desc: "PDF",
70 data: []byte("\x25\x50\x44\x46\x2d\x31\x2e\x33\x0a\x25\xc4\xe5\xf2\xe5\xeb\xa7"),
71 binary: true,
72 },
73 }
74 for _, tc := range testCases {
75 t.Run(tc.desc, func(t *testing.T) {
76 if got := isBinary(tc.data); got != tc.binary {
77 t.Errorf("wanted isBinary(%v) = %v, got %v instead", tc.data, tc.binary, got)
78 }
79 })
80 }
81 }
82
View as plain text