-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactor_test.go
171 lines (144 loc) · 4.39 KB
/
reactor_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
162
163
164
165
166
167
168
169
170
171
package restify_test
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing/iotest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/phogolabs/flaw"
"github.com/phogolabs/restify"
"github.com/phogolabs/restify/fake"
)
var _ = Describe("Reactor", func() {
var (
reactor *restify.Reactor
request *http.Request
response *httptest.ResponseRecorder
)
BeforeEach(func() {
buffer := &bytes.Buffer{}
request = httptest.NewRequest("POST", "http://example.com", buffer)
response = httptest.NewRecorder()
})
JustBeforeEach(func() {
reactor = restify.NewReactor(response, request)
})
Describe("Status", func() {
It("sets the status successfully", func() {
reactor.Status(http.StatusAccepted)
Expect(reactor.Render(nil)).To(Succeed())
Expect(response.Code).To(Equal(http.StatusAccepted))
})
})
Describe("Bind", func() {
It("binds the input successfully", func() {
input := &BindableInput{}
Expect(reactor.Bind(input)).To(Succeed())
Expect(input.BindCnt).To(Equal(1))
})
Context("when the binding fails", func() {
It("returns an error", func() {
input := &BindableInput{BindFail: true}
Expect(reactor.Bind(input)).To(MatchError("oh no"))
Expect(input.BindCnt).To(Equal(1))
Expect(input.BindableChild).NotTo(BeNil())
Expect(input.BindableChild.BindCnt).To(Equal(1))
})
Context("when the child fails", func() {
It("returns an error", func() {
input := &BindableInput{
BindableChild: &BindableChild{
BindFail: true,
},
}
Expect(reactor.Bind(input)).To(MatchError("oh no"))
Expect(input.BindableChild).NotTo(BeNil())
Expect(input.BindableChild.BindCnt).To(Equal(1))
})
})
})
Context("when the request boy returns an error", func() {
JustBeforeEach(func() {
var (
data = make([]byte, 1)
reader = iotest.TimeoutReader(&bytes.Buffer{})
)
reader.Read(data)
request.Body = ioutil.NopCloser(reader)
})
It("returns an error", func() {
input := &BindableInput{}
Expect(reactor.Bind(input)).To(MatchError("timeout"))
})
})
})
Describe("Render", func() {
It("renders the output successfully", func() {
output := &RenderableOutput{}
Expect(reactor.Render(output)).To(Succeed())
Expect(output.RenderCnt).To(Equal(1))
Expect(output.RenderableChild).NotTo(BeNil())
Expect(output.RenderableChild.RenderCnt).To(Equal(1))
})
Context("when the rendering fails", func() {
It("returns an error", func() {
output := &RenderableOutput{RenderFail: true}
Expect(reactor.Render(output)).To(MatchError("oh no"))
Expect(output.RenderCnt).To(Equal(1))
})
Context("when the child fails", func() {
It("returns an error", func() {
output := &RenderableOutput{
RenderableChild: &RenderableChild{
RenderFail: true,
},
}
Expect(reactor.Render(output)).To(MatchError("oh no"))
Expect(output.RenderableChild.RenderCnt).To(Equal(1))
})
})
})
Context("when the output is error", func() {
It("renders the output successfully", func() {
output := fmt.Errorf("oh no")
Expect(reactor.Render(output)).To(Succeed())
Expect(response.Code).To(Equal(http.StatusInternalServerError))
})
})
Context("when the output is not a pointer", func() {
It("returns an error", func() {
var output RenderableOutput
Expect(reactor.Render(output)).To(MatchError("the target must be a pointer"))
})
})
Context("when the response writer returns an error", func() {
JustBeforeEach(func() {
response := &fake.ResponseWriter{}
response.HeaderReturns(http.Header{})
response.WriteReturns(0, fmt.Errorf("oh no"))
reactor = restify.NewReactor(response, request)
})
It("returns an error", func() {
output := &RenderableOutput{}
Expect(reactor.Render(output)).To(MatchError("oh no"))
})
})
})
Describe("RenderWith", func() {
It("renders the output with the given code successfully", func() {
output := &RenderableOutput{}
Expect(reactor.RenderWith(http.StatusCreated, output)).To(Succeed())
Expect(response.Code).To(Equal(http.StatusCreated))
})
Context("when the output is an error", func() {
It("overrides the error status", func() {
output := flaw.Errorf("not found")
Expect(reactor.RenderWith(http.StatusNotFound, output)).To(Succeed())
Expect(response.Code).To(Equal(http.StatusNotFound))
})
})
})
})