Skip to content
This repository has been archived by the owner on Jan 10, 2022. It is now read-only.

Commit

Permalink
Added disconnect response when server is closing connection
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiang committed Jan 23, 2014
1 parent 4b1a9c5 commit 279102c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/Fabiang/Xmpp/EventListener/Stream/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public function streamServer(XMLEvent $event)
{
if (false === $event->isStartTag()) {
$this->blocking = false;

if ($this->getConnection()->isConnected()) {
$this->getConnection()->disconnect();
}
}
}

Expand Down
30 changes: 25 additions & 5 deletions tests/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ class FeatureContext extends BehatContext
* @var Test
*/
protected $connection;

/**
* Catch connection exceptions.
*
*
* @var boolean
*/
protected $catch = false;

/**
* Catched exception.
*
*
* @var \Exception
*/
public $exception;
Expand Down Expand Up @@ -140,7 +140,19 @@ public function testResponseDataForTls()
. "id='1234567890' from='localhost' version='1.0' xml:lang='en'><stream:features></stream:features>"
));
}


/**
* @Given /^Test response data for disconnect$/
*/
public function testResponseDataForDisconnect()
{
$this->connection->setData(array(
"<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
. "id='1234567890' from='localhost' version='1.0' xml:lang='en'>",
'</stream:stream>'
));
}

/**
* @Given /^exceptions are catched when connecting$/
*/
Expand Down Expand Up @@ -216,6 +228,14 @@ public function starttlsShouldBeSend()
assertContains('<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>', $this->connection->getBuffer());
}

/**
* @Then /^Stream end received$/
*/
public function streamEndReceived()
{
assertContains('</stream:stream>', $this->connection->getData());
}

/**
*
* @return Client
Expand Down
7 changes: 7 additions & 0 deletions tests/features/connection.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ Feature: Connection
When connecting
Then Starttls should be send
And Stream start should be send 2 times

Scenario: server closes connection
Given Test connection adapter
And Test response data for disconnect
When connecting
Then Stream end should be send
And should be disconnected
1 change: 1 addition & 0 deletions tests/src/Fabiang/Xmpp/EventListener/Stream/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function testEvents()
$event->setStartTag(false);
$this->object->streamServer($event);
$this->assertFalse($this->object->isBlocking());
$this->assertFalse($this->connection->isConnected());

$event->setStartTag(true);
$this->object->stream($event);
Expand Down
42 changes: 37 additions & 5 deletions tests/src/Fabiang/Xmpp/Stream/XMLStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,38 @@ function () use (&$triggered) {
$this->assertSame(10, $triggered, 'Event where not triggered five times');
}

/**
* Test parsing xml if xml stream is finished.
*
* @covers Fabiang\Xmpp\Stream\XmlStream::parse
* @return void
*/
public function testParseFinalEndTag()
{
$triggered = 0;

$this->object->getEventManager()->attach(
'{http://etherx.jabber.org/streams}stream',
function ($e) use (&$triggered) {
if ($e->isEndTag()) {
$triggered++;
}
}
);

$start = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
. '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" '
. 'xmlns="jabber:client" from="gamebox" id="b9a85bbd" xml:lang="en" version="1.0">';

$middle = '<stream:features>1234</stream:features>';

$end = '</stream:stream>';

$this->object->parse($start);
$this->object->parse($end);
$this->assertSame(1, $triggered);
}

/**
* Test parsing xml.
*
Expand All @@ -125,22 +157,22 @@ public function testParseChallenge()
. 'xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="gamebox" id="7f3ceab2" '
. 'xml:lang="en" version="1.0">';
$this->assertInstanceOf('\DOMDocument', $this->object->parse($xml));

$xml = '<stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls>'
. '<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>DIGEST-MD5</mechanism>'
. '<mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism>'
. '</mechanisms><compression xmlns="http://jabber.org/features/compress"><method>zlib</method>'
. '</compression><auth xmlns="http://jabber.org/features/iq-auth"/>'
. '<register xmlns="http://jabber.org/features/iq-register"/></stream:features>';
$this->assertInstanceOf('\DOMDocument', $this->object->parse($xml));

$xml = '<proceed ';
$this->assertInstanceOf('\DOMDocument', $this->object->parse($xml));

$xml = 'xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>';
$this->assertInstanceOf('\DOMDocument', $this->object->parse($xml));
$this->assertInstanceOf('\DOMDocument', $this->object->parse($xml));
}

/**
* Test parsing invalid XML.
*
Expand Down

0 comments on commit 279102c

Please sign in to comment.