Repository navigation
Expand file tree
/
Copy pathnode_test.go
More file actions
332 lines (301 loc) · 8.74 KB
/
Copy pathnode_test.go
File metadata and controls
332 lines (301 loc) · 8.74 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package jsonquery
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
func TestLoadURLStatus(t *testing.T) {
for _, status := range []int{200, 201, 202, 206, 299, 300, 302, 304, 400, 401, 403, 404, 429, 500, 503} {
t.Run(fmt.Sprint(status), func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
fmt.Fprint(w, `{"message":"response"}`)
}))
defer server.Close()
doc, err := LoadURL(server.URL)
if status >= 200 && status < 300 {
if err != nil {
t.Fatal(err)
}
if got := doc.SelectElement("message"); got == nil || got.Value() != "response" {
t.Fatalf("unexpected document: %#v", doc)
}
return
}
if doc != nil || err == nil {
t.Fatalf("LoadURL = (%v, %v), want nil document and HTTP error", doc, err)
}
if !strings.Contains(err.Error(), fmt.Sprint(status)) {
t.Fatalf("error %q does not identify HTTP status %d", err, status)
}
})
}
}
func TestLoadURLRedirectStatus(t *testing.T) {
for _, status := range []int{http.StatusOK, http.StatusNotFound} {
t.Run(fmt.Sprint(status), func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/redirect" {
http.Redirect(w, r, "/document", http.StatusFound)
return
}
w.WriteHeader(status)
fmt.Fprint(w, `{"message":"redirected"}`)
}))
defer server.Close()
doc, err := LoadURL(server.URL + "/redirect")
if status == http.StatusOK {
if err != nil {
t.Fatal(err)
}
if got := doc.SelectElement("message"); got == nil || got.Value() != "redirected" {
t.Fatalf("unexpected redirected document: %#v", doc)
}
} else if doc != nil || err == nil || !strings.Contains(err.Error(), fmt.Sprint(status)) {
t.Fatalf("LoadURL = (%v, %v), want final HTTP status %d", doc, err, status)
}
})
}
}
type loadURLTransport func(*http.Request) (*http.Response, error)
func (f loadURLTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}
type loadURLBody struct {
io.Reader
read, closed bool
}
func (b *loadURLBody) Read(p []byte) (int, error) {
b.read = true
return b.Reader.Read(p)
}
func (b *loadURLBody) Close() error {
b.closed = true
return nil
}
func TestLoadURLClosesBody(t *testing.T) {
client := http.DefaultClient
defer func() { http.DefaultClient = client }()
for _, tc := range []struct {
name string
status int
body string
wantErr bool
}{
{"success", http.StatusOK, `{"message":"response"}`, false},
{"invalid JSON", http.StatusOK, `invalid`, true},
{"empty success", http.StatusNoContent, ``, true},
{"client error", http.StatusNotFound, `{"message":"missing"}`, true},
{"server error", http.StatusServiceUnavailable, `invalid`, true},
{"protocol upgrade", http.StatusSwitchingProtocols, `{"message":"response"}`, true},
} {
t.Run(tc.name, func(t *testing.T) {
body := &loadURLBody{Reader: strings.NewReader(tc.body)}
http.DefaultClient = &http.Client{Transport: loadURLTransport(func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: tc.status,
Status: fmt.Sprintf("%d %s", tc.status, http.StatusText(tc.status)),
Header: make(http.Header),
Body: body,
Request: r,
}, nil
})}
doc, err := LoadURL("http://example.test/document")
if (err != nil) != tc.wantErr {
t.Fatalf("LoadURL = (%v, %v), want error: %v", doc, err, tc.wantErr)
}
if !body.closed {
t.Error("response body was not closed")
}
if (tc.status < 200 || tc.status >= 300) && body.read {
t.Error("HTTP error body was read")
}
})
}
}
func parseString(s string) (*Node, error) {
return Parse(strings.NewReader(s))
}
func TestParseJsonNumberArray(t *testing.T) {
s := `[1,2,3,4,5,6]`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
var values []float64
for _, n := range doc.ChildNodes() {
values = append(values, n.Value().(float64))
}
expected := []float64{1, 2, 3, 4, 5, 6}
if p1, p2 := len(values), len(expected); p1 != p2 {
t.Fatalf("got %d elements but expected %d", p1, p2)
}
if !reflect.DeepEqual(values, expected) {
t.Fatalf("got %v but expected %v", values, expected)
}
}
func TestParseJsonObject(t *testing.T) {
s := `{
"name":"John",
"age":31,
"female":false
}`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
m := make(map[string]interface{})
for _, n := range doc.ChildNodes() {
m[n.Data] = n.Value()
}
expected := []struct {
name string
value interface{}
}{
{"name", "John"},
{"age", float64(31)},
{"female", false},
}
for _, v := range expected {
if e, g := v.value, m[v.name]; e != g {
t.Fatalf("expected %s = %v(%T),but %s = %v(%t)", v.name, e, e, v.name, g, g)
}
}
}
func TestTextNodeValuePreservesJSONType(t *testing.T) {
doc, err := parseString(`{"count": 12, "enabled": true, "name": "sensor"}`)
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
value interface{}
text string
}{
{"count", float64(12), "12"},
{"enabled", true, "true"},
{"name", "sensor", "sensor"},
} {
parent := doc.SelectElement(tc.name)
if parent == nil || parent.FirstChild == nil || parent.FirstChild.Type != TextNode {
t.Fatalf("missing text node for %s", tc.name)
}
child := parent.FirstChild
if got := child.Value(); !reflect.DeepEqual(got, tc.value) {
t.Errorf("%s text value = %v (%T), want %v (%T)", tc.name, got, got, tc.value, tc.value)
}
if child.Data != tc.text || parent.InnerText() != tc.text {
t.Errorf("%s rendered text changed: data=%q inner=%q", tc.name, child.Data, parent.InnerText())
}
}
}
func TestParseJsonObjectArray(t *testing.T) {
s := `[
{"models":[ "Fiesta", "Focus", "Mustang" ] }
]`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
first := doc.FirstChild
models := first.SelectElement("models")
if expected := reflect.ValueOf(models.Value()).Kind(); expected != reflect.Slice {
t.Fatalf("expected models is slice(Array) but got %v", expected)
}
expected := []string{"Fiesta", "Focus", "Mustang"}
var values []string
for _, v := range models.Value().([]interface{}) {
values = append(values, v.(string))
}
if !reflect.DeepEqual(expected, values) {
t.Fatalf("expected %v but got %v", expected, values)
}
}
func TestParseJson(t *testing.T) {
s := `{
"name":"John",
"age":30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
n := doc.SelectElement("name")
if n == nil {
t.Fatal("n is nil")
}
if n.NextSibling != nil {
t.Fatal("next sibling shoud be nil")
}
if e, g := "John", n.InnerText(); e != g {
t.Fatalf("expected %v but %v", e, g)
}
cars := doc.SelectElement("cars")
if e, g := 3, len(cars.ChildNodes()); e != g {
t.Fatalf("expected %v but %v", e, g)
}
}
func TestLargeFloat(t *testing.T) {
s := `{
"large_number": 365823929453
}`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
n := doc.SelectElement("large_number")
if n.Value() != float64(365823929453) {
t.Fatalf("expected %v but %v", "365823929453", n.InnerText())
}
}
func TestOutputXMLEscapesText(t *testing.T) {
doc, err := parseString(`{"a": "x < y && y > z", "b": ["<i>", "AT&T"]}`)
if err != nil {
t.Fatal(err)
}
out := doc.OutputXML()
want := `<?xml version="1.0" encoding="utf-8"?><root><a>x < y && y > z</a><b><i></b><b>AT&T</b></root>`
if out != want {
t.Fatalf("OutputXML() = %s, want %s", out, want)
}
// The output must be well-formed and decode back to the original text.
var v struct {
A string `xml:"a"`
B []string `xml:"b"`
}
if err := xml.Unmarshal([]byte(out), &v); err != nil {
t.Fatalf("OutputXML() is not well-formed XML: %v", err)
}
if v.A != "x < y && y > z" || len(v.B) != 2 || v.B[0] != "<i>" || v.B[1] != "AT&T" {
t.Fatalf("decoded %+v", v)
}
}
func TestLargeNumberText(t *testing.T) {
doc, err := parseString(`{"a":1000000,"b":123456789,"c":-0.000001,"d":1.5}`)
if err != nil {
t.Fatal(err)
}
for key, want := range map[string]string{"a": "1000000", "b": "123456789", "c": "-0.000001", "d": "1.5"} {
n := FindOne(doc, "//"+key)
if n == nil {
t.Fatalf("//%s not found", key)
}
if got := n.InnerText(); got != want {
t.Errorf("//%s InnerText() = %q, want %q", key, got, want)
}
}
if got, want := doc.OutputXML(), `<?xml version="1.0" encoding="utf-8"?><root><a>1000000</a><b>123456789</b><c>-0.000001</c><d>1.5</d></root>`; got != want {
t.Errorf("OutputXML() = %q, want %q", got, want)
}
}