sum7
/
yaja
Archived
1
0
Fork 0

[TEST] add for client (comm.go)

This commit is contained in:
Martin/Geno 2018-03-05 09:17:05 +01:00
parent 46e5efd98b
commit 64ee706b4a
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
2 changed files with 51 additions and 1 deletions

View File

@ -32,6 +32,56 @@ func TestRead(t *testing.T) {
assert.Error(err) assert.Error(err)
} }
func TestDecode(t *testing.T) {
assert := assert.New(t)
server, clientConn := net.Pipe()
client := &Client{
Logging: log.WithField("test", "decode"),
}
client.setConnection(clientConn)
go server.Write([]byte(`<message xmlns="jabber:client" to="a@example.com"></message>`))
msg := &xmpp.MessageClient{}
err := client.ReadDecode(msg)
assert.NoError(err)
assert.Equal("a@example.com", msg.To.String())
go server.Write([]byte(`<iq xmlns="jabber:client" to="a@example.com"></iq>`))
iq := &xmpp.IQClient{}
err = client.ReadDecode(iq)
assert.NoError(err)
assert.Equal("a@example.com", iq.To.String())
assert.Nil(iq.Ping)
go server.Write([]byte(`<iq xmlns="jabber:client" type="result"><ping xmlns="urn:xmpp:ping"/></iq>`))
err = client.ReadDecode(iq)
assert.NoError(err)
assert.NotNil(iq.Ping)
wgWait := &sync.WaitGroup{}
go server.Write([]byte(`<iq xmlns="jabber:client" type="get"><ping xmlns="urn:xmpp:ping"/></iq>`))
wgWait.Add(1)
go func() {
_, err := read(xml.NewDecoder(server))
assert.NoError(err)
wgWait.Done()
}()
err = client.ReadDecode(iq)
wgWait.Wait()
assert.NoError(err)
go server.Write([]byte(`<>`))
err = client.ReadDecode(msg)
assert.Error(err)
}
func TestSend(t *testing.T) { func TestSend(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)

View File

@ -46,7 +46,7 @@ func (client *Client) Start() error {
iq := &xmpp.IQClient{} iq := &xmpp.IQClient{}
err = client.Decode(iq, element) err = client.Decode(iq, element)
if err == nil { if err == nil {
if iq.Ping != nil { if iq.Ping != nil && iq.Type == xmpp.IQTypeGet {
client.Logging.Info("client.Start: answer ping") client.Logging.Info("client.Start: answer ping")
iq.Type = xmpp.IQTypeResult iq.Type = xmpp.IQTypeResult
iq.To = iq.From iq.To = iq.From