2
0
mirror of https://github.com/FluuxIO/go-xmpp synced 2024-11-11 07:11:03 +00:00

Code cleanup

This commit is contained in:
Mickael Remond 2017-10-20 16:53:15 +02:00
parent 228cb14491
commit 4ac645a9ec
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3

View File

@ -6,12 +6,15 @@ import (
"fmt" "fmt"
"net" "net"
"testing" "testing"
"time"
) )
const ( const (
// Default port is not standard XMPP port to avoid interfering // Default port is not standard XMPP port to avoid interfering
// with local running XMPP server // with local running XMPP server
testXMPPAddress = "localhost:15222" testXMPPAddress = "localhost:15222"
defaultTimeout = 2 * time.Second
) )
func TestClient_Connect(t *testing.T) { func TestClient_Connect(t *testing.T) {
@ -44,17 +47,20 @@ func handlerConnectSuccess(t *testing.T, c net.Conn) {
decoder := xml.NewDecoder(c) decoder := xml.NewDecoder(c)
checkOpenStream(t, c, decoder) checkOpenStream(t, c, decoder)
sendStreamFeatures(t, c, decoder) sendStreamFeatures(t, c, decoder) // Send initial features
readAuth(t, decoder) readAuth(t, decoder)
fmt.Fprintln(c, "<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>") fmt.Fprintln(c, "<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>")
checkOpenStream(t, c, decoder) checkOpenStream(t, c, decoder) // Reset stream
sendBindFeature(t, c, decoder) sendBindFeature(t, c, decoder) // Send post auth features
bind(t, c, decoder) bind(t, c, decoder)
} }
func checkOpenStream(t *testing.T, c net.Conn, decoder *xml.Decoder) { func checkOpenStream(t *testing.T, c net.Conn, decoder *xml.Decoder) {
for { c.SetDeadline(time.Now().Add(defaultTimeout))
defer c.SetDeadline(time.Time{})
for { // TODO clean up. That for loop is not elegant and I prefer bounded recursion.
var token xml.Token var token xml.Token
token, err := decoder.Token() token, err := decoder.Token()
if err != nil { if err != nil {
@ -76,7 +82,7 @@ func checkOpenStream(t *testing.T, c net.Conn, decoder *xml.Decoder) {
} }
} }
func sendStreamFeatures(t *testing.T, c net.Conn, decoder *xml.Decoder) { func sendStreamFeatures(t *testing.T, c net.Conn, _ *xml.Decoder) {
// This is a basic server, supporting only 1 stream feature: SASL Plain Auth // This is a basic server, supporting only 1 stream feature: SASL Plain Auth
features := `<stream:features> features := `<stream:features>
<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"> <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
@ -111,7 +117,7 @@ func readAuth(t *testing.T, decoder *xml.Decoder) string {
return "" return ""
} }
func sendBindFeature(t *testing.T, c net.Conn, decoder *xml.Decoder) { func sendBindFeature(t *testing.T, c net.Conn, _ *xml.Decoder) {
// This is a basic server, supporting only 1 stream feature: SASL Plain Auth // This is a basic server, supporting only 1 stream feature: SASL Plain Auth
features := `<stream:features> features := `<stream:features>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/> <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
@ -135,14 +141,13 @@ func bind(t *testing.T, c net.Conn, decoder *xml.Decoder) {
return return
} }
switch payload := iq.Payload.(type) { switch iq.Payload.(type) {
case *bindBind: case *bindBind:
fmt.Println("JID:", payload.Jid) result := `<iq id='%s' type='result'>
}
result := `<iq id='%s' type='result'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'> <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
<jid>%s</jid> <jid>%s</jid>
</bind> </bind>
</iq>` </iq>`
fmt.Fprintf(c, result, iq.Id, "test@localhost/test") // TODO use real JID fmt.Fprintf(c, result, iq.Id, "test@localhost/test") // TODO use real JID
}
} }