-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
59 lines (52 loc) · 1.26 KB
/
main_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"testing"
)
func Test_Example(t *testing.T) {
tests := []struct {
name string
url string
expected string
contentType string
}{
{
name: "Must proxy",
url: "/oauth2/v3/certs",
expected: "true",
contentType: "application/json; charset=UTF-8",
},
{
name: "Must not proxy",
url: "/hello",
expected: "false",
contentType: "text/plain; charset=utf-8",
},
}
go main()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "http://localhost:8080"+tt.url, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if resp.Header.Get("x-proxy") != tt.expected {
t.Errorf(`unexpected value for "x-proxy", got: %q`, resp.Header.Get("content-type"))
}
if resp.Header.Get("content-type") != tt.contentType {
t.Errorf(`unexpected value for "content-type", got: %q`, resp.Header.Get("content-type"))
}
if t.Failed() {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Reponse body: %s", body)
}
})
}
}