-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
149 lines (124 loc) · 3.35 KB
/
Copy pathintegration_test.go
File metadata and controls
149 lines (124 loc) · 3.35 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
package main
import (
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
)
type recordedReq struct {
Method string
Path string
Header http.Header
Body string
}
func TestIntegration(t *testing.T) {
var mu sync.Mutex
var lastReq recordedReq
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
bodyBytes, _ := io.ReadAll(r.Body)
lastReq = recordedReq{
Method: r.Method,
Path: r.URL.Path,
Header: r.Header,
Body: string(bodyBytes),
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status": "ok"}`))
}))
defer server.Close()
baseAddr = server.URL
gLabel = "/api/v1/users"
// Clear headers
gHeaders = make(map[string]string)
// 1. GET requests
t.Run("GET request with no subpath", func(t *testing.T) {
makeRequest("GET", []string{"get"})
mu.Lock()
req := lastReq
mu.Unlock()
if req.Method != "GET" {
t.Errorf("expected GET, got %s", req.Method)
}
if req.Path != "/api/v1/users" {
t.Errorf("expected /api/v1/users, got %s", req.Path)
}
})
t.Run("GET request with subpath", func(t *testing.T) {
makeRequest("GET", []string{"get", "info"})
mu.Lock()
req := lastReq
mu.Unlock()
if req.Path != "/api/v1/users/info" {
t.Errorf("expected /api/v1/users/info, got %s", req.Path)
}
})
// 2. Custom headers
t.Run("Header commands", func(t *testing.T) {
// Set header
handleHeaderCommand([]string{"set", "header", "X-Test-Header", "HelloHeader"})
if gHeaders["X-Test-Header"] != "HelloHeader" {
t.Errorf("expected header to be set")
}
// Make a GET request and verify header is sent
makeRequest("GET", []string{"get"})
mu.Lock()
req := lastReq
mu.Unlock()
if req.Header.Get("X-Test-Header") != "HelloHeader" {
t.Errorf("expected header X-Test-Header to be HelloHeader, got %s", req.Header.Get("X-Test-Header"))
}
// Clear header
handleClearCommand([]string{"clear", "header", "X-Test-Header"})
if _, ok := gHeaders["X-Test-Header"]; ok {
t.Errorf("expected header to be cleared")
}
})
// 3. POST request with content
t.Run("POST request with inline content", func(t *testing.T) {
makeRequest("POST", []string{"post", "-c", `{"name":"test"}`})
mu.Lock()
req := lastReq
mu.Unlock()
if req.Method != "POST" {
t.Errorf("expected POST, got %s", req.Method)
}
if req.Body != `{"name":"test"}` {
t.Errorf("expected request body to match, got %s", req.Body)
}
if req.Header.Get("Content-Type") != "application/json" {
t.Errorf("expected Content-Type application/json, got %s", req.Header.Get("Content-Type"))
}
})
// 4. PUT/PATCH/DELETE/HEAD/OPTIONS requests
t.Run("PUT request", func(t *testing.T) {
makeRequest("PUT", []string{"put", "-c", "put-body"})
mu.Lock()
req := lastReq
mu.Unlock()
if req.Method != "PUT" {
t.Errorf("expected PUT, got %s", req.Method)
}
})
t.Run("DELETE request", func(t *testing.T) {
makeRequest("DELETE", []string{"delete"})
mu.Lock()
req := lastReq
mu.Unlock()
if req.Method != "DELETE" {
t.Errorf("expected DELETE, got %s", req.Method)
}
})
t.Run("OPTIONS request", func(t *testing.T) {
makeRequest("OPTIONS", []string{"options"})
mu.Lock()
req := lastReq
mu.Unlock()
if req.Method != "OPTIONS" {
t.Errorf("expected OPTIONS, got %s", req.Method)
}
})
}