-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
109 lines (91 loc) · 2.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
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
package fibervhost
import (
"github.com/gofiber/fiber/v2"
"net/http/httptest"
"testing"
)
// go test -run Test_Vhost_Match
func Test_Vhost_Match(t *testing.T) {
want := "example.com"
app := fiber.New()
app.Use(New(Config{
Hostname: want,
Handler: func(c *fiber.Ctx) error {
got := ToVhostStruct(c.Locals("vhost"))
if !(got.Host == want) {
t.Error("Error: incorrect match, host does not match hostname")
}
return nil
},
HostnameRegexpString: "",
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("test")
})
req := httptest.NewRequest("GET", "http://"+want, nil)
app.Test(req)
}
// go test -run Test_Vhost_No_Match
func Test_Vhost_No_Match(t *testing.T) {
want := "test.com"
app := fiber.New()
app.Use(New(Config{
Hostname: want,
Handler: func(c *fiber.Ctx) error {
t.Error("Error: match occurred with different host & hostname")
return nil
},
HostnameRegexpString: "",
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("test")
})
req := httptest.NewRequest("GET", "http://example.com", nil)
app.Test(req)
}
// go test -run Test_VHost_Next_Skip
func Test_VHost_Next_Skip(t *testing.T) {
want := "example.com"
app := fiber.New()
app.Use(New(Config{
Next: func(c *fiber.Ctx) bool {
if c.Get("X-test-skip") == "yes" {
return true
}
return false
},
Hostname: want,
Handler: func(c *fiber.Ctx) error {
t.Error("Error: did not skip when Next returned true")
return nil
},
HostnameRegexpString: "",
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("test")
})
req := httptest.NewRequest("GET", "http:"+want, nil)
req.Header.Add("X-test-skip", "yes")
app.Test(req)
}
func Test_Custom_Regexp(t *testing.T) {
want := "a.example.com"
regex_str := "([a-z].example.com)"
app := fiber.New()
app.Use(New(Config{
Hostname: want,
Handler: func(c *fiber.Ctx) error {
got := ToVhostStruct(c.Locals("vhost"))
if !(got.Host == want || got.HostnameRegexpString == regex_str) {
t.Error("Error: incorrect match, host does not match hostname")
}
return nil
},
HostnameRegexpString: regex_str,
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("test")
})
req := httptest.NewRequest("GET", "http://"+want, nil)
app.Test(req)
}