-
Notifications
You must be signed in to change notification settings - Fork 33
/
static_test.go
185 lines (161 loc) · 5.49 KB
/
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package aah
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"aahframe.work/ahttp"
"aahframe.work/internal/util"
"github.com/stretchr/testify/assert"
)
func TestStaticFilesDelivery(t *testing.T) {
importPath := filepath.Join(testdataBaseDir(), "webapp1")
ts := newTestServer(t, importPath)
defer ts.Close()
t.Logf("Test Server URL [Static Files Delivery]: %s", ts.URL)
httpClient := new(http.Client)
// Static File - /robots.txt
t.Log("Static File - /robots.txt")
resp, err := httpClient.Get(ts.URL + "/robots.txt")
assert.Nil(t, err)
assert.Equal(t, 200, resp.StatusCode)
assert.True(t, strings.Contains(responseBody(resp), "User-agent: *"))
assert.Equal(t, "no-cache, no-store, must-revalidate", resp.Header.Get(ahttp.HeaderCacheControl))
// Static File - /assets/css/aah.css
t.Log("Static File - /assets/css/aah.css")
resp, err = httpClient.Get(ts.URL + "/assets/css/aah.css")
assert.Nil(t, err)
assert.Equal(t, 200, resp.StatusCode)
assert.True(t, strings.Contains(responseBody(resp), "Minimal aah framework application template CSS."))
assert.Equal(t, "no-cache, no-store, must-revalidate", resp.Header.Get(ahttp.HeaderCacheControl))
// Directory Listing - /assets
t.Log("Directory Listing - /assets")
resp, err = httpClient.Get(ts.URL + "/assets")
assert.Nil(t, err)
assert.Equal(t, 200, resp.StatusCode)
body := responseBody(resp)
assert.True(t, strings.Contains(body, "<title>Listing of /assets/</title>"))
assert.True(t, strings.Contains(body, "<h1>Listing of /assets/</h1><hr>"))
assert.True(t, strings.Contains(body, `<a href="robots.txt">robots.txt</a>`))
assert.Equal(t, "", resp.Header.Get(ahttp.HeaderCacheControl))
// Static File - /assets/img/aah-framework-logo.png
t.Log("Static File - /assets/img/aah-framework-logo.png")
resp, err = httpClient.Get(ts.URL + "/assets/img/aah-framework-logo.png")
assert.Nil(t, err)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "image/png", resp.Header.Get(ahttp.HeaderContentType))
assert.Equal(t, "6990", resp.Header.Get(ahttp.HeaderContentLength))
assert.Equal(t, "no-cache, no-store, must-revalidate", resp.Header.Get(ahttp.HeaderCacheControl))
// Static File - /assets/img/notfound/file.txt
t.Log("Static File - /assets/img/notfound/file.txt")
resp, err = httpClient.Get(ts.URL + "/assets/img/notfound/file.txt")
assert.Nil(t, err)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "0", resp.Header.Get(ahttp.HeaderContentLength))
}
func TestStaticDetectContentType(t *testing.T) {
testcases := []struct {
label string
filename string
result string
}{
{
label: "svg",
filename: "image1.svg",
result: "image/svg+xml",
},
{
label: "png",
filename: "image2.png",
result: "image/png",
},
{
label: "jpg",
filename: "image3.jpg",
result: "image/jpeg",
},
{
label: "jpeg",
filename: "image4.jpeg",
result: "image/jpeg",
},
{
label: "pdf",
filename: "file.pdf",
result: "application/pdf",
},
{
label: "javascript",
filename: "file.js",
result: "application/javascript; charset=utf-8",
},
{
label: "txt",
filename: "file.txt",
result: "text/plain; charset=utf-8",
},
{
label: "xml",
filename: "file.xml",
result: "application/xml; charset=utf-8",
},
{
label: "css",
filename: "file.css",
result: "text/css; charset=utf-8",
},
{
label: "html",
filename: "file.html",
result: "text/html; charset=utf-8",
},
}
for _, tc := range testcases {
t.Run(tc.label, func(t *testing.T) {
v, _ := util.DetectFileContentType(tc.filename, nil)
assert.Equal(t, tc.result, v)
})
}
content, _ := ioutil.ReadFile(filepath.Join(testdataBaseDir(), "test-image.noext"))
v, _ := util.DetectFileContentType("test-image.noext", bytes.NewReader(content))
assert.Equal(t, "image/png", v)
}
func TestStaticCacheHeader(t *testing.T) {
sm := staticManager{
mimeCacheHdrMap: map[string]string{
"text/css": "public, max-age=604800, proxy-revalidate",
"application/javascript": "public, max-age=604800, proxy-revalidate",
"image/png": "public, max-age=604800, proxy-revalidate",
},
defaultCacheHdr: "public, max-age=31536000",
}
str := sm.cacheHeader("application/json")
assert.Equal(t, "public, max-age=31536000", str)
str = sm.cacheHeader("image/png")
assert.Equal(t, "public, max-age=604800, proxy-revalidate", str)
str = sm.cacheHeader("application/json; charset=utf-8")
assert.Equal(t, "public, max-age=31536000", str)
str = sm.cacheHeader("text/css")
assert.Equal(t, "public, max-age=604800, proxy-revalidate", str)
}
func TestStaticWriteFileError(t *testing.T) {
importPath := filepath.Join(testdataBaseDir(), "webapp1")
ts := newTestServer(t, importPath)
defer ts.Close()
t.Logf("Test Server URL [Static Write File Error]: %s", ts.URL)
sm := ts.app.staticMgr
req := httptest.NewRequest(ahttp.MethodGet, "http://localhost:8080/assets/js/myfile.js", nil)
w1 := httptest.NewRecorder()
sm.writeError(ahttp.AcquireResponseWriter(w1), ahttp.AcquireRequest(req), os.ErrPermission)
assert.Equal(t, "403 Forbidden", responseBody(w1.Result()))
w2 := httptest.NewRecorder()
sm.writeError(ahttp.AcquireResponseWriter(w2), ahttp.AcquireRequest(req), nil)
assert.Equal(t, "500 Internal Server Error", responseBody(w2.Result()))
}