/
xml.go
64 lines (48 loc) · 1.22 KB
/
xml.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
// Copyright 2015 Volker Dobler. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// xml.go contains XPath checks against XML documents.
package ht
import (
"fmt"
"gopkg.in/xmlpath.v2"
)
func init() {
RegisterCheck(&XML{})
}
// ----------------------------------------------------------------------------
// XML
// XML allows to check XML request bodies.
type XML struct {
// Path is a XPath expression understood by gopkg.in/xmlpath.v2.
Path string
// Condition the first element addressed by Path must fulfill.
Condition
path *xmlpath.Path
}
// Execute implements Check's Execute method.
func (x *XML) Execute(t *Test) error {
if t.Response.BodyErr != nil {
return CantCheck{t.Response.BodyErr}
}
root, err := xmlpath.Parse(t.Response.Body())
if err != nil {
return err
}
if s, ok := x.path.String(root); !ok {
return fmt.Errorf("No such element %s", x.Path)
} else if e := x.Fulfilled(s); err != nil {
return e
}
return nil
}
// Prepare implements Check's Prepare method.
func (x *XML) Prepare(*Test) error {
p, err := xmlpath.Compile(x.Path)
if err != nil {
return err
}
x.path = p
return nil
}
var _ Preparable = &XML{}