-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema_test.go
141 lines (132 loc) · 3.63 KB
/
schema_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
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package knit
import (
"bytes"
"encoding/json"
"net/url"
"testing"
gatewayv1alpha1 "buf.build/gen/go/bufbuild/knit/protocolbuffers/go/buf/knit/gateway/v1alpha1"
"github.com/bufbuild/knit-go/internal/gen/buf/knittest/knittestconnect"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func TestComputeResponseSchema(t *testing.T) {
t.Parallel()
route, err := url.Parse("https://foo.bar.baz/")
require.NoError(t, err)
gateway := &Gateway{Route: route}
err = gateway.AddServiceByName(knittestconnect.FooServiceName)
require.NoError(t, err)
err = gateway.AddServiceByName(knittestconnect.FizzServiceName)
require.NoError(t, err)
err = gateway.addRelation(newRelationFooStyle(t, nil))
require.NoError(t, err)
err = gateway.addRelation(newRelationFooBalanceCents(t, nil))
require.NoError(t, err)
err = gateway.addRelation(newRelationFooBars(t, nil))
require.NoError(t, err)
err = gateway.addRelation(newRelationBarBaz(t, nil))
require.NoError(t, err)
err = gateway.addRelation(newRelationBarBedazzles(t, nil))
require.NoError(t, err)
err = gateway.addRelation(newRelationFizzBuzzes(t, nil))
require.NoError(t, err)
wktMask := getExhaustiveMaskForWellKnownTypes()
scalarMask := getExhaustiveMaskForScalars()
fooMask := getExhaustiveFooMask()
fizzMask := getExhaustiveFizzMask()
testCases := []struct {
name string
req *gatewayv1alpha1.Request
}{
{
name: "wkts",
req: &gatewayv1alpha1.Request{
Method: knittestconnect.FooServiceName + ".GetFoo",
Mask: []*gatewayv1alpha1.MaskField{
{
Name: "results",
Mask: []*gatewayv1alpha1.MaskField{
{
Name: "wkts",
Mask: wktMask,
},
},
},
},
},
},
{
name: "scalars",
req: &gatewayv1alpha1.Request{
Method: knittestconnect.FizzServiceName + ".GetFizz",
Mask: []*gatewayv1alpha1.MaskField{
{
Name: "result",
Mask: []*gatewayv1alpha1.MaskField{
{
Name: "scalars",
Mask: scalarMask,
},
},
},
},
},
},
{
name: "foo",
req: &gatewayv1alpha1.Request{
Method: knittestconnect.FooServiceName + ".GetFoo",
Mask: []*gatewayv1alpha1.MaskField{
{
Name: "results",
Mask: fooMask,
},
},
},
},
{
name: "fizz",
req: &gatewayv1alpha1.Request{
Method: knittestconnect.FizzServiceName + ".GetFizz",
Mask: []*gatewayv1alpha1.MaskField{
{
Name: "result",
Mask: fizzMask,
},
},
},
},
}
for i := range testCases {
testCase := testCases[i]
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
_, schema, err := computeResponseSchema(gateway, testCase.req, true, rpcKindUnary)
require.NoError(t, err)
checkGoldenFile(t, testCase.name+"-schema.txt", format(t, schema))
})
}
}
func format(t *testing.T, msg proto.Message) string {
t.Helper()
data, err := protojson.Marshal(msg)
require.NoError(t, err)
var buf bytes.Buffer
err = json.Indent(&buf, data, "", " ")
require.NoError(t, err)
return buf.String()
}