-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcontext_test.go
107 lines (84 loc) · 2.96 KB
/
context_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
// 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 (
"io/ioutil"
"net/http/httptest"
"testing"
"aahframe.work/ahttp"
"aahframe.work/config"
"aahframe.work/log"
"aahframe.work/router"
"github.com/stretchr/testify/assert"
)
func TestContextSubdomain(t *testing.T) {
testSubdomainValue(t, "username1.sample.com", "username1", true)
testSubdomainValue(t, "username2.sample.com", "username2", true)
testSubdomainValue(t, "admin.username1.sample.com", "admin", true)
testSubdomainValue(t, "sample.com", "", false)
}
func testSubdomainValue(t *testing.T, host, subdomain string, isSubdomain bool) {
ctx := &Context{
Req: &ahttp.Request{Host: host},
domain: &router.Domain{IsSubDomain: isSubdomain},
}
assert.Equal(t, subdomain, ctx.Subdomain())
}
func TestContextSetURL(t *testing.T) {
a := newApp()
a.cfg = config.NewEmpty()
err := a.initLog()
assert.Nil(t, err)
a.Log().(*log.Logger).SetWriter(ioutil.Discard)
req := httptest.NewRequest("POST", "http://localhost:8080/users/edit", nil)
ctx := newContext(nil, req)
ctx.a = a
assert.Equal(t, "localhost:8080", ctx.Req.Host)
assert.Equal(t, "POST", ctx.Req.Method)
assert.Equal(t, "/users/edit", ctx.Req.Path)
assert.False(t, ctx.decorated)
// No effects, since decorated is false
ctx.SetURL("http://status.localhost:8080/maintenance")
assert.Equal(t, "/users/edit", ctx.Req.Path)
assert.Equal(t, "localhost:8080", ctx.Req.Host)
// now it affects
ctx.decorated = true
ctx.SetURL("http://status.localhost:8080/maintenance")
assert.True(t, ctx.decorated)
assert.Equal(t, "status.localhost:8080", ctx.Req.Host)
assert.Equal(t, "POST", ctx.Req.Method) // no change expected
assert.Equal(t, "/maintenance", ctx.Req.Path)
// incorrect URL
ctx.SetURL("http://status. localhost :8080//maintenance")
assert.Equal(t, "status.localhost:8080", ctx.Req.Host)
assert.Equal(t, "POST", ctx.Req.Method) // no change expected
assert.Equal(t, "/maintenance", ctx.Req.Path)
}
func TestContextSetMethod(t *testing.T) {
a := newApp()
a.cfg = config.NewEmpty()
err := a.initLog()
assert.Nil(t, err)
a.Log().(*log.Logger).SetWriter(ioutil.Discard)
req := httptest.NewRequest("POST", "http://localhost:8080/users/edit", nil)
ctx := newContext(nil, req)
ctx.a = a
assert.Equal(t, "localhost:8080", ctx.Req.Host)
assert.Equal(t, "POST", ctx.Req.Method)
assert.Equal(t, "/users/edit", ctx.Req.Path)
assert.False(t, ctx.decorated)
// No effects, since decorated is false
ctx.SetMethod("GET")
assert.Equal(t, "POST", ctx.Req.Method)
// now it affects
ctx.decorated = true
ctx.SetMethod("get")
assert.Equal(t, "GET", ctx.Req.Method)
assert.Equal(t, "localhost:8080", ctx.Req.Host) // no change expected
assert.Equal(t, "/users/edit", ctx.Req.Path) // no change expected
assert.True(t, ctx.decorated)
// invalid method
ctx.SetMethod("nomethod")
assert.Equal(t, "GET", ctx.Req.Method)
}