-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
178 lines (147 loc) · 4.04 KB
/
Copy pathintegration_test.go
File metadata and controls
178 lines (147 loc) · 4.04 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
package main
import (
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
)
type recordedReq struct {
Method string
Path string
RawQuery 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,
RawQuery: r.URL.RawQuery,
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)
}
})
t.Run("GET request with query params only", func(t *testing.T) {
makeRequest("GET", []string{"get", "?limit=5"})
mu.Lock()
req := lastReq
mu.Unlock()
if req.Path != "/api/v1/users" {
t.Errorf("expected /api/v1/users, got %s", req.Path)
}
if req.RawQuery != "limit=5" {
t.Errorf("expected limit=5, got %s", req.RawQuery)
}
})
t.Run("GET request with subpath and query params", func(t *testing.T) {
makeRequest("GET", []string{"get", "info?limit=10"})
mu.Lock()
req := lastReq
mu.Unlock()
if req.Path != "/api/v1/users/info" {
t.Errorf("expected /api/v1/users/info, got %s", req.Path)
}
if req.RawQuery != "limit=10" {
t.Errorf("expected limit=10, got %s", req.RawQuery)
}
})
// 2. Custom headers
t.Run("Header commands", func(t *testing.T) {
handleHeaderCommand([]string{"set", "header", "X-Test-Header", "HelloHeader"})
if gHeaders["X-Test-Header"] != "HelloHeader" {
t.Errorf("expected header to be set")
}
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"))
}
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)
}
})
}