diff --git a/client.go b/client.go index 58d0da1..6275920 100644 --- a/client.go +++ b/client.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "errors" "fmt" + "io" "net" "time" @@ -211,7 +212,7 @@ func (c *Client) Send(packet stanza.Packet) error { return errors.New("cannot marshal packet " + err.Error()) } - return c.sendWithLogger(string(data)) + return c.sendWithWriter(c.Session.streamLogger, data) } // SendRaw sends an XMPP stanza as a string to the server. @@ -224,12 +225,12 @@ func (c *Client) SendRaw(packet string) error { return errors.New("client is not connected") } - return c.sendWithLogger(packet) + return c.sendWithWriter(c.Session.streamLogger, []byte(packet)) } -func (c *Client) sendWithLogger(packet string) error { +func (c *Client) sendWithWriter(writer io.Writer, packet []byte) error { var err error - _, err = fmt.Fprintf(c.Session.streamLogger, packet) + _, err = writer.Write(packet) return err } diff --git a/client_internal_test.go b/client_internal_test.go new file mode 100644 index 0000000..6daef09 --- /dev/null +++ b/client_internal_test.go @@ -0,0 +1,19 @@ +package xmpp + +import ( + "bytes" + "testing" +) + +func TestClient_Send(t *testing.T) { + buffer := bytes.NewBufferString("") + client := Client{} + data := []byte("https://da.wikipedia.org/wiki/J%C3%A6vnd%C3%B8gn") + if err := client.sendWithWriter(buffer, data); err != nil { + t.Errorf("Writing failed: %v", err) + } + + if buffer.String() != string(data) { + t.Errorf("Incorrect value sent to buffer: '%s'", buffer.String()) + } +}