1package xmpp 2 3import ( 4 "encoding/xml" 5 "reflect" 6 7 "github.com/coyim/coyim/xmpp/data" 8 9 . "gopkg.in/check.v1" 10) 11 12type XMLXMPPSuite struct{} 13 14var _ = Suite(&XMLXMPPSuite{}) 15 16func (s *XMLXMPPSuite) Test_xmlEscape_escapesSpecialCharactersButNotRegularOnes(c *C) { 17 res := xmlEscape("abc\"<foo>bar>bar\\and &her'e;") 18 c.Assert(res, Equals, "abc"<foo>bar>bar\\and &her'e;") 19} 20 21type testFoo struct { 22 XMLName xml.Name `xml:"http://etherx.jabber.org/streams foo"` 23 To string `xml:"to,attr"` 24} 25 26func (s *XMLXMPPSuite) Test_next_usesCustomStorageIfAvailable(c *C) { 27 mockIn := &mockConnIOReaderWriter{read: []byte("<stream:foo xmlns:stream='http://etherx.jabber.org/streams' to='hello'></stream:foo>")} 28 conn := conn{ 29 in: xml.NewDecoder(mockIn), 30 customStorage: map[xml.Name]reflect.Type{ 31 xml.Name{Space: NsStream, Local: "foo"}: reflect.TypeOf(testFoo{}), 32 }, 33 } 34 35 nm, i, e := next(&conn) 36 c.Assert(e, IsNil) 37 c.Assert(nm, Equals, xml.Name{Space: NsStream, Local: "foo"}) 38 val, _ := i.(*testFoo) 39 c.Assert(*val, Equals, testFoo{XMLName: nm, To: "hello"}) 40} 41 42func (s *XMLXMPPSuite) Test_next_causesErrorWhenTryingToDecodeWrong(c *C) { 43 mockIn := &mockConnIOReaderWriter{read: []byte("<stream:foo xmlns:stream='http://etherx.jabber.org/streams'><something></foo></stream:foo>")} 44 conn := conn{ 45 in: xml.NewDecoder(mockIn), 46 customStorage: map[xml.Name]reflect.Type{ 47 xml.Name{Space: NsStream, Local: "foo"}: reflect.TypeOf(testFoo{}), 48 }, 49 } 50 51 _, _, e := next(&conn) 52 c.Assert(e.Error(), Equals, "XML syntax error on line 1: element <something> closed by </foo>") 53} 54 55func (s *XMLXMPPSuite) Test_ClientMesage_unmarshalsXMPPExtensions(c *C) { 56 datax := ` 57 <message 58 xmlns='jabber:client' 59 xmlns:stream='http://etherx.jabber.org/streams' 60 id='coyim1234' 61 from='bernardo@shakespeare.lit/pda' 62 to='francisco@shakespeare.lit/elsinore' 63 type='chat'> 64 <composing xmlns='http://jabber.org/protocol/chatstates'/> 65 <x xmlns='jabber:x:event'> 66 <offline/> 67 <delivered/> 68 <composing/> 69 </x> 70 </message> 71 ` 72 73 v := &data.ClientMessage{} 74 err := xml.Unmarshal([]byte(datax), v) 75 c.Assert(err, Equals, nil) 76 77 c.Assert(v.ID, Equals, "coyim1234") 78 c.Assert(v.From, Equals, "bernardo@shakespeare.lit/pda") 79 c.Assert(v.To, Equals, "francisco@shakespeare.lit/elsinore") 80 c.Assert(v.Type, Equals, "chat") 81 c.Assert(v.Extensions, DeepEquals, data.Extensions{ 82 &data.Extension{XMLName: xml.Name{Space: "http://jabber.org/protocol/chatstates", Local: "composing"}}, 83 &data.Extension{ 84 XMLName: xml.Name{Space: "jabber:x:event", Local: "x"}, 85 Body: "\n\t <offline/>\n\t\t\t<delivered/>\n\t\t\t<composing/>\n\t\t", 86 }, 87 }) 88} 89