forked from argoproj/argo-rollouts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_static_test.go
113 lines (103 loc) · 2.99 KB
/
server_static_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
package server
import (
"embed"
"io"
"mime"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/tj/assert"
)
const TestRootPath = "/test-root"
var (
//go:embed static_test/*
staticTestData embed.FS //nolint
mockServer ArgoRolloutsServer
)
func init() {
static = staticTestData
staticBasePath = "static_test"
indexHtmlFile = staticBasePath + "/index.html"
mockServer = mockArgoRolloutServer()
}
func TestIndexHtmlIsServed(t *testing.T) {
tests := []struct {
requestPath string
}{
{TestRootPath + "/"},
{TestRootPath + "/index.html"},
{TestRootPath + "/nonsense/../index.html"},
{TestRootPath + "/test-dir/test.css"},
}
for _, test := range tests {
t.Run(test.requestPath, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, test.requestPath, nil)
w := httptest.NewRecorder()
mockServer.staticFileHttpHandler(w, req)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, res.StatusCode, http.StatusOK)
if strings.HasSuffix(test.requestPath, ".css") {
assert.Equal(t, res.Header.Get(ContentType), mime.TypeByExtension(".css"))
assert.Contains(t, string(data), "empty by intent")
} else {
assert.Equal(t, res.Header.Get(ContentType), mime.TypeByExtension(".html"))
assert.Contains(t, string(data), "<title>index-title</title>")
}
})
}
}
func TestWhenFileNotFoundSendIndexPageForUiReactRouter(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, TestRootPath+"/namespace-default", nil)
w := httptest.NewRecorder()
mockServer.staticFileHttpHandler(w, req)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, res.StatusCode, http.StatusOK)
assert.Contains(t, string(data), "<title>index-title</title>")
}
func TestSlashWillBeRedirectedToRootPath(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
mockServer.staticFileHttpHandler(w, req)
res := w.Result()
defer res.Body.Close()
_, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, res.StatusCode, http.StatusFound)
assert.Contains(t, res.Header.Get("Location"), TestRootPath)
}
func TestInvalidFilesOrHackingAttemptReturn404(t *testing.T) {
tests := []struct {
requestPath string
}{
{"/index.html"}, // should fail, because not prefixed with Option.RootPath
{"/etc/passwd"},
{TestRootPath + "/../etc/passwd"},
{TestRootPath + "/../../etc/passwd"},
{TestRootPath + "/../../../etc/passwd"},
}
for _, test := range tests {
t.Run(test.requestPath, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, test.requestPath, nil)
w := httptest.NewRecorder()
mockServer.staticFileHttpHandler(w, req)
res := w.Result()
defer res.Body.Close()
assert.Equal(t, res.StatusCode, http.StatusNotFound)
})
}
}
func mockArgoRolloutServer() ArgoRolloutsServer {
s := ArgoRolloutsServer{
Options: ServerOptions{
RootPath: TestRootPath,
},
}
return s
}