Codebase list golang-gomega / da1d0e4
Merge pull request #212 from aubm/fix-xml-charset Fixes xml decoding for non UTF-8 xml files Onsi Fakhouri authored 7 years ago GitHub committed 7 years ago
3 changed file(s) with 21 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
22 import (
33 "encoding/xml"
44 "fmt"
5 "io"
56 "reflect"
7 "strings"
68
79 "github.com/onsi/gomega/format"
10 "golang.org/x/net/html/charset"
811 )
912
1013 type MatchXMLMatcher struct {
2023 aval := &xmlNode{}
2124 eval := &xmlNode{}
2225
23 if err := xml.Unmarshal([]byte(actualString), aval); err != nil {
26 if err := newXmlDecoder(strings.NewReader(actualString)).Decode(aval); err != nil {
2427 return false, fmt.Errorf("Actual '%s' should be valid XML, but it is not.\nUnderlying error:%s", actualString, err)
2528 }
26 if err := xml.Unmarshal([]byte(expectedString), eval); err != nil {
29 if err := newXmlDecoder(strings.NewReader(expectedString)).Decode(eval); err != nil {
2730 return false, fmt.Errorf("Expected '%s' should be valid XML, but it is not.\nUnderlying error:%s", expectedString, err)
2831 }
2932
5558 }
5659 return actualString, expectedString, nil
5760 }
61
62 func newXmlDecoder(reader io.Reader) *xml.Decoder {
63 dec := xml.NewDecoder(reader)
64 dec.CharsetReader = charset.NewReaderLabel
65 return dec
66 }
1616 sample_06 = readFileContents("test_data/xml/sample_06.xml")
1717 sample_07 = readFileContents("test_data/xml/sample_07.xml")
1818 sample_08 = readFileContents("test_data/xml/sample_08.xml")
19
20 sample_11 = readFileContents("test_data/xml/sample_11.xml")
1921 )
2022
2123 Context("When passed stringifiables", func() {
2729 Ω(sample_01).ShouldNot(MatchXML(sample_05)) // different structures
2830 Ω(sample_06).ShouldNot(MatchXML(sample_07)) // same xml names with different namespaces
2931 Ω(sample_07).ShouldNot(MatchXML(sample_08)) // same structures with different values
32 Ω(sample_11).Should(MatchXML(sample_11)) // with non UTF-8 encoding
3033 })
3134
3235 It("should work with byte arrays", func() {
0 <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
1 <note>
2 <to>Tove</to>
3 <from>Jani</from>
4 <heading>Reminder</heading>
5 <body>Don't forget me this weekend!</body>
6 </note>