structs_test.go
changeset 98 c9cc4eda6dce
parent 72 53f15893a1a7
child 110 7696e6a01709
equal deleted inserted replaced
88:d2ec96c80efe 98:c9cc4eda6dce
     4 
     4 
     5 package xmpp
     5 package xmpp
     6 
     6 
     7 import (
     7 import (
     8 	"bytes"
     8 	"bytes"
       
     9 	"encoding/xml"
     9 	"testing"
    10 	"testing"
    10 	"xml"
       
    11 )
    11 )
    12 
    12 
    13 func assertEquals(t *testing.T, expected, observed string) {
    13 func assertEquals(t *testing.T, expected, observed string) {
    14 	if expected != observed {
    14 	if expected != observed {
    15 		t.Errorf("Expected:\n%s\nObserved:\n%s\n", expected,
    15 		t.Errorf("Expected:\n%s\nObserved:\n%s\n", expected,
    18 }
    18 }
    19 
    19 
    20 func TestJid(t *testing.T) {
    20 func TestJid(t *testing.T) {
    21 	str := "user@domain/res"
    21 	str := "user@domain/res"
    22 	jid := &JID{}
    22 	jid := &JID{}
    23 	if !jid.Set(str) {
    23 	if err := jid.Set(str); err != nil {
    24 		t.Errorf("Set(%s) failed\n", str)
    24 		t.Errorf("Set(%s) failed: %s", str, err)
    25 	}
    25 	}
    26 	assertEquals(t, "user", jid.Node)
    26 	assertEquals(t, "user", jid.Node)
    27 	assertEquals(t, "domain", jid.Domain)
    27 	assertEquals(t, "domain", jid.Domain)
    28 	assertEquals(t, "res", jid.Resource)
    28 	assertEquals(t, "res", jid.Resource)
    29 	assertEquals(t, str, jid.String())
    29 	assertEquals(t, str, jid.String())
    30 
    30 
    31 	str = "domain.tld"
    31 	str = "domain.tld"
    32 	if !jid.Set(str) {
    32 	if err := jid.Set(str); err != nil {
    33 		t.Errorf("Set(%s) failed\n", str)
    33 		t.Errorf("Set(%s) failed: %s", str, err)
    34 	}
    34 	}
    35 	if jid.Node != "" {
    35 	if jid.Node != "" {
    36 		t.Errorf("Node: %v\n", jid.Node)
    36 		t.Errorf("Node: %v\n", jid.Node)
    37 	}
    37 	}
    38 	assertEquals(t, "domain.tld", jid.Domain)
    38 	assertEquals(t, "domain.tld", jid.Domain)
    41 	}
    41 	}
    42 	assertEquals(t, str, jid.String())
    42 	assertEquals(t, str, jid.String())
    43 }
    43 }
    44 
    44 
    45 func assertMarshal(t *testing.T, expected string, marshal interface{}) {
    45 func assertMarshal(t *testing.T, expected string, marshal interface{}) {
    46 	buf := bytes.NewBuffer(nil)
    46 	var buf bytes.Buffer
    47 	xml.Marshal(buf, marshal)
    47 	enc := xml.NewEncoder(&buf)
    48 	observed := string(buf.Bytes())
    48 	enc.Context.Map[NsClient] = ""
       
    49 	enc.Context.Map[NsStream] = "stream"
       
    50 	err := enc.Encode(marshal)
       
    51 	if err != nil {
       
    52 		t.Errorf("Marshal error for %s: %s", marshal, err)
       
    53 	}
       
    54 	observed := buf.String()
    49 	assertEquals(t, expected, observed)
    55 	assertEquals(t, expected, observed)
    50 }
    56 }
    51 
    57 
    52 func TestStreamMarshal(t *testing.T) {
    58 func TestStreamMarshal(t *testing.T) {
    53 	s := &stream{To: "bob"}
    59 	s := &stream{To: "bob"}
    54 	exp := `<stream:stream xmlns="jabber:client"` +
    60 	exp := `<stream:stream xmlns="` + NsClient +
    55 		` xmlns:stream="` + NsStream + `" to="bob">`
    61 		`" xmlns:stream="` + NsStream + `" to="bob">`
    56 	assertMarshal(t, exp, s)
    62 	assertEquals(t, exp, s.String())
    57 
    63 
    58 	s = &stream{To: "bob", From: "alice", Id: "#3", Version: "5.3"}
    64 	s = &stream{To: "bob", From: "alice", Id: "#3", Version: "5.3"}
    59 	exp = `<stream:stream xmlns="jabber:client"` +
    65 	exp = `<stream:stream xmlns="` + NsClient +
    60 		` xmlns:stream="` + NsStream + `" to="bob" from="alice"` +
    66 		`" xmlns:stream="` + NsStream + `" to="bob" from="alice"` +
    61 		` id="#3" version="5.3">`
    67 		` id="#3" version="5.3">`
    62 	assertMarshal(t, exp, s)
    68 	assertEquals(t, exp, s.String())
    63 
    69 
    64 	s = &stream{Lang: "en_US"}
    70 	s = &stream{Lang: "en_US"}
    65 	exp = `<stream:stream xmlns="jabber:client"` +
    71 	exp = `<stream:stream xmlns="` + NsClient +
    66 		` xmlns:stream="` + NsStream + `" xml:lang="en_US">`
    72 		`" xmlns:stream="` + NsStream + `" xml:lang="en_US">`
    67 	assertMarshal(t, exp, s)
    73 	assertEquals(t, exp, s.String())
    68 }
    74 }
    69 
    75 
    70 func TestStreamErrorMarshal(t *testing.T) {
    76 func TestStreamErrorMarshal(t *testing.T) {
    71 	name := xml.Name{Space: NsStreams, Local: "ack"}
    77 	name := xml.Name{Space: NsStreams, Local: "ack"}
    72 	e := &streamError{Any: Generic{XMLName: name}}
    78 	e := &streamError{Any: Generic{XMLName: name}}
    95 		` xml:lang="en"><foo>text</foo></iq>`
   101 		` xml:lang="en"><foo>text</foo></iq>`
    96 	st, err := ParseStanza(str)
   102 	st, err := ParseStanza(str)
    97 	if err != nil {
   103 	if err != nil {
    98 		t.Fatalf("iq: %v", err)
   104 		t.Fatalf("iq: %v", err)
    99 	}
   105 	}
   100 	assertEquals(t, "iq", st.GetName())
   106 	iq, ok := st.(*Iq)
   101 	assertEquals(t, "alice", st.GetTo())
   107 	if !ok {
   102 	assertEquals(t, "bob", st.GetFrom())
   108 		t.Fatalf("not iq: %v", st)
   103 	assertEquals(t, "1", st.GetId())
   109 	}
   104 	assertEquals(t, "A", st.GetType())
   110 	assertEquals(t, "iq", iq.XMLName.Local)
   105 	assertEquals(t, "en", st.GetLang())
   111 	assertEquals(t, "alice", iq.To)
   106 	if st.GetError() != nil {
   112 	assertEquals(t, "bob", iq.From)
   107 		t.Errorf("iq: error %v", st.GetError())
   113 	assertEquals(t, "1", iq.Id)
       
   114 	assertEquals(t, "A", iq.Type)
       
   115 	assertEquals(t, "en", iq.Lang)
       
   116 	if iq.Error != nil {
       
   117 		t.Errorf("iq: error %v", iq.Error)
   108 	}
   118 	}
   109 	if st.innerxml() == "" {
   119 	if st.innerxml() == "" {
   110 		t.Errorf("iq: empty child")
   120 		t.Errorf("iq: empty child")
   111 	}
   121 	}
   112 	assertEquals(t, "<foo>text</foo>", st.innerxml())
   122 	assertEquals(t, "<foo>text</foo>", st.innerxml())
       
   123 	assertEquals(t, st.innerxml(), iq.Innerxml)
   113 
   124 
   114 	str = `<message to="alice" from="bob"/>`
   125 	str = `<message to="alice" from="bob"/>`
   115 	st, err = ParseStanza(str)
   126 	st, err = ParseStanza(str)
   116 	if err != nil {
   127 	if err != nil {
   117 		t.Fatalf("message: %v", err)
   128 		t.Fatalf("message: %v", err)
   118 	}
   129 	}
   119 	assertEquals(t, "message", st.GetName())
   130 	m, ok := st.(*Message)
   120 	assertEquals(t, "alice", st.GetTo())
   131 	if !ok {
   121 	assertEquals(t, "bob", st.GetFrom())
   132 		t.Fatalf("not message: %v", st)
   122 	assertEquals(t, "", st.GetId())
   133 	}
   123 	assertEquals(t, "", st.GetLang())
   134 	assertEquals(t, "message", m.XMLName.Local)
   124 	if st.GetError() != nil {
   135 	assertEquals(t, "alice", m.To)
   125 		t.Errorf("message: error %v", st.GetError())
   136 	assertEquals(t, "bob", m.From)
       
   137 	assertEquals(t, "", m.Id)
       
   138 	assertEquals(t, "", m.Lang)
       
   139 	if m.Error != nil {
       
   140 		t.Errorf("message: error %v", m.Error)
   126 	}
   141 	}
   127 	if st.innerxml() != "" {
   142 	if st.innerxml() != "" {
   128 		t.Errorf("message: child %v", st.innerxml())
   143 		t.Errorf("message: child %v", st.innerxml())
   129 	}
   144 	}
   130 
   145 
   131 	str = `<presence/>`
   146 	str = `<presence/>`
   132 	st, err = ParseStanza(str)
   147 	st, err = ParseStanza(str)
   133 	if err != nil {
   148 	if err != nil {
   134 		t.Fatalf("presence: %v", err)
   149 		t.Fatalf("presence: %v", err)
   135 	}
   150 	}
   136 	assertEquals(t, "presence", st.GetName())
   151 	_, ok = st.(*Presence)
       
   152 	if !ok {
       
   153 		t.Fatalf("not presence: %v", st)
       
   154 	}
   137 }
   155 }
   138 
   156 
   139 func TestMarshalEscaping(t *testing.T) {
   157 func TestMarshalEscaping(t *testing.T) {
   140 	msg := &Message{Body: &Generic{XMLName: xml.Name{Local: "body"},
   158 	msg := &Message{Body: &Generic{XMLName: xml.Name{Local: "body"},
   141 		Chardata: `&<!-- "`}}
   159 		Chardata: `&<!-- "`}}