forked from riferrei/srclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemaRegistryClient_test.go
107 lines (94 loc) · 2.79 KB
/
schemaRegistryClient_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
package srclient
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func bodyToString(in io.ReadCloser) string {
buf := new(bytes.Buffer)
buf.ReadFrom(in)
return buf.String()
}
func TestSchemaRegistryClient_CreateSchemaWithoutReferences(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
responsePayload := schemaResponse{
Subject: "test1",
Version: 1,
Schema: "test2",
ID: 1,
}
response, _ := json.Marshal(responsePayload)
switch req.URL.String() {
case "/subjects/test1-value/versions":
requestPayload := schemaRequest{
Schema: "test2",
SchemaType: Protobuf.String(),
References: []Reference{},
}
expected, _ := json.Marshal(requestPayload)
// Test payload
assert.Equal(t, bodyToString(req.Body), string(expected))
// Send response to be tested
rw.Write(response)
case "/subjects/test1-value/versions/latest":
// Send response to be tested
rw.Write(response)
default:
assert.Error(t, errors.New("unhandled request"))
}
}))
srClient := CreateSchemaRegistryClient(server.URL)
srClient.CodecCreationEnabled(false)
schema, err := srClient.CreateSchema("test1", "test2", Protobuf, false)
// Test response
assert.NoError(t, err)
assert.Equal(t, schema.id, 1)
assert.Nil(t, schema.codec)
assert.Equal(t, schema.schema, "test2")
assert.Equal(t, schema.version, 1)
}
func TestSchemaRegistryClient_CreateSchemaWithReferences(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
responsePayload := schemaResponse{
Subject: "test1",
Version: 1,
Schema: "test2",
ID: 1,
}
response, _ := json.Marshal(responsePayload)
switch req.URL.String() {
case "/subjects/test1-value/versions":
requestPayload := schemaRequest{
Schema: "test2",
SchemaType: Protobuf.String(),
References: []Reference{
{Name: "test3", Subject: "test4", Version: 1},
},
}
expected, _ := json.Marshal(requestPayload)
// Test payload
assert.Equal(t, bodyToString(req.Body), string(expected))
// Send response to be tested
rw.Write(response)
case "/subjects/test1-value/versions/latest":
// Send response to be tested
rw.Write(response)
default:
assert.Error(t, errors.New("unhandled request"))
}
}))
srClient := CreateSchemaRegistryClient(server.URL)
srClient.CodecCreationEnabled(false)
schema, err := srClient.CreateSchema("test1", "test2", Protobuf, false, Reference{Name: "test3", Subject: "test4", Version: 1})
// Test response
assert.NoError(t, err)
assert.Equal(t, schema.id, 1)
assert.Nil(t, schema.codec)
assert.Equal(t, schema.schema, "test2")
assert.Equal(t, schema.version, 1)
}