generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
56 lines (51 loc) · 1.05 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
package main
import (
"fmt"
"net/http"
"net/netip"
"testing"
)
func TestExtAuthz(t *testing.T) {
var config IpAuthConfig
prefix, _ := netip.ParsePrefix("2.57.3.0/24")
block := []netip.Prefix{prefix}
server := NewExtAuthzServer(config, block)
go server.run("localhost:0")
httpClient := &http.Client{}
httpReq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:%d/check", <-server.httpPort), nil)
if err != nil {
t.Fatalf(err.Error())
}
cases := []struct {
name string
ip string
want int
}{
{
name: "HTTP-allow",
ip: "10.10.0.0",
want: http.StatusOK,
},
{
name: "HTTP-deny",
ip: "2.57.3.5",
want: http.StatusForbidden,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var got int
httpReq.Header.Set("x-envoy-external-address", tc.ip)
resp, err := httpClient.Do(httpReq)
if err != nil {
t.Errorf(err.Error())
} else {
got = resp.StatusCode
resp.Body.Close()
}
if got != tc.want {
t.Errorf("want %d but got %d", tc.want, got)
}
})
}
}