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" "context"
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt"
"io" "io"
"net" "net"
"time" "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") //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 ? // 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 ? // 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 return err
} }

View File

@ -85,7 +85,7 @@ func (c *Component) Resume(sm SMState) error {
c.updateState(StateConnected) c.updateState(StateConnected)
// Authentication // 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) c.updateState(StateStreamError)
return NewConnError(errors.New("cannot send handshake "+err.Error()), false) 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()) 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 errors.New("cannot send packet " + err.Error())
} }
return nil 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 // SendIQ sends an IQ set or get stanza to the server. If a result is received
// the provided handler function will automatically be called. // the provided handler function will automatically be called.
// //
@ -195,7 +201,7 @@ func (c *Component) SendRaw(packet string) error {
} }
var err error var err error
_, err = fmt.Fprintf(transport, packet) err = c.sendWithWriter(transport, []byte(packet))
return err return err
} }

View File

@ -117,21 +117,25 @@ func (mock *ServerMock) loop() {
//====================================================================================================================== //======================================================================================================================
func respondToIQ(t *testing.T, c net.Conn) { func respondToIQ(t *testing.T, c net.Conn) {
// Decoder to parse the request recvBuf := make([]byte, 1024)
decoder := xml.NewDecoder(c) var iqR stanza.IQ
_, err := c.Read(recvBuf[:]) // recv data
iqReq, err := receiveIq(c, decoder)
if err != nil { 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) mockIQError(c)
return return
} }
// Crafting response // 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 := iqResp.DiscoInfo()
disco.AddFeatures("vcard-temp", disco.AddFeatures("vcard-temp",
`http://jabber.org/protocol/address`) `http://jabber.org/protocol/address`)