-
Notifications
You must be signed in to change notification settings - Fork 33
/
router_test.go
121 lines (102 loc) · 3.56 KB
/
router_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
// 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 (
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"aahframe.work/ahttp"
"aahframe.work/router"
"github.com/stretchr/testify/assert"
)
func TestRouterTemplateFuncs(t *testing.T) {
importPath := filepath.Join(testdataBaseDir(), "webapp1")
ts := newTestServer(t, importPath)
defer ts.Close()
t.Logf("Test Server URL [Router Template funcs]: %s", ts.URL)
err := ts.app.initRouter()
assert.Nil(t, err)
err = ts.app.initView()
assert.Nil(t, err)
vm := ts.app.viewMgr
viewArgs := map[string]interface{}{}
viewArgs["Host"] = "localhost:8080"
url1 := vm.tmplURL(viewArgs, "version_home#welcome", "v0.1")
assert.Equal(t, "//localhost:8080/doc/v0.1#welcome", string(url1))
url2 := vm.tmplURLm(viewArgs, "show_doc", map[string]interface{}{
"version": "v0.2",
"content": "getting-started.html",
})
assert.Equal(t, "//localhost:8080/doc/v0.2/getting-started.html", string(url2))
url3 := vm.tmplURL(viewArgs)
assert.Equal(t, "#", string(url3))
url4 := vm.tmplURL(viewArgs, "host")
assert.Equal(t, "//localhost:8080", string(url4))
}
func TestRouterCORS(t *testing.T) {
importPath := filepath.Join(testdataBaseDir(), "webapp1")
ts := newTestServer(t, importPath)
defer ts.Close()
t.Logf("Test Server URL [CORS]: %s", ts.URL)
// CORS NOT enabled
t.Log("CORS NOT enabled")
ctx1 := &Context{
domain: &router.Domain{},
}
CORSMiddleware(ctx1, &Middleware{})
// CORS preflight request
t.Log("CORS preflight request")
req3, err := http.NewRequest(ahttp.MethodOptions, ts.URL+"/users/edit", nil)
assert.Nil(t, err)
req3.Header.Set(ahttp.HeaderAccessControlRequestMethod, ahttp.MethodPost)
req3.Header.Set(ahttp.HeaderOrigin, "http://sample.com")
ctx3 := newContext(httptest.NewRecorder(), req3)
ctx3.a = ts.app
ctx3.domain = &router.Domain{CORSEnabled: true}
ctx3.route = &router.Route{
CORS: &router.CORS{
AllowOrigins: []string{"http://sample.com"},
AllowMethods: []string{ahttp.MethodGet, ahttp.MethodPost, ahttp.MethodOptions},
AllowCredentials: true,
MaxAge: "806400",
},
}
CORSMiddleware(ctx3, &Middleware{})
// CORS regular request
t.Log("CORS regular request")
req4, err := http.NewRequest(ahttp.MethodOptions, ts.URL+"/users/edit", nil)
assert.Nil(t, err)
req4.Header.Set(ahttp.HeaderOrigin, "http://sample.com")
ctx4 := newContext(httptest.NewRecorder(), req4)
ctx4.a = ts.app
ctx4.domain = &router.Domain{CORSEnabled: true}
ctx4.route = &router.Route{
CORS: &router.CORS{
AllowOrigins: []string{"http://sample.com"},
AllowMethods: []string{ahttp.MethodGet, ahttp.MethodPost, ahttp.MethodOptions},
ExposeHeaders: []string{ahttp.HeaderXRequestedWith},
AllowCredentials: true,
},
}
CORSMiddleware(ctx4, &Middleware{})
// Preflight invalid origin
t.Log("Preflight invalid origin")
req5, err := http.NewRequest(ahttp.MethodOptions, ts.URL+"/users/edit", nil)
assert.Nil(t, err)
req5.Header.Set(ahttp.HeaderAccessControlRequestMethod, ahttp.MethodPost)
req5.Header.Set(ahttp.HeaderOrigin, "http://example.com")
ctx5 := newContext(httptest.NewRecorder(), req5)
ctx5.a = ts.app
ctx5.domain = &router.Domain{CORSEnabled: true}
ctx5.route = &router.Route{
CORS: &router.CORS{
AllowOrigins: []string{"http://sample.com"},
AllowMethods: []string{ahttp.MethodGet, ahttp.MethodPost, ahttp.MethodOptions},
AllowCredentials: true,
MaxAge: "806400",
},
}
CORSMiddleware(ctx5, &Middleware{})
}