-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
274 lines (214 loc) · 6.67 KB
/
proxy_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package natshttp
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"io"
"net"
"net/http"
"strconv"
"testing"
"time"
"github.com/nats-io/nats.go"
"golang.org/x/sync/errgroup"
"github.com/go-chi/chi/v5"
"github.com/go-http-utils/headers"
"github.com/stretchr/testify/assert"
)
func ExampleProxy_basic() {
// connect to NATS
conn, err := nats.Connect(nats.DefaultURL)
if err != nil {
panic(err)
}
// create a TCP listener
listener, err := net.Listen("tcp", "localhost:8080")
if err != nil {
panic(err)
}
// create a proxy which forwards requests to 'test.service.>' subject hierarchy
proxy := Proxy{
Subject: "test.service",
Listener: listener,
Transport: &Transport{
Conn: conn,
},
}
// create a context and an error group for running processes
ctx, cancel := context.WithCancel(context.Background())
eg := errgroup.Group{}
// start listening
eg.Go(func() error {
return proxy.Listen(ctx)
})
// wait 10 seconds then cancel the context
eg.Go(func() error {
<-time.After(10 * time.Second)
cancel()
return nil
})
// wait for the listener to complete
if err = eg.Wait(); err != nil {
panic(err)
}
}
func TestProxy_Head(t *testing.T) {
s := runBasicNatsServer(t)
defer shutdownNatsServer(t, s)
conn := client(t, s)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
routes := chi.NewRouter()
routes.Head("/head.test", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
listener, err := net.Listen("tcp", "127.0.0.1:0")
assert.Nil(t, err)
runProxy(t, routes, listener, conn, "", ctx)
baseUrl := fmt.Sprintf("http://%s/", listener.Addr().String())
resp, err := http.Head(baseUrl + "head.test")
assert.Nil(t, err)
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
assert.Equal(t, fmt.Sprintf("%d %s", http.StatusNoContent, http.StatusText(http.StatusNoContent)), resp.Status)
b, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, 0, len(b))
}
func TestProxy_Simple(t *testing.T) {
s := runBasicNatsServer(t)
defer shutdownNatsServer(t, s)
conn := client(t, s)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
body := "Hello World"
routes := chi.NewRouter()
handler := func(w http.ResponseWriter, req *http.Request) {
var err error
switch req.Method {
case http.MethodGet:
_, err = io.WriteString(w, body)
case http.MethodPost, http.MethodPut:
_, err = io.Copy(w, req.Body)
default:
// do nothing
}
assert.Nil(t, err, "echo handler failed to copy request body into response writer")
}
routes.Get("/", handler)
routes.Post("/", handler)
listener, err := net.Listen("tcp", "127.0.0.1:0")
assert.Nil(t, err)
runProxy(t, routes, listener, conn, "", ctx)
baseUrl := fmt.Sprintf("http://%s/", listener.Addr().String())
t.Run("GET", func(t *testing.T) {
// Get
resp, err := http.Get(baseUrl)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, fmt.Sprintf("%d %s", http.StatusOK, http.StatusText(http.StatusOK)), resp.Status)
assert.Equal(t, len(body), int(resp.ContentLength))
b, err := io.ReadAll(io.NopCloser(resp.Body))
assert.Nil(t, err)
assert.Equal(t, body, string(b))
})
t.Run("POST", func(t *testing.T) {
// Get
body = "Ping Pong"
bodyReader := io.NopCloser(bytes.NewReader([]byte(body)))
resp, err := http.Post(baseUrl, "text/plain", bodyReader)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, fmt.Sprintf("%d %s", http.StatusOK, http.StatusText(http.StatusOK)), resp.Status)
assert.Equal(t, len(body), int(resp.ContentLength))
b, err := io.ReadAll(io.NopCloser(resp.Body))
assert.Nil(t, err)
assert.Equal(t, body, string(b))
})
}
func TestProxy_Chunked(t *testing.T) {
s := runBasicNatsServer(t)
defer shutdownNatsServer(t, s)
conn := client(t, s)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
routes := chi.NewRouter()
listener, err := net.Listen("tcp", "127.0.0.1:0")
assert.Nil(t, err)
runProxy(t, routes, listener, conn, "", ctx)
baseUrl := fmt.Sprintf("http://%s", listener.Addr().String())
t.Run("GET", func(t *testing.T) {
body := make([]byte, conn.MaxPayload()*2)
n, err := rand.Read(body)
assert.Nil(t, err)
assert.Equal(t, len(body), n)
routes.Get("/auto", func(w http.ResponseWriter, r *http.Request) {
_, err = io.Copy(w, bytes.NewReader(body))
assert.Nil(t, err)
})
routes.Get("/chunked", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Transfer-Encoding", "chunked")
_, err = io.Copy(w, bytes.NewReader(body))
assert.Nil(t, err)
})
routes.Get("/content-length", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(headers.ContentLength, strconv.Itoa(len(body)))
_, err = io.Copy(w, bytes.NewReader(body))
assert.Nil(t, err)
})
paths := []string{"auto", "chunked", "content-length"}
for _, path := range paths {
t.Run(path, func(t *testing.T) {
resp, err := http.Get(baseUrl + "/" + path)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, fmt.Sprintf("%d %s", http.StatusOK, http.StatusText(http.StatusOK)), resp.Status)
switch path {
case "auto":
assert.Equal(t, int64(-1), resp.ContentLength)
assert.Equal(t, []string{"chunked"}, resp.TransferEncoding)
case "chunked":
assert.Equal(t, int64(-1), resp.ContentLength)
assert.Equal(t, []string{"chunked"}, resp.TransferEncoding)
case "set-length":
assert.Equal(t, len(body), resp.ContentLength)
assert.Nil(t, resp.TransferEncoding)
}
b, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, len(body), len(b))
// assert.Equal(t, body, b)
})
}
})
t.Run("POST", func(t *testing.T) {
body := make([]byte, conn.MaxPayload()*10)
n, err := rand.Read(body)
routes.Post("/", func(w http.ResponseWriter, r *http.Request) {
reqBody, err := io.ReadAll(r.Body)
assert.Nil(t, err)
for idx := 0; idx < len(reqBody); idx++ {
if body[idx] != reqBody[idx] {
println()
}
}
assert.Equal(t, len(reqBody), len(body))
assert.Equal(t, reqBody, body)
// echo back what we received
n, err := w.Write(reqBody)
assert.Nil(t, err)
assert.Equal(t, len(reqBody), n)
})
assert.Nil(t, err)
assert.Equal(t, len(body), n)
bodyReader := io.NopCloser(bytes.NewReader(body))
resp, err := http.Post(baseUrl, "", bodyReader)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, fmt.Sprintf("%d %s", http.StatusOK, http.StatusText(http.StatusOK)), resp.Status)
b, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, len(body), len(b))
assert.Equal(t, body, b)
})
}