-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
198 lines (177 loc) · 5.22 KB
/
Copy pathmain_test.go
File metadata and controls
198 lines (177 loc) · 5.22 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"testing"
)
// silence swaps os.Stdout and os.Stderr for the duration of fn so CLI output
// doesn't clutter the test log, returning everything written to either.
func silence(t *testing.T, fn func()) string {
t.Helper()
oldOut, oldErr := os.Stdout, os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
os.Stdout, os.Stderr = w, w
done := make(chan string, 1)
go func() {
var b bytes.Buffer
io.Copy(&b, r)
done <- b.String()
}()
fn()
w.Close()
os.Stdout, os.Stderr = oldOut, oldErr
return <-done
}
func writeFile(t *testing.T, path, content string) {
t.Helper()
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func TestRunVersion(t *testing.T) {
var code int
out := silence(t, func() { code = run([]string{"--version"}) })
if code != 0 {
t.Errorf("exit = %d, want 0", code)
}
if !strings.Contains(out, "httpsuite") {
t.Errorf("version output = %q", out)
}
}
// TestRunExitCodes verifies the 0-pass / 1-fail contract for a single file.
func TestRunExitCodes(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/ok" {
w.WriteHeader(200)
return
}
w.WriteHeader(500)
}))
defer srv.Close()
dir := t.TempDir()
pass := filepath.Join(dir, "ok.http")
writeFile(t, pass, "GET "+srv.URL+"/ok\n# @expect status == 200\n")
fail := filepath.Join(dir, "bad.http")
writeFile(t, fail, "GET "+srv.URL+"/bad\n# @expect status == 200\n")
var passCode, failCode int
silence(t, func() { passCode = run([]string{pass}) })
silence(t, func() { failCode = run([]string{fail}) })
if passCode != 0 {
t.Errorf("passing file exit = %d, want 0", passCode)
}
if failCode != 1 {
t.Errorf("failing file exit = %d, want 1", failCode)
}
}
func TestRunMissingPath(t *testing.T) {
var code int
silence(t, func() { code = run([]string{filepath.Join(t.TempDir(), "nope.http")}) })
if code != 2 {
t.Errorf("missing path exit = %d, want 2 (setup error)", code)
}
}
func TestRunInvalidVarFlag(t *testing.T) {
// A --var without '=' fails flag parsing → exit 2 (usage error).
var code int
silence(t, func() { code = run([]string{"--var", "novalue", "."}) })
if code != 2 {
t.Errorf("bad --var exit = %d, want 2", code)
}
}
func TestRunVarOverride(t *testing.T) {
var mu sync.Mutex
var gotID string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
gotID = r.URL.Query().Get("id")
mu.Unlock()
w.WriteHeader(200)
}))
defer srv.Close()
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "u.http"), "GET "+srv.URL+"/u?id={{id}}\n# @expect status == 200\n")
var code int
silence(t, func() { code = run([]string{"--var", "id=42", filepath.Join(dir, "u.http")}) })
if code != 0 {
t.Errorf("exit = %d, want 0", code)
}
mu.Lock()
defer mu.Unlock()
if gotID != "42" {
t.Errorf("server saw id=%q, want 42 (--var override)", gotID)
}
}
func TestRunReportWritesJUnit(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
defer srv.Close()
dir := t.TempDir()
file := filepath.Join(dir, "a.http")
writeFile(t, file, "GET "+srv.URL+"/a\n# @expect status == 200\n")
report := filepath.Join(dir, "report.xml")
var code int
silence(t, func() { code = run([]string{"--report", report, file}) })
if code != 0 {
t.Fatalf("exit = %d, want 0", code)
}
data, err := os.ReadFile(report)
if err != nil {
t.Fatalf("report not written: %v", err)
}
if !strings.Contains(string(data), "<testsuite") {
t.Errorf("report is not JUnit XML:\n%s", data)
}
}
// TestRunDiscoversSuiteYaml verifies a directory with httpsuite.yaml runs via
// the suite definition.
func TestRunDiscoversSuiteYaml(t *testing.T) {
var hits int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&hits, 1)
w.WriteHeader(200)
}))
defer srv.Close()
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.http"), "GET "+srv.URL+"/a\n# @expect status == 200\n")
writeFile(t, filepath.Join(dir, "httpsuite.yaml"), "groups:\n - name: g\n files: [a.http]\n")
var code int
silence(t, func() { code = run([]string{dir}) })
if code != 0 {
t.Errorf("suite exit = %d, want 0", code)
}
if got := atomic.LoadInt32(&hits); got != 1 {
t.Errorf("server hit %d times, want 1", got)
}
}
// TestRunDiscoversGlob verifies a directory with no httpsuite.yaml runs every
// *.http file in sorted order.
func TestRunDiscoversGlob(t *testing.T) {
var hits int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&hits, 1)
w.WriteHeader(200)
}))
defer srv.Close()
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.http"), "GET "+srv.URL+"/a\n# @expect status == 200\n")
writeFile(t, filepath.Join(dir, "b.http"), "GET "+srv.URL+"/b\n# @expect status == 200\n")
var code int
silence(t, func() { code = run([]string{dir}) })
if code != 0 {
t.Errorf("glob exit = %d, want 0", code)
}
if got := atomic.LoadInt32(&hits); got != 2 {
t.Errorf("server hit %d times, want 2 (both files)", got)
}
}