-
Notifications
You must be signed in to change notification settings - Fork 0
/
goztl_test.go
58 lines (48 loc) · 1.38 KB
/
goztl_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
package goztl
import (
"io"
"net/http"
"net/http/httptest"
"testing"
)
const (
testToken = "TOKEN"
)
func setup() (client *Client, mux *http.ServeMux, teardown func()) {
// mux is the HTTP request multiplexer used with the test server.
mux = http.NewServeMux()
// server is a test HTTP server used to provide mock API responses.
server := httptest.NewServer(mux)
// client is the Zentral client being tested and is configured
// to use the test server.
client, _ = NewClient(nil, server.URL, testToken)
return client, mux, server.Close
}
func testBody(t *testing.T, r *http.Request, want string) {
t.Helper()
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
if got := string(b); got != want {
t.Errorf("request Body is %s, want %s", got, want)
}
}
func testHeader(t *testing.T, r *http.Request, header string, want string) {
t.Helper()
if got := r.Header.Get(header); got != want {
t.Errorf("Header.Get(%q) returned %q, want %q", header, got, want)
}
}
func testMethod(t *testing.T, r *http.Request, want string) {
t.Helper()
if got := r.Method; got != want {
t.Errorf("Request method: %v, want %v", got, want)
}
}
func testQueryArg(t *testing.T, r *http.Request, arg string, want string) {
t.Helper()
if got := r.URL.Query().Get(arg); got != want {
t.Errorf("Request query arg %q: value %q, want %q", arg, got, want)
}
}