From 6d8e9d325a7862f5c3b9d51206374621aefa9417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?CORNIERE=20R=C3=A9mi?= Date: Mon, 9 Dec 2019 13:31:01 +0100 Subject: [PATCH] Try removing decoder from IQ tests and changing writing method --- client.go | 3 +-- component.go | 12 +++++++++--- tcp_server_mock.go | 18 +++++++++++------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/client.go b/client.go index a5ad1bf..4d7857e 100644 --- a/client.go +++ b/client.go @@ -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, "%s%s", "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, "") + err = c.sendWithWriter(c.transport, []byte("")) return err } diff --git a/component.go b/component.go index 2f61aef..8b96240 100644 --- a/component.go +++ b/component.go @@ -85,7 +85,7 @@ func (c *Component) Resume(sm SMState) error { c.updateState(StateConnected) // Authentication - if _, err := fmt.Fprintf(c.transport, "%s", c.handshake(streamId)); err != nil { + if err := c.sendWithWriter(c.transport, []byte(fmt.Sprintf("%s", 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 } diff --git a/tcp_server_mock.go b/tcp_server_mock.go index 4afed80..efdda23 100644 --- a/tcp_server_mock.go +++ b/tcp_server_mock.go @@ -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`)