This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfresh_test.go
161 lines (145 loc) · 4.1 KB
/
fresh_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package fresh
import (
"bytes"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vicanso/elton"
)
func TestFresh(t *testing.T) {
fn := NewDefault()
modifiedAt := "Tue, 25 Dec 2018 00:02:22 GMT"
t.Run("skip", func(t *testing.T) {
assert := assert.New(t)
c := elton.NewContext(nil, nil)
done := false
c.Next = func() error {
done = true
return nil
}
fn := New(Config{
Skipper: func(c *elton.Context) bool {
return true
},
})
err := fn(c)
assert.Nil(err)
assert.True(done)
})
t.Run("return error", func(t *testing.T) {
assert := assert.New(t)
c := elton.NewContext(nil, nil)
customErr := errors.New("abccd")
c.Next = func() error {
return customErr
}
fn := New(Config{})
err := fn(c)
assert.Equal(err, customErr, "custom error should be return")
})
t.Run("not modified", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("GET", "/users/me", nil)
req.Header.Set(elton.HeaderIfModifiedSince, modifiedAt)
resp := httptest.NewRecorder()
resp.Header().Set(elton.HeaderLastModified, modifiedAt)
c := elton.NewContext(resp, req)
done := false
c.Next = func() error {
done = true
c.Body = map[string]string{
"name": "tree.xie",
}
c.BodyBuffer = bytes.NewBufferString(`{"name":"tree.xie"}`)
return nil
}
err := fn(c)
assert.Nil(err)
assert.True(done)
assert.Equal(c.StatusCode, 304, "status code should be 304")
assert.Nil(c.Body, "body should be nil")
assert.Nil(c.BodyBuffer, "body buffer should be nil")
})
t.Run("no body", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("GET", "/users/me", nil)
req.Header.Set(elton.HeaderIfModifiedSince, modifiedAt)
resp := httptest.NewRecorder()
resp.Header().Set(elton.HeaderLastModified, modifiedAt)
c := elton.NewContext(resp, req)
c.Next = func() error {
return nil
}
c.NoContent()
err := fn(c)
assert.Nil(err)
assert.Equal(c.StatusCode, 204, "no body should be passed by fresh")
})
t.Run("post method", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("POST", "/users/me", nil)
req.Header.Set(elton.HeaderIfModifiedSince, modifiedAt)
resp := httptest.NewRecorder()
resp.Header().Set(elton.HeaderLastModified, modifiedAt)
c := elton.NewContext(resp, req)
done := false
c.Next = func() error {
done = true
c.StatusCode = http.StatusOK
c.Body = map[string]string{
"name": "tree.xie",
}
c.BodyBuffer = bytes.NewBufferString(`{"name":"tree.xie"}`)
return nil
}
err := fn(c)
assert.Nil(err)
assert.True(done)
assert.Equal(c.StatusCode, 200, "post requset should be passed by fresh")
assert.NotNil(c.Body, "post requset should be passed by fresh")
assert.NotNil(c.BodyBuffer, "post requset should be passed by fresh")
})
t.Run("error response", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("GET", "/users/me", nil)
req.Header.Set(elton.HeaderIfModifiedSince, modifiedAt)
resp := httptest.NewRecorder()
resp.Header().Set(elton.HeaderLastModified, modifiedAt)
c := elton.NewContext(resp, req)
done := false
c.Next = func() error {
done = true
c.StatusCode = http.StatusBadRequest
c.Body = map[string]string{
"name": "tree.xie",
}
c.BodyBuffer = bytes.NewBufferString(`{"name":"tree.xie"}`)
return nil
}
err := fn(c)
assert.Nil(err)
assert.True(done)
assert.Equal(c.StatusCode, http.StatusBadRequest, "error response should be passed by fresh")
assert.NotNil(c.Body, "error response should be passed by fresh")
assert.NotNil(c.BodyBuffer, "error response should be passed by fresh")
})
}
// https://stackoverflow.com/questions/50120427/fail-unit-tests-if-coverage-is-below-certain-percentage
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
rc := m.Run()
// rc 0 means we've passed,
// and CoverMode will be non empty if run with -cover
if rc == 0 && testing.CoverMode() != "" {
c := testing.Coverage()
if c < 0.9 {
fmt.Println("Tests passed but coverage failed at", c)
rc = -1
}
}
os.Exit(rc)
}