-
Notifications
You must be signed in to change notification settings - Fork 3
/
server_test.go
128 lines (98 loc) · 2.99 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
package hpc_test
import (
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/rpc/v2"
"github.com/stretchr/testify/assert"
"github.com/tj/go-hpc"
)
type AddInput struct {
A int
B int
}
type AddOutput struct {
Value int `json:"value"`
}
type Math struct {
}
func (m *Math) Add(r *http.Request, in *AddInput, out *AddOutput) error {
out.Value = in.A + in.B
return nil
}
type GetStatsOutput struct {
Requests int `json:"requests"`
}
func (m *Math) GetStats(r *http.Request, in *struct{}, out *GetStatsOutput) error {
out.Requests = 5
return nil
}
func (m *Math) Error(r *http.Request, in *struct{}, out *struct{}) error {
return hpc.NewError(400, "Boom")
}
func (m *Math) InternalError(r *http.Request, in *struct{}, out *struct{}) error {
return errors.New("boom")
}
func TestCodec_request(t *testing.T) {
r := rpc.NewServer()
r.RegisterCodec(hpc.NewCodec(), "application/json")
r.RegisterService(&Math{}, "")
s := httptest.NewServer(r)
defer s.Close()
body := strings.NewReader(`{ "a": 5, "b": 10 }`)
res, err := http.Post(s.URL+"/math/add", "application/json", body)
assert.NoError(t, err, "error posting")
b, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "error reading")
defer res.Body.Close()
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "{\"value\":15}\n", string(b))
}
func TestCodec_underscores(t *testing.T) {
r := rpc.NewServer()
r.RegisterCodec(hpc.NewCodec(), "application/json")
r.RegisterService(&Math{}, "")
s := httptest.NewServer(r)
defer s.Close()
body := strings.NewReader(`{ "a": 5, "b": 10 }`)
res, err := http.Post(s.URL+"/math/get_stats", "application/json", body)
assert.NoError(t, err, "error posting")
b, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "error reading")
defer res.Body.Close()
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "{\"requests\":5}\n", string(b))
}
func TestCodec_errors(t *testing.T) {
r := rpc.NewServer()
r.RegisterCodec(hpc.NewCodec(), "application/json")
r.RegisterService(&Math{}, "")
s := httptest.NewServer(r)
defer s.Close()
body := strings.NewReader(`{ "a": 5, "b": 10 }`)
res, err := http.Post(s.URL+"/math/error", "application/json", body)
assert.NoError(t, err, "error posting")
b, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "error reading")
defer res.Body.Close()
assert.Equal(t, 400, res.StatusCode)
assert.Equal(t, "{\"error\":\"Boom\"}\n", string(b))
}
func TestCodec_internalErrors(t *testing.T) {
r := rpc.NewServer()
r.RegisterCodec(hpc.NewCodec(), "application/json")
r.RegisterService(&Math{}, "")
s := httptest.NewServer(r)
defer s.Close()
body := strings.NewReader(`{ "a": 5, "b": 10 }`)
res, err := http.Post(s.URL+"/math/internal_error", "application/json", body)
assert.NoError(t, err, "error posting")
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "error reading")
assert.Equal(t, 400, res.StatusCode)
assert.Equal(t, "{\"error\":\"Bad Request\"}\n", string(b))
}