-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
136 lines (113 loc) · 3.52 KB
/
Copy pathcontext_test.go
File metadata and controls
136 lines (113 loc) · 3.52 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
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
package jin
import (
"errors"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/juanjiTech/jin/render"
"github.com/stretchr/testify/assert"
)
// CreateTestContext returns a fresh engine and context for testing purposes
func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine) {
r = New()
c = r.allocateContext(0)
c.reset()
c.writermem.reset(w)
return
}
// CreateTestContextOnly returns a fresh context base on the engine for testing purposes
func CreateTestContextOnly(w http.ResponseWriter, r *Engine) (c *Context) {
c = r.allocateContext(r.maxParams)
c.reset()
c.writermem.reset(w)
return
}
func compareFunc(t *testing.T, a, b any) {
sf1 := reflect.ValueOf(a)
sf2 := reflect.ValueOf(b)
if sf1.Pointer() != sf2.Pointer() {
t.Error("different functions")
}
}
func TestContextHandlers(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
assert.Nil(t, c.handlers)
assert.Nil(t, c.handlers.Last())
c.handlers = HandlersChain{}
assert.NotNil(t, c.handlers)
assert.Nil(t, c.handlers.Last())
f := func(c *Context) {}
g := func(c *Context) {}
c.handlers = HandlersChain{f}
compareFunc(t, f, c.handlers.Last())
c.handlers = HandlersChain{f, g}
compareFunc(t, g, c.handlers.Last())
}
func TestContextAbort(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
assert.False(t, c.IsAborted())
c.Abort()
assert.True(t, c.IsAborted())
assert.Equal(t, abortIndex, c.index)
}
func TestContextError(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
assert.Empty(t, c.Errors)
err := errors.New("test error")
c.Error(err)
assert.Len(t, c.Errors, 1)
assert.Equal(t, "test error", (*c.Errors[0]).Error())
assert.Panics(t, func() {
c.Error(nil)
})
}
func TestContextStatus(t *testing.T) {
recorder := httptest.NewRecorder()
c, _ := CreateTestContext(recorder)
c.Status(http.StatusTeapot)
c.Writer.WriteHeaderNow()
assert.Equal(t, http.StatusTeapot, recorder.Code)
}
func TestContextFullPath(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.fullPath = "/test/path"
assert.Equal(t, "/test/path", c.FullPath())
}
func TestBodyAllowedForStatus(t *testing.T) {
assert.False(t, bodyAllowedForStatus(http.StatusProcessing))
assert.False(t, bodyAllowedForStatus(http.StatusNoContent))
assert.False(t, bodyAllowedForStatus(http.StatusNotModified))
assert.True(t, bodyAllowedForStatus(http.StatusOK))
}
func TestContextRender(t *testing.T) {
recorder := httptest.NewRecorder()
c, _ := CreateTestContext(recorder)
// Test with JSON
c.Render(http.StatusOK, render.JSON{Data: map[string]string{"foo": "bar"}})
assert.Equal(t, http.StatusOK, recorder.Code)
assert.Equal(t, "{\"foo\":\"bar\"}", recorder.Body.String())
assert.Equal(t, "application/json; charset=utf-8", recorder.Header().Get("Content-Type"))
// Test with no content
recorder = httptest.NewRecorder()
c, _ = CreateTestContext(recorder)
c.Render(http.StatusNoContent, render.JSON{Data: map[string]string{"foo": "bar"}})
assert.Equal(t, http.StatusNoContent, recorder.Code)
assert.Empty(t, recorder.Body.String())
// Test with render error
recorder = httptest.NewRecorder()
c, _ = CreateTestContext(recorder)
c.Render(http.StatusOK, &render.YAML{Data: make(chan int)})
assert.True(t, c.IsAborted())
assert.NotEmpty(t, c.Errors)
}
func TestContextNext(t *testing.T) {
recorder := httptest.NewRecorder()
c, _ := CreateTestContext(recorder)
c.handlers = HandlersChain{nil, func(c *Context) {
c.Status(http.StatusOK)
}}
c.Map(c)
c.Next()
assert.Equal(t, http.StatusOK, recorder.Code)
}