'use strict';

var ltx = require('node-xmpp-core').ltx
  , C2SServer = require('../index').C2SServer
  , net = require('net')

require('should')

describe('Issues', function() {
    it('Shouldn\'t crash on invalid JID', function(done) {
        var Router = require('../index').Router
        var router = new Router()
        var message = new ltx.Element(
            'message', { to: 'foo@localhost', from: 'foo@localhost' }
        ).c('body').t('Hello')
        router.send(message)
        done()
    })
})

describe('Stream without proper "to" attribute', function() {

    function testEmptyTo(to) {
        var port = 5222
        var c2s
        var client

        var streamData = false
        var streamClosed = false
        var streamError = ''

        before(function(done) {
            c2s = new C2SServer({
                port: port,
                domain: 'localhost'
            })
            c2s.on('connect', function(stream) {
                stream.on('error', function(error) {
                    streamError = error
                })
            })
            c2s.on('online', function() {
                var stanza = '<?xml version="1.0"?>' +
                    '<stream:stream ' + to + ' xmlns="jabber:client" ' +
                    'xmlns:stream="http://etherx.jabber.org/streams" ' +
                    'version="1.0">'
                client = net.connect({ port: port }, function() {
                    client.write(stanza)
                })
                client.on('data', function(data) {
                    if (/host-unknown/.test(data.toString())) {
                        streamData = true
                    }
                })

                client.on('end', function() {
                    streamClosed = true
                })
                done()
            })
        })

        after(function(done) {
            c2s.shutdown(done)
        })

        it('Should return error to client', function(done) {
            this.timeout(6000)
            /*jshint noempty: false */
            while (streamData !== true) {} 
            done()
        })

        it('Should close stream', function(done) {
            this.timeout(6000)
            /*jshint noempty: false */
            while (streamClosed !== true) {}
            done()
        })

        it('Should generate error event on server', function(done) {
            this.timeout(6000)
            /*jshint noempty: false */
            while (0 === streamError.message.match(/Empty domain/).length) {}
            done()
        })
    }

    describe('Handle stream with empty "to"', testEmptyTo.bind(undefined, 'to=""'))
    describe('Handle stream without "to"', testEmptyTo.bind(undefined, ''))
})

@@ -569,17 +569,16 @@
 ion() %7B%0A
-%0A
     func
@@ -1397,20 +1397,18 @@
 ct(%7B
+port:
  port
-: port 
 %7D, f
@@ -1984,109 +1984,42 @@
     
-this.timeout(6000)%0A            /*jshint noempty: false */%0A            while (streamData !== 
+streamData.should.be.exactly(
 true)
- %7B%7D 
 %0A   
@@ -2113,110 +2113,44 @@
     
-this.timeout(6000)%0A            /*jshint noempty: false */%0A            while (streamClosed !== 
+streamClosed.should.be.exactly(
 true)
- %7B%7D
 %0A   
@@ -2261,91 +2261,8 @@
     
- this.timeout(6000)%0A            /*jshint noempty: false */%0A            while (0 ===
  str
@@ -2270,23 +2270,22 @@
 amError.
-message
+should
 .match(/
@@ -2302,19 +2302,8 @@
 in/)
-.length) %7B%7D
 %0A   

Revert "Get tests passing in nodejs 0.8"