Try removing decoder from IQ tests and changing writing method

This commit is contained in:
CORNIERE Rémi 2019-12-09 13:31:01 +01:00
parent e675e65a59
commit 6d8e9d325a
3 changed files with 21 additions and 12 deletions

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/xml"
"errors"
"fmt"
"io"
"net"
"time"
@ -200,7 +199,7 @@ func (c *Client) Resume(state SMState) error {
//fmt.Fprintf(client.conn, "<presence xml:lang='en'><show>%s</show><status>%s</status></presence>", "chat", "Online")
// TODO: Do we always want to send initial presence automatically ?
// Do we need an option to avoid that or do we rely on client to send the presence itself ?
_, err = fmt.Fprintf(c.transport, "<presence/>")
err = c.sendWithWriter(c.transport, []byte("<presence/>"))
return err
}

View File

@ -85,7 +85,7 @@ func (c *Component) Resume(sm SMState) error {
c.updateState(StateConnected)
// Authentication
if _, err := fmt.Fprintf(c.transport, "<handshake>%s</handshake>", c.handshake(streamId)); err != nil {
if err := c.sendWithWriter(c.transport, []byte(fmt.Sprintf("<handshake>%s</handshake>", c.handshake(streamId)))); err != nil {
c.updateState(StateStreamError)
return NewConnError(errors.New("cannot send handshake "+err.Error()), false)
@ -159,12 +159,18 @@ func (c *Component) Send(packet stanza.Packet) error {
return errors.New("cannot marshal packet " + err.Error())
}
if _, err := fmt.Fprintf(transport, string(data)); err != nil {
if err := c.sendWithWriter(transport, data); err != nil {
return errors.New("cannot send packet " + err.Error())
}
return nil
}
func (c *Component) sendWithWriter(writer io.Writer, packet []byte) error {
var err error
_, err = writer.Write(packet)
return err
}
// SendIQ sends an IQ set or get stanza to the server. If a result is received
// the provided handler function will automatically be called.
//
@ -195,7 +201,7 @@ func (c *Component) SendRaw(packet string) error {
}
var err error
_, err = fmt.Fprintf(transport, packet)
err = c.sendWithWriter(transport, []byte(packet))
return err
}

View File

@ -117,21 +117,25 @@ func (mock *ServerMock) loop() {
//======================================================================================================================
func respondToIQ(t *testing.T, c net.Conn) {
// Decoder to parse the request
decoder := xml.NewDecoder(c)
iqReq, err := receiveIq(c, decoder)
recvBuf := make([]byte, 1024)
var iqR stanza.IQ
_, err := c.Read(recvBuf[:]) // recv data
if err != nil {
t.Fatalf("failed to receive IQ : %s", err.Error())
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
t.Errorf("read timeout: %s", err)
} else {
t.Errorf("read error: %s", err)
}
}
xml.Unmarshal(recvBuf, &iqR)
if !iqReq.IsValid() {
if !iqR.IsValid() {
mockIQError(c)
return
}
// Crafting response
iqResp := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, From: iqReq.To, To: iqReq.From, Id: iqReq.Id, Lang: "en"})
iqResp := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, From: iqR.To, To: iqR.From, Id: iqR.Id, Lang: "en"})
disco := iqResp.DiscoInfo()
disco.AddFeatures("vcard-temp",
`http://jabber.org/protocol/address`)