This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
117 lines (106 loc) · 2.63 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
110
111
112
113
114
115
116
117
// 🚀 Fiber is an Express inspired web framework written in Go with 💖
// 📌 API Documentation: https://fiber.wiki
// 📝 Github Repository: https://github.com/gofiber/fiber
// 🙏 Thanks & Credits to @arsmn
package embed
import (
"net/http"
"testing"
"github.com/gofiber/fiber"
)
func Test_Embed(t *testing.T) {
app := *fiber.New()
app.Use("/test", New(Config{
Root: http.Dir("./testdata"),
}))
app.Use("/dir", New(Config{
Root: http.Dir("./testdata"),
Browse: true,
}))
app.Get("/", func(c *fiber.Ctx) {
c.SendString("Hello, World!")
})
tests := []struct {
name string
url string
statusCode int
contentType string
modifiedTime string
}{
{
name: "Should be returns status 200 with suitable content-type",
url: "/test/index.html",
statusCode: 200,
contentType: "text/html",
},
{
name: "Should be returns status 200 with suitable content-type",
url: "/test",
statusCode: 200,
contentType: "text/html",
},
{
name: "Should be returns status 200 with suitable content-type",
url: "/test/test.json",
statusCode: 200,
contentType: "application/json",
},
{
name: "Should be returns status 200 with suitable content-type",
url: "/test/main.css",
statusCode: 200,
contentType: "text/css",
},
{
name: "Should be returns status 404",
url: "/test/nofile.js",
statusCode: 404,
},
{
name: "Should be returns status 404",
url: "/test/nofile",
statusCode: 404,
},
{
name: "Should be returns status 200",
url: "/",
statusCode: 200,
contentType: "text/plain; charset=utf-8",
},
{
name: "Should be returns status 403",
url: "/test/inner",
statusCode: 403,
},
{
name: "Should list the directory contents",
url: "/dir/inner",
statusCode: 200,
contentType: "text/html",
},
{
name: "Should be returns status 200",
url: "/dir/inner/fiber.png",
statusCode: 200,
contentType: "image/png",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", tt.url, nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
if resp.StatusCode != tt.statusCode {
t.Fatalf(`%s: StatusCode: got %v - expected %v`, t.Name(), resp.StatusCode, tt.statusCode)
}
if tt.contentType != "" {
ct := resp.Header.Get("Content-Type")
if ct != tt.contentType {
t.Fatalf(`%s: Content-Type: got %s - expected %s`, t.Name(), ct, tt.contentType)
}
}
})
}
}