-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
63 lines (52 loc) · 1.28 KB
/
client_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
package client_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/remind101/pkg/client"
"github.com/remind101/pkg/client/metadata"
)
type mathClient struct {
c *client.Client
}
type multiplyInput struct {
A int `json:"a"`
B int `json:"b"`
}
type multiplyOutput struct {
Result int `json:"result"`
}
func (mc *mathClient) Multiply(a, b int) (int, error) {
params := multiplyInput{A: a, B: b}
var data multiplyOutput
req := mc.c.NewRequest(context.Background(), "POST", "/multiply", params, &data)
err := req.Send()
return data.Result, err
}
func TestClient(t *testing.T) {
r := mux.NewRouter()
r.Handle("/multiply", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
var params multiplyInput
err := json.NewDecoder(r.Body).Decode(¶ms)
if err != nil {
t.Error(err)
}
response := multiplyOutput{Result: params.A * params.B}
json.NewEncoder(rw).Encode(response)
})).Methods("POST")
s := httptest.NewServer(r)
defer s.Close()
mc := mathClient{
c: client.New(metadata.ClientInfo{ServiceName: "Math", Endpoint: s.URL}),
}
res, err := mc.Multiply(5, 2)
if err != nil {
t.Error(err)
}
if got, want := res, 10; got != want {
t.Errorf("got %d; expected %d", got, want)
}
}