-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
180 lines (160 loc) · 4.33 KB
/
main_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bytes"
"fmt"
"os"
"testing"
)
type Testfile struct {
name string
content string
file *os.File
}
var stdinSaved *os.File = os.Stdin
func Setup(contents []string, t *testing.T) []Testfile {
var err error
os.Stdin, err = os.CreateTemp("", "stdin")
if err != nil {
t.FailNow()
}
testfiles := make([]Testfile, len(contents))
for i, c := range contents {
testfiles[i] = Testfile{content: c}
file, err := os.CreateTemp("", fmt.Sprintf("tf%d", i))
if err != nil {
t.Logf("unable to create tempfile for '%s'", c)
t.FailNow()
}
_, err = file.Write([]byte(c))
if err != nil {
t.Logf("unable to write tempfile with '%s'", c)
t.FailNow()
}
err = file.Close()
if err != nil {
t.Logf("unable to close tempfile for '%s'", c)
t.FailNow()
}
testfiles[i].file = file
testfiles[i].name = file.Name()
}
return testfiles
}
func TearDown(testfiles []Testfile) {
for _, tf := range testfiles {
_ = os.Remove(tf.name)
}
_ = os.Stdin.Close()
os.Stdin = stdinSaved
}
func assertEqual(expect []byte, actual []byte, t *testing.T) {
if bytes.Compare(expect, actual) != 0 {
t.Errorf("bytes did not match:\n. expected:\n %s\n actual:\n %s\n",
string(expect), string(actual))
}
}
func TestReadArguments(t *testing.T) {
files := Setup([]string{"1", "2", "{\"foo\": true, \"bar\": false}"}, t)
defer TearDown(files)
tests := [][]Testfile{
{}, // nothing
{Testfile{content: "{\"hello\":\"world\"}"}},
{Testfile{content: "["}, files[0], Testfile{content: ","}, files[1], Testfile{content: "]"}},
{files[2]},
}
for i, test := range tests {
t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
expected := bytes.NewBuffer(nil)
args := make([]string, len(test))
for j, testfile := range test {
if testfile.name != "" {
args[j] = fmt.Sprintf("@%s", testfile.name)
} else {
args[j] = testfile.content
}
expected.WriteString(testfile.content)
}
expect := expected.Bytes()
actual := readArguments(args)
assertEqual(expect, actual.Bytes(), t)
})
}
}
func TestReadArgumentsStdin(t *testing.T) {
f := Setup([]string{}, t)
defer TearDown(f)
expect := []byte("{\"foo\": true, \"bar\": false}")
_, err := os.Stdin.Write(expect)
if err != nil {
t.Logf("unable to write to stdin\n")
t.FailNow()
}
_ = os.Stdin.Close()
reopen := func(t *testing.T) {
f, err := os.Open(os.Stdin.Name())
if err != nil {
t.Logf("unable to reopen stdin")
t.FailNow()
}
os.Stdin = f
}
t.Run("no args", func(t *testing.T) {
reopen(t)
actual := readArguments([]string{})
assertEqual(expect, actual.Bytes(), t)
})
t.Run("-", func(t *testing.T) {
reopen(t)
actual := readArguments([]string{"-"})
assertEqual(expect, actual.Bytes(), t)
})
t.Run("@-", func(t *testing.T) {
reopen(t)
actual := readArguments([]string{"@-"})
assertEqual(expect, actual.Bytes(), t)
})
}
func TestParseHeaders(t *testing.T) {
tests := [][]string{
{"", "", ""},
{"{}", "", "{}"},
{"not http\n\nhello\nworld", "", "not http\n\nhello\nworld"},
{"HTTP/1.1 200 OK\nContent-Type: application/json\nContent-Length: 19\n\n{\"hello\":\"world\"}\n",
"HTTP/1.1 200 OK\nContent-Type: application/json\nContent-Length: 19\n\n",
"{\"hello\":\"world\"}\n"},
{"HTTP/1.1 200 OK\nContent-Type: application/json\nContent-Length: 0\n\n",
"HTTP/1.1 200 OK\nContent-Type: application/json\nContent-Length: 0\n\n",
""},
{"myfunc(1,2,3)", "", "myfunc(1,2,3)"},
}
for i, test := range tests {
t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
buffer := bytes.NewBuffer(nil)
buffer.WriteString(test[0])
headers, jsonsrc, err := parseHeaders(buffer)
if err != nil {
t.Logf("error parseHeaders: %v", err)
t.FailNow()
}
assertEqual([]byte(test[1]), headers, t)
assertEqual([]byte(test[2]), jsonsrc, t)
})
}
}
func TestCleanJsonp(t *testing.T) {
tests := [][]string{
{"jsonp(true)", "true", "jsonp"},
{"true", "true", ""},
{"myJSFunc({\"hello\":\"world\"})", "{\"hello\":\"world\"}", "myJSFunc"},
{"[1,2,3]", "[1,2,3]", ""},
{"myfunc(1,2,3)", "1,2,3", "myfunc"},
{"myfunc(1,2,3)\n", "1,2,3", "myfunc"},
}
for i, test := range tests {
t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
jsonSrc, jsonpName := cleanJsonp([]byte(test[0]))
assertEqual([]byte(test[1]), jsonSrc, t)
assertEqual([]byte(test[2]), []byte(jsonpName), t)
})
}
}