-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_test.go
129 lines (111 loc) · 3.86 KB
/
url_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
package jsonutil_test
import (
"errors"
"net/url"
"reflect"
"strconv"
"testing"
"github.com/MarkRosemaker/jsonutil"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
)
func TestURL(t *testing.T) {
type testURL struct {
URL url.URL `json:"url"`
URLPointer *url.URL `json:"urlPointer"`
URLOmitEmpty url.URL `json:"urlOmitEmpty,omitempty"`
URLPointerOmitEmpty *url.URL `json:"urlPointerOmitEmpty,omitempty"`
}
jsonOpts := json.JoinOptions(
json.WithMarshalers(json.MarshalToFunc(jsonutil.URLMarshal)),
json.WithUnmarshalers(json.UnmarshalFromFunc(jsonutil.URLUnmarshal)),
)
t.Run("EOF", func(t *testing.T) {
out := &testURL{}
errSyn := &jsontext.SyntacticError{}
if err := json.Unmarshal([]byte(`{"url":`), out, jsonOpts); err == nil {
t.Fatalf("expected error")
} else if !errors.As(err, &errSyn) {
t.Fatalf("expected error to be a semantic error, got: %v", err)
} else if want := `unexpected EOF`; errSyn.Err.Error() != want {
t.Fatalf("expected syntactic error be %s, got: %#v", want, errSyn.Err)
}
})
t.Run("not a string", func(t *testing.T) {
out := &testURL{}
errSem := &json.SemanticError{}
if err := json.Unmarshal([]byte(`{"url":3}`), out, jsonOpts); err == nil {
t.Fatalf("expected error")
} else if !errors.As(err, &errSem) {
t.Fatalf("expected error to be a semantic error, got: %v", err)
} else if tpInt := reflect.TypeFor[*url.URL](); errSem.GoType != tpInt {
t.Fatalf("expected semantic error to have type %s, got: %s", tpInt, errSem.GoType)
}
})
t.Run("parse error", func(t *testing.T) {
out := &testURL{}
errSem := &json.SemanticError{}
if err := json.Unmarshal([]byte(`{"url":" http://example.org"}`), out, jsonOpts); err == nil {
t.Fatalf("expected error")
} else if !errors.As(err, &errSem) {
t.Fatalf("expected error to be a semantic error, got: %v", err)
} else if tpInt := reflect.TypeFor[*url.URL](); errSem.GoType != tpInt {
t.Fatalf("expected semantic error to have type %s, got: %s", tpInt, errSem.GoType)
}
})
t.Run("null", func(t *testing.T) {
out := &testURL{}
if err := json.Unmarshal([]byte(`{"url":null,"urlPointer":null,"urlOmitEmpty":null,"urlPointerOmitEmpty":null}`), out, jsonOpts); err != nil {
t.Errorf("unexpected error: %v", err)
}
})
u := url.URL{
Scheme: "https",
Host: "example.com",
Path: "/path",
}
for i, tc := range []struct {
in testURL
out string
}{
{testURL{}, `{"url":"","urlPointer":null}`},
{
testURL{URL: u, URLPointer: &u, URLOmitEmpty: u, URLPointerOmitEmpty: &u},
`{"url":"https://example.com/path","urlPointer":"https://example.com/path","urlOmitEmpty":"https://example.com/path","urlPointerOmitEmpty":"https://example.com/path"}`,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
b, err := json.Marshal(tc.in, jsonOpts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(b) != tc.out {
t.Fatalf("want: %s, got: %s", tc.out, string(b))
}
var out testURL
if err := json.Unmarshal(b, &out, jsonOpts); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := out.URL.String(), tc.in.URL.String(); got != want {
t.Fatalf("want: %s, got: %s", want, got)
}
if got, want := out.URLOmitEmpty.String(), tc.in.URLOmitEmpty.String(); got != want {
t.Fatalf("want: %s, got: %s", want, got)
}
for _, tt := range []struct{ got, want *url.URL }{
{out.URLPointer, tc.in.URLPointer},
{out.URLPointerOmitEmpty, tc.in.URLPointerOmitEmpty},
} {
if tt.got == nil && tt.want != nil {
t.Fatalf("expected non-nil URLPointer")
} else if tt.got != nil && tt.want == nil {
t.Fatalf("expected nil URLPointer")
} else if tt.got != nil && tt.want != nil {
if tt.got.String() != tt.want.String() {
t.Fatalf("want: %s, got: %s", tt.want, tt.got)
}
}
}
})
}
}