-
Notifications
You must be signed in to change notification settings - Fork 179
/
server_test.go
240 lines (207 loc) · 5.22 KB
/
server_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2009 The Go Authors. All rights reserved.
// Copyright 2012 The Gorilla Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpc
import (
"log"
"net/http"
"strconv"
"testing"
)
type Service1Request struct {
A int
B int
}
type Service1Response struct {
Result int
}
type Service1 struct {
}
func (t *Service1) Multiply(r *http.Request, req *Service1Request, res *Service1Response) error {
res.Result = req.A * req.B
return nil
}
func (t *Service1) Add(req *Service1Request, res *Service1Response) error {
res.Result = req.A + req.B
return nil
}
type Service2 struct {
}
func TestRegisterService(t *testing.T) {
var err error
s := NewServer()
service1 := new(Service1)
service2 := new(Service2)
// Inferred name.
err = s.RegisterService(service1, "")
if err != nil || !s.HasMethod("Service1.Multiply") {
t.Errorf("Expected to be registered: Service1.Multiply")
}
// Provided name.
err = s.RegisterService(service1, "Foo")
if err != nil || !s.HasMethod("Foo.Multiply") {
t.Errorf("Expected to be registered: Foo.Multiply")
}
// No methods.
err = s.RegisterService(service2, "")
if err == nil {
t.Errorf("Expected error on service2")
}
}
func TestRegisterTCPService(t *testing.T) {
var err error
s := NewServer()
service1 := new(Service1)
service2 := new(Service2)
// Inferred name.
err = s.RegisterTCPService(service1, "")
if err != nil || !s.HasMethod("Service1.Add") {
t.Errorf("Expected to be registered: Service1.Add")
}
// Provided name.
err = s.RegisterTCPService(service1, "Foo")
if err != nil || !s.HasMethod("Foo.Add") {
t.Errorf("Expected to be registered: Foo.Add")
}
// No methods.
err = s.RegisterTCPService(service2, "")
if err == nil {
t.Errorf("Expected error on service2")
}
}
// MockCodec decodes to Service1.Multiply.
type MockCodec struct {
A, B int
}
func (c MockCodec) NewRequest(*http.Request) CodecRequest {
return MockCodecRequest(c)
}
type MockCodecRequest struct {
A, B int
}
func (r MockCodecRequest) Method() (string, error) {
return "Service1.Multiply", nil
}
func (r MockCodecRequest) ReadRequest(args interface{}) error {
req := args.(*Service1Request)
req.A, req.B = r.A, r.B
return nil
}
func (r MockCodecRequest) WriteResponse(w http.ResponseWriter, reply interface{}, methodErr error) error {
if methodErr != nil {
if _, err := w.Write([]byte(methodErr.Error())); err != nil {
log.Fatal(err)
}
} else {
res := reply.(*Service1Response)
if _, err := w.Write([]byte(strconv.Itoa(res.Result))); err != nil {
log.Fatal(err)
}
}
return nil
}
type MockResponseWriter struct {
header http.Header
Status int
Body string
}
func NewMockResponseWriter() *MockResponseWriter {
header := make(http.Header)
return &MockResponseWriter{header: header}
}
func (w *MockResponseWriter) Header() http.Header {
return w.header
}
func (w *MockResponseWriter) Write(p []byte) (int, error) {
w.Body = string(p)
if w.Status == 0 {
w.Status = 200
}
return len(p), nil
}
func (w *MockResponseWriter) WriteHeader(status int) {
w.Status = status
}
func TestServeHTTP(t *testing.T) {
const (
A = 2
B = 3
)
expected := A * B
s := NewServer()
if err := s.RegisterService(new(Service1), ""); err != nil {
log.Fatal(err)
}
s.RegisterCodec(MockCodec{A, B}, "mock")
r, err := http.NewRequest("POST", "", nil)
if err != nil {
t.Fatal(err)
}
r.Header.Set("Content-Type", "mock; dummy")
w := NewMockResponseWriter()
s.ServeHTTP(w, r)
if w.Status != 200 {
t.Errorf("Status was %d, should be 200.", w.Status)
}
if w.Body != strconv.Itoa(expected) {
t.Errorf("Response body was %s, should be %s.", w.Body, strconv.Itoa(expected))
}
// Test wrong Content-Type
r.Header.Set("Content-Type", "invalid")
w = NewMockResponseWriter()
s.ServeHTTP(w, r)
if w.Status != 415 {
t.Errorf("Status was %d, should be 415.", w.Status)
}
if w.Body != "rpc: unrecognized Content-Type: invalid" {
t.Errorf("Wrong response body.")
}
// Test omitted Content-Type; codec should default to the sole registered one.
r.Header.Del("Content-Type")
w = NewMockResponseWriter()
s.ServeHTTP(w, r)
if w.Status != 200 {
t.Errorf("Status was %d, should be 200.", w.Status)
}
if w.Body != strconv.Itoa(expected) {
t.Errorf("Response body was %s, should be %s.", w.Body, strconv.Itoa(expected))
}
}
func TestInterception(t *testing.T) {
const (
A = 2
B = 3
)
expected := A * B
r2, err := http.NewRequest("POST", "mocked/request", nil)
if err != nil {
t.Fatal(err)
}
s := NewServer()
if err = s.RegisterService(new(Service1), ""); err != nil {
t.Fatal(err)
}
s.RegisterCodec(MockCodec{A, B}, "mock")
s.RegisterInterceptFunc(func(i *RequestInfo) *http.Request {
return r2
})
s.RegisterAfterFunc(func(i *RequestInfo) {
if i.Request != r2 {
t.Errorf("Request was %v, should be %v.", i.Request, r2)
}
})
r, err := http.NewRequest("POST", "", nil)
if err != nil {
t.Fatal(err)
}
r.Header.Set("Content-Type", "mock; dummy")
w := NewMockResponseWriter()
s.ServeHTTP(w, r)
if w.Status != 200 {
t.Errorf("Status was %d, should be 200.", w.Status)
}
if w.Body != strconv.Itoa(expected) {
t.Errorf("Response body was %s, should be %s.", w.Body, strconv.Itoa(expected))
}
}