-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_test.go
52 lines (40 loc) · 963 Bytes
/
echo_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
package traefik_echoserver
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestEchoServer(t *testing.T) {
cfg := CreateConfig()
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
handler, err := New(ctx, next, cfg, "echo-server")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
reqUrl, err := url.JoinPath("http://localhost:8080", cfg.Path)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, nil)
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(recorder, req)
resp := recorder.Result()
body, _ := io.ReadAll(resp.Body)
t.Log(resp.Header)
t.Log(string(body))
if recorder.Code != 200 {
t.Errorf("code: %d", recorder.Code)
}
host := strings.Split(strings.Split(string(body), "\n")[0], "=")[1]
if host != "localhost" {
t.Errorf("host: %s", host)
}
}