Skip to content

Commit 681e974

Browse files
authored
fix: fix encode json body (streamnative#610)
* fix: fix encode json body Signed-off-by: Zixuan Liu <[email protected]> * test: fix get_ns_anti_affinity_group test Signed-off-by: Zixuan Liu <[email protected]> * chore: fix license Signed-off-by: Zixuan Liu <[email protected]>
1 parent 225855a commit 681e974

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

pkg/cli/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,11 @@ func endpoint(parts ...string) string {
364364

365365
// encodeJSONBody is used to JSON encode a body
366366
func encodeJSONBody(obj interface{}) (io.Reader, error) {
367-
buf := bytes.NewBuffer(nil)
368-
enc := json.NewEncoder(buf)
369-
if err := enc.Encode(obj); err != nil {
367+
b, err := json.Marshal(obj)
368+
if err != nil {
370369
return nil, err
371370
}
372-
return buf, nil
371+
return bytes.NewReader(b), nil
373372
}
374373

375374
// decodeJSONBody is used to JSON decode a body

pkg/cli/client_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cli
19+
20+
import (
21+
"io/ioutil"
22+
"testing"
23+
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func TestEncodeJSONBody(t *testing.T) {
28+
testcases := []struct {
29+
obj interface{}
30+
expected int
31+
}{
32+
{obj: "1", expected: 3},
33+
{obj: "12", expected: 4},
34+
{obj: 1, expected: 1},
35+
{obj: 12, expected: 2},
36+
}
37+
38+
for _, testcase := range testcases {
39+
r, err := encodeJSONBody(testcase.obj)
40+
require.NoError(t, err)
41+
42+
b, err := ioutil.ReadAll(r)
43+
require.NoError(t, err)
44+
45+
require.Equal(t, testcase.expected, len(b))
46+
}
47+
}

pkg/ctl/namespace/get_ns_anti_affinity_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func doGetAntiAffinityGroup(vc *cmdutils.VerbCmd) error {
7676
admin := cmdutils.NewPulsarClient()
7777
res, err := admin.Namespaces().GetNamespaceAntiAffinityGroup(ns)
7878
if err == nil {
79-
vc.Command.Print(res)
79+
vc.Command.Println(res)
8080
}
8181
return err
8282
}

pkg/ctl/namespace/ns_anti_affinity_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ func TestNsAntiAffinityGroup(t *testing.T) {
4747
getArgs = []string{"get-anti-affinity-group", "public/test-anti-namespace"}
4848
getOut, execErr, _, _ = TestNamespaceCommands(getAntiAffinityGroup, getArgs)
4949
assert.Nil(t, execErr)
50-
assert.Equal(t, getOut.String(), "")
50+
assert.Equal(t, getOut.String(), "\n")
5151
}

0 commit comments

Comments
 (0)