...
1
2
3
4
5
6
7
8
9
10
11
12
13 package couchdb
14
15 import (
16 "encoding/json"
17 "net/http"
18 "testing"
19
20 "gitlab.com/flimzy/testy"
21
22 internal "github.com/go-kivik/kivik/v4/int/errors"
23 )
24
25 func TestDeJSONify(t *testing.T) {
26 tests := []struct {
27 name string
28 input interface{}
29 expected interface{}
30 status int
31 err string
32 }{
33 {
34 name: "string",
35 input: `{"foo":"bar"}`,
36 expected: map[string]interface{}{"foo": "bar"},
37 },
38 {
39 name: "[]byte",
40 input: []byte(`{"foo":"bar"}`),
41 expected: map[string]interface{}{"foo": "bar"},
42 },
43 {
44 name: "json.RawMessage",
45 input: json.RawMessage(`{"foo":"bar"}`),
46 expected: map[string]interface{}{"foo": "bar"},
47 },
48 {
49 name: "map",
50 input: map[string]string{"foo": "bar"},
51 expected: map[string]string{"foo": "bar"},
52 },
53 {
54 name: "invalid JSON string",
55 input: `{"foo":"\C"}`,
56 status: http.StatusBadRequest,
57 err: "invalid character 'C' in string escape code",
58 },
59 }
60 for _, test := range tests {
61 t.Run(test.name, func(t *testing.T) {
62 result, err := deJSONify(test.input)
63 if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
64 t.Error(d)
65 }
66 if d := testy.DiffInterface(test.expected, result); d != nil {
67 t.Error(d)
68 }
69 })
70 }
71 }
72
View as plain text