-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
unary_test.go
89 lines (80 loc) · 1.92 KB
/
unary_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
package grpcstub
import (
"context"
"testing"
"github.com/k1LoW/grpcstub/testdata/routeguide"
)
func TestUnary(t *testing.T) {
ctx := context.Background()
ts := NewServer(t, "testdata/route_guide.proto")
t.Cleanup(func() {
ts.Close()
})
ts.Method("GetFeature").Response(map[string]any{"name": "hello", "location": map[string]any{"latitude": 10, "longitude": 13}})
ts.Method("GetFeature").Response(map[string]any{"name": "hello", "location": map[string]any{"latitude": 99, "longitude": 99}})
client := routeguide.NewRouteGuideClient(ts.Conn())
res, err := client.GetFeature(ctx, &routeguide.Point{
Latitude: 10,
Longitude: 13,
})
if err != nil {
t.Fatal(err)
}
{
got := res.Name
if want := "hello"; got != want {
t.Errorf("got %v\nwant %v", got, want)
return
}
}
{
got := res.Location.Latitude
if want := int32(10); got != want {
t.Errorf("got %v\nwant %v", got, want)
}
}
{
got := len(ts.Requests())
if want := 1; got != want {
t.Errorf("got %v\nwant %v", got, want)
return
}
}
req := ts.Requests()[0]
{
got := int32(req.Message["longitude"].(float64))
if want := int32(13); got != want {
t.Errorf("got %v\nwant %v", got, want)
}
}
}
func TestUnaryUnmatched(t *testing.T) {
ctx := context.Background()
ts := NewServer(t, "testdata/route_guide.proto")
t.Cleanup(func() {
ts.Close()
})
ts.Method("GetFeature").Match(func(req *Request) bool {
return false
}).Response(map[string]any{"name": "hello", "location": map[string]any{"latitude": 10, "longitude": 13}})
client := routeguide.NewRouteGuideClient(ts.Conn())
_, err := client.GetFeature(ctx, &routeguide.Point{
Latitude: 10,
Longitude: 13,
})
if err == nil {
t.Error("want error")
}
{
got := len(ts.Requests())
if want := 0; got != want {
t.Errorf("got %v\nwant %v", got, want)
}
}
{
got := len(ts.UnmatchedRequests())
if want := 1; got != want {
t.Errorf("got %v\nwant %v", got, want)
}
}
}