-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathglobal_test.go
More file actions
66 lines (54 loc) · 1.46 KB
/
global_test.go
File metadata and controls
66 lines (54 loc) · 1.46 KB
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
package hime
import (
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGlobal(t *testing.T) {
t.Run("App", func(t *testing.T) {
t.Run("retrieve any data from empty global", func(t *testing.T) {
app := New()
assert.Nil(t, app.Global("key1"))
})
t.Run("retrieve data from global", func(t *testing.T) {
app := New()
app.Globals(Globals{
"key1": "value1",
"key2": "value2",
})
assert.Equal(t, app.Global("key1"), "value1")
assert.Equal(t, app.Global("key2"), "value2")
assert.Nil(t, app.Global("key3"))
})
})
t.Run("Context", func(t *testing.T) {
t.Run("retrieve any data from empty global", func(t *testing.T) {
app := New()
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
app.Handler(Handler(func(ctx *Context) error {
assert.Nil(t, app.Global("key1"))
return nil
}))
app.ServeHTTP(w, r)
})
t.Run("retrieve data from global", func(t *testing.T) {
app := New()
app.Globals(Globals{
"key1": "value1",
"key2": "value2",
})
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
app.Handler(Handler(func(ctx *Context) error {
assert.Equal(t, ctx.Global("key1"), "value1")
assert.Equal(t, ctx.Global("key2"), "value2")
assert.Equal(t, Global(ctx, "key1"), "value1")
assert.Equal(t, Global(ctx, "key2"), "value2")
assert.Nil(t, ctx.Global("key3"))
return nil
}))
app.ServeHTTP(w, r)
})
})
}