-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptor_test.go
119 lines (111 loc) · 2.56 KB
/
interceptor_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
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
package interceptor
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/fieldmaskpb"
discoveryv1 "github.com/linhbkhn95/go-grpc-middleware-field-mask/pb/go/discovery/v1"
)
func TestFieldMaskSuite(t *testing.T) {
s := &FieldMaskSuite{
InterceptorTestSuite: &discoveryv1.InterceptorTestSuite{
DiscoveryService: &discoveryv1.DiscoveryService{},
ServerOpts: []grpc.ServerOption{
grpc.UnaryInterceptor(
UnaryServerInterceptor(DefaultFilterFunc),
),
},
},
}
suite.Run(t, s)
}
type FieldMaskSuite struct {
*discoveryv1.InterceptorTestSuite
}
func (s *FieldMaskSuite) TestUnary_ReturnAllResponseWhenDisableFieldMask() {
resp, err := s.Client.ListProducts(context.Background(), &discoveryv1.ListProductsRequest{Id: "1"})
assert.Equal(s.T(), nil, err)
expected := &discoveryv1.ListProductsResponse{
Result: &discoveryv1.ListProductsResult{
Products: []*discoveryv1.Product{
{
Id: "1",
Name: "Product 1",
Img: "Image 1",
Price: 1,
Shop: &discoveryv1.Shop{
Id: "1",
Name: "Shop 1",
},
},
{
Id: "2",
Name: "Product 2",
Img: "Image 2",
Price: 1,
Shop: &discoveryv1.Shop{
Id: "2",
Name: "Shop 2",
},
},
{
Id: "3",
Name: "Product 3",
Img: "Image 3",
Price: 1,
Shop: &discoveryv1.Shop{
Id: "3",
Name: "Shop 3",
},
},
{
Id: "4",
Name: "Product 4",
Img: "Image 4",
Price: 1,
Shop: &discoveryv1.Shop{
Id: "4",
Name: "Shop 4",
},
},
},
},
}
assert.ElementsMatch(s.T(), expected.Result.Products, resp.Result.Products)
}
func (s *FieldMaskSuite) TestUnary_FilterResponseWhenApplyingFieldMask() {
resp, err := s.Client.ListProducts(
context.Background(),
&discoveryv1.ListProductsRequest{
Id: "1", FieldMask: &fieldmaskpb.FieldMask{
Paths: []string{"result.products.id", "result.products.price"},
}})
assert.Equal(s.T(), nil, err)
expected := &discoveryv1.ListProductsResponse{
Result: &discoveryv1.ListProductsResult{
Products: []*discoveryv1.Product{
{
Id: "1",
Price: 1,
},
{
Id: "2",
Price: 1,
},
{
Id: "3",
Price: 1,
},
{
Id: "4",
Price: 1,
},
},
},
}
assert.ElementsMatch(s.T(), expected.Result.Products, resp.Result.Products)
}