-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_xml_test.go
67 lines (51 loc) · 1.49 KB
/
writer_xml_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
package emissione_test
import (
"github.com/kernle32dll/emissione-go"
"encoding/xml"
"net/http/httptest"
"testing"
)
func TestXMLWriter(t *testing.T) {
type SampleObject struct {
TestProp int `xml:"TestProp"`
}
sampleObject := SampleObject{42}
t.Run("simple", func(t *testing.T) {
xmlMarshalled, mErr := xml.Marshal(sampleObject)
if mErr != nil {
t.Error(mErr)
}
writer := emissione.NewXmlWriter()
w := httptest.NewRecorder()
if writer.Write(w, sampleObject) != nil {
t.Errorf("TestXMLWriter() unpexected error")
}
want := string(xmlMarshalled)
if got := w.Body.String(); got != want {
t.Errorf("TestXMLWriter() = %v, want %v", got, want)
}
wantHeader := "application/xml;charset=utf-8"
if got := w.Header().Get("Content-Type"); got != wantHeader {
t.Errorf("TestXMLWriter() = %v, want %v", got, wantHeader)
}
})
t.Run("indent", func(t *testing.T) {
xmlMarshalled, mErr := xml.MarshalIndent(sampleObject, "x", "**")
if mErr != nil {
t.Error(mErr)
}
writer := emissione.NewXmlIndentWriter("x", "**")
w := httptest.NewRecorder()
if writer.Write(w, sampleObject) != nil {
t.Errorf("TestXMLWriter() unpexected error")
}
want := string(xmlMarshalled)
if got := w.Body.String(); got != want {
t.Errorf("TestXMLWriter() = %v, want %v", got, want)
}
wantHeader := "application/xml;charset=utf-8"
if got := w.Header().Get("Content-Type"); got != wantHeader {
t.Errorf("TestXMLWriter() = %v, want %v", got, wantHeader)
}
})
}