-
Notifications
You must be signed in to change notification settings - Fork 34
/
handler_test.go
119 lines (93 loc) · 2.83 KB
/
handler_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
package train
import (
"github.com/shaoshing/gotest"
"io/ioutil"
"net/http"
"net/http/httptest"
"os/exec"
"strings"
"testing"
)
var httpClient = http.Client{}
var httpServer *httptest.Server
func initServer() {
http.DefaultServeMux = http.NewServeMux()
ConfigureHttpHandler(nil)
httpServer = httptest.NewServer(nil)
}
func TestDeliverUnbundledAssets(t *testing.T) {
initServer()
assert.Test = t
Config.BundleAssets = true
assertAsset("/assets/static.txt", "static.txt\n", "text/plain")
assertAsset("/assets/images/dummy.png", "dummy\n", "image/png")
assert404("/assets/not/found.js")
assertAsset("/assets/javascripts/normal.js", "@normal.js\n", "application/javascript")
assertAsset("/assets/stylesheets/normal.css", "normal.css\n", "text/css")
bundledJs := assertAsset("/assets/javascripts/require.js", "", "application/javascript")
assert.Contain("@sub/normal.js", bundledJs)
assert.Contain("@sub/normal1.coffee", bundledJs)
assert.Contain("@sub/require.js", bundledJs)
assert.Contain("@normal.js", bundledJs)
assert.Contain("@normal1.coffee", bundledJs)
assert.Contain("@require.js", bundledJs)
assertAsset("/assets/stylesheets/require.css", `normal.css
sub/normal.css
sub/require.css
require.css
`, "text/css")
assertAsset("/assets/stylesheets/app.css", `h1 {
color: green; }
h2 {
color: red; }`, "text/css")
assertAsset("/assets/stylesheets/app2.css", `h2 {
color: green; }
h3 {
color: green; }`, "text/css")
body, _, status := get("/assets/stylesheets/app.err.css")
assert.Contain("Could not compile sass", body)
assert.Equal(500, status)
body, _, _ = get("/assets/javascripts/app.js")
assert.Contain("square = function(x)", body)
body, _, status = get("/assets/javascripts/app.err.js")
assert.Contain("Could not compile coffee", body)
assert.Equal(500, status)
}
func TestDeliverBundledAssets(t *testing.T) {
Config.Mode = PRODUCTION_MODE
initServer()
assert.Test = t
exec.Command("cp", "-rf", "assets/public", "./").Run()
defer func() {
exec.Command("rm", "-rf", "public").Run()
Config.Mode = DEVELOPMENT_MODE
}()
assertAsset("/assets/app.js", "app.js\n", "application/javascript")
assert404("/assets/normal.js")
}
func get(url string) (body, contentType string, status int) {
response, err := httpClient.Get(httpServer.URL + url)
if err != nil {
panic(err)
}
b_body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
body = string(b_body)
contentType = response.Header.Get("Content-Type")
status = response.StatusCode
return
}
func assertAsset(url, expectedBody, expectedContentType string) string {
body, contentType, _ := get(url)
if len(expectedBody) != 0 {
assert.Equal(expectedBody, body)
}
assert.Equal(true, strings.Index(contentType, expectedContentType) != -1)
return body
}
func assert404(url string) {
_, _, status := get(url)
assert.Equal(404, status)
}