-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels_test.go
More file actions
98 lines (90 loc) · 2.29 KB
/
models_test.go
File metadata and controls
98 lines (90 loc) · 2.29 KB
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
package servercow
import (
"encoding/json"
"testing"
)
func TestValueMarshalSingle(t *testing.T) {
v := value{"hello"}
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
// single value must marshal as a plain JSON string, not an array
if string(b) != `"hello"` {
t.Errorf("got %s, want %q", b, "hello")
}
}
func TestValueMarshalMultiple(t *testing.T) {
v := value{"a", "b"}
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
if string(b) != `["a","b"]` {
t.Errorf("got %s, want [\"a\",\"b\"]", b)
}
}
func TestValueMarshalEmpty(t *testing.T) {
var v value
b, err := json.Marshal(record{Name: "x", Type: "TXT", Content: v})
if err != nil {
t.Fatal(err)
}
// empty content must be omitted from JSON (omitempty)
var r record
if err := json.Unmarshal(b, &r); err != nil {
t.Fatal(err)
}
if len(r.Content) != 0 {
t.Errorf("expected empty content, got %v", r.Content)
}
}
func TestValueUnmarshalString(t *testing.T) {
var v value
if err := json.Unmarshal([]byte(`"hello"`), &v); err != nil {
t.Fatal(err)
}
if len(v) != 1 || v[0] != "hello" {
t.Errorf("got %v, want [hello]", v)
}
}
func TestValueUnmarshalArray(t *testing.T) {
var v value
if err := json.Unmarshal([]byte(`["a","b","c"]`), &v); err != nil {
t.Fatal(err)
}
if len(v) != 3 || v[0] != "a" || v[1] != "b" || v[2] != "c" {
t.Errorf("got %v, want [a b c]", v)
}
}
func TestValueUnmarshalNull(t *testing.T) {
var v value
if err := json.Unmarshal([]byte("null"), &v); err != nil {
t.Fatalf("UnmarshalJSON(null) returned error: %v", err)
}
if v != nil {
t.Errorf("expected nil after unmarshaling null, got %v", v)
}
}
func TestRecordRoundtrip(t *testing.T) {
original := record{
Name: "_acme-challenge",
Type: "TXT",
TTL: 120,
Content: value{"token1", "token2"},
}
b, err := json.Marshal(original)
if err != nil {
t.Fatal(err)
}
var decoded record
if err := json.Unmarshal(b, &decoded); err != nil {
t.Fatal(err)
}
if decoded.Name != original.Name || decoded.Type != original.Type || decoded.TTL != original.TTL {
t.Errorf("record fields mismatch: got %+v, want %+v", decoded, original)
}
if len(decoded.Content) != 2 || decoded.Content[0] != "token1" || decoded.Content[1] != "token2" {
t.Errorf("content mismatch: got %v, want [token1 token2]", decoded.Content)
}
}