Skip to content

Commit 842637b

Browse files
committed
Merge pull request #8 from clue/colonspace
Require a single space after colon and preserve whitespace otherwise
2 parents 083a962 + 93dca99 commit 842637b

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/Protocol/Parser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ private function parseMessage($message)
4646
$key = '_';
4747
$value = $line;
4848
} else {
49-
$pos = strpos($line, ':');
49+
$pos = strpos($line, ': ');
5050
if ($pos === false) {
5151
throw new \UnexpectedValueException('Parse error, no colon in line "' . $line . '" found');
5252
}
53-
$value = ltrim(substr($line, $pos + 1));
53+
$value = substr($line, $pos + 2);
5454
$key = substr($line, 0, $pos);
5555
}
5656

tests/Protocol/ParserTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ public function testParseResponse()
1919
$this->assertEquals('Success', $first->getPart('Response'));
2020
}
2121

22+
public function testParseResponseSpace()
23+
{
24+
$parser = new Parser();
25+
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));
26+
27+
$ret = $parser->push("Response: Success\r\nMessage: spaces \r\n\r\n");
28+
$this->assertCount(1, $ret);
29+
30+
$first = reset($ret);
31+
/* @var $first Clue\React\Ami\Protocol\Response */
32+
33+
$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $first);
34+
$this->assertEquals(' spaces ', $first->getPart('Message'));
35+
}
36+
2237
public function testParsingMultipleEvents()
2338
{
2439
$parser = new Parser();
@@ -60,4 +75,15 @@ public function testParsingInvalidResponseFails()
6075

6176
$parser->push("invalid response\r\n\r\n");
6277
}
78+
79+
/**
80+
* @expectedException UnexpectedValueException
81+
*/
82+
public function testParsingInvalidResponseNoSpaceAfterColonFails()
83+
{
84+
$parser = new Parser();
85+
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));
86+
87+
$parser->push("Response:NoSpace\r\n\r\n");
88+
}
6389
}

0 commit comments

Comments
 (0)