forked from ivpusic/httpcheck
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtester_body_xml.go
61 lines (51 loc) · 1.52 KB
/
tester_body_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
package httpcheck
import (
"bytes"
"encoding/xml"
"io"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// XML //////////////////////////////////////////////////////////
// WithXML adds a xml encoded body to the request.
func (tt *Tester) WithXML(body any) *Tester {
encoded, err := xml.Marshal(body)
require.NoError(tt.t, err)
return tt.WithBody(encoded)
}
// Deprecated: WithXml adds a xml encoded body to the request.
func (tt *Tester) WithXml(body any) *Tester {
return tt.WithXML(body)
}
// HasXML checks if body contains xml with provided value.
func (tt *Tester) HasXML(expected any) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
b, err := xml.Marshal(expected)
require.NoError(tt.t, err)
assert.Equal(tt.t, string(b), string(body))
return tt
}
// MustHasXML checks if body contains xml with provided value.
func (tt *Tester) MustHasXML(expected any) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
b, err := xml.Marshal(expected)
require.NoError(tt.t, err)
require.Equal(tt.t, string(b), string(body))
return tt
}
// Deprecated: HasXml checks if body contains xml with provided value.
//
//nolint:golint
func (tt *Tester) HasXml(expected any) *Tester {
return tt.HasXML(expected)
}