From 07b0d2d14dd10004958730964ad7bfc6f43dd84b Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Tue, 13 Feb 2018 23:04:13 +0100 Subject: [PATCH] Cleanup and add test for IOT control set parsing --- cmd/xmpp_jukebox/xmpp_jukebox.go | 14 +++++++------- component.go | 2 +- component_test.go | 2 +- control_test.go | 28 ++++++++++++++++++++++++++++ doc.go | 2 +- iq.go | 22 ---------------------- 6 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 control_test.go diff --git a/cmd/xmpp_jukebox/xmpp_jukebox.go b/cmd/xmpp_jukebox/xmpp_jukebox.go index c5cdf28..76e0d66 100644 --- a/cmd/xmpp_jukebox/xmpp_jukebox.go +++ b/cmd/xmpp_jukebox/xmpp_jukebox.go @@ -40,11 +40,11 @@ func main() { for packet := range client.Recv() { switch packet := packet.(type) { - case *xmpp.Message: - processMessage(client, p, packet) - case *xmpp.IQ: - processIq(client, p, packet) - case *xmpp.Presence: + case xmpp.Message: + processMessage(client, p, &packet) + case xmpp.IQ: + processIq(client, p, &packet) + case xmpp.Presence: // Do nothing with received presence default: fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", packet) @@ -77,7 +77,7 @@ func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) { playSCURL(p, url) setResponse := new(iot.ControlSetResponse) reply := xmpp.IQ{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "result", Id: packet.Id}, Payload: []xmpp.IQPayload{setResponse}} - client.SendRaw(reply.XMPPFormat()) + client.Send(reply) // TODO add Soundclound artist / title retrieval sendUserTune(client, "Radiohead", "Spectre") default: @@ -100,7 +100,7 @@ func playSCURL(p *mpg123.Player, rawURL string) { func connectXmpp(jid string, password string, address string) (client *xmpp.Client, err error) { xmppOptions := xmpp.Options{Address: address, - Jid: jid, Password: password, PacketLogger: os.Stdout, + Jid: jid, Password: password, PacketLogger: os.Stdout, Insecure: true, Retry: 10} if client, err = xmpp.NewClient(xmppOptions); err != nil { diff --git a/component.go b/component.go index 57bbabf..d14bf46 100644 --- a/component.go +++ b/component.go @@ -1,4 +1,4 @@ -package xmpp +package xmpp // import "fluux.io/xmpp" import ( "crypto/sha1" diff --git a/component_test.go b/component_test.go index e3b6817..e23b816 100644 --- a/component_test.go +++ b/component_test.go @@ -1,4 +1,4 @@ -package xmpp +package xmpp // import "fluux.io/xmpp" import "testing" diff --git a/control_test.go b/control_test.go new file mode 100644 index 0000000..5c2ec01 --- /dev/null +++ b/control_test.go @@ -0,0 +1,28 @@ +package xmpp // import "fluux.io/xmpp" + +import ( + "encoding/xml" + "testing" + + "fluux.io/xmpp/iot" +) + +func TestControlSet(t *testing.T) { + packet := ` + + + + + +` + + parsedIQ := IQ{} + data := []byte(packet) + if err := xml.Unmarshal(data, &parsedIQ); err != nil { + t.Errorf("Unmarshal(%s) returned error", data) + } + + if cs, ok := parsedIQ.Payload[0].(*iot.ControlSet); !ok { + t.Errorf("Paylod is not an iot control set: %v", cs) + } +} diff --git a/doc.go b/doc.go index 5e1c074..fde621f 100644 --- a/doc.go +++ b/doc.go @@ -29,4 +29,4 @@ Fluux XMPP has been primarily tested with ejabberd (https://www.ejabberd.im) but it should work with any XMPP compliant server. */ -package xmpp +package xmpp // import "fluux.io/xmpp" diff --git a/iq.go b/iq.go index cffbc31..7740d6a 100644 --- a/iq.go +++ b/iq.go @@ -2,7 +2,6 @@ package xmpp // import "fluux.io/xmpp" import ( "encoding/xml" - "fmt" "reflect" "strconv" @@ -236,27 +235,6 @@ func (iq *IQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { } } -// XMPPFormat returns the string representation of the XMPP packet. -// TODO: Should I simply rely on xml.Marshal ? -func (iq *IQ) XMPPFormat() string { - if iq.Payload != nil { - var payload []byte - var err error - if payload, err = xml.Marshal(iq.Payload); err != nil { - return fmt.Sprintf(""+ - "", - iq.To, iq.Type, iq.Id) - } - return fmt.Sprintf(""+ - "%s", - iq.To, iq.Type, iq.Id, payload) - } - return fmt.Sprintf(""+ - "%s", - iq.To, iq.Type, iq.Id, - iq.RawXML) -} - // ============================================================================ // Generic IQ Payload