Skip to content

Commit 4e3c82f

Browse files
authored
Merge pull request #98 from clue-labs/options-asterisk
Support OPTIONS method with asterisk-form (OPTIONS * HTTP/1.1)
2 parents 42e6a0f + 7c2a74c commit 4e3c82f

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/RequestData.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,18 @@ public function getDefaultPort()
5656

5757
public function getPath()
5858
{
59-
$path = parse_url($this->url, PHP_URL_PATH) ?: '/';
59+
$path = parse_url($this->url, PHP_URL_PATH);
6060
$queryString = parse_url($this->url, PHP_URL_QUERY);
6161

62-
return $path.($queryString ? "?$queryString" : '');
62+
// assume "/" path by default, but allow "OPTIONS *"
63+
if ($path === null) {
64+
$path = ($this->method === 'OPTIONS' && $queryString === null) ? '*': '/';
65+
}
66+
if ($queryString !== null) {
67+
$path .= '?' . $queryString;
68+
}
69+
70+
return $path;
6371
}
6472

6573
public function setProtocolVersion($version)

tests/RequestDataTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,58 @@ public function toStringReturnsHTTPRequestMessage()
1919
$this->assertSame($expected, $requestData->__toString());
2020
}
2121

22+
/** @test */
23+
public function toStringReturnsHTTPRequestMessageWithEmptyQueryString()
24+
{
25+
$requestData = new RequestData('GET', 'http://www.example.com/path?hello=world');
26+
27+
$expected = "GET /path?hello=world HTTP/1.0\r\n" .
28+
"Host: www.example.com\r\n" .
29+
"User-Agent: React/alpha\r\n" .
30+
"\r\n";
31+
32+
$this->assertSame($expected, $requestData->__toString());
33+
}
34+
35+
/** @test */
36+
public function toStringReturnsHTTPRequestMessageWithZeroQueryStringAndRootPath()
37+
{
38+
$requestData = new RequestData('GET', 'http://www.example.com?0');
39+
40+
$expected = "GET /?0 HTTP/1.0\r\n" .
41+
"Host: www.example.com\r\n" .
42+
"User-Agent: React/alpha\r\n" .
43+
"\r\n";
44+
45+
$this->assertSame($expected, $requestData->__toString());
46+
}
47+
48+
/** @test */
49+
public function toStringReturnsHTTPRequestMessageWithOptionsAbsoluteRequestForm()
50+
{
51+
$requestData = new RequestData('OPTIONS', 'http://www.example.com/');
52+
53+
$expected = "OPTIONS / HTTP/1.0\r\n" .
54+
"Host: www.example.com\r\n" .
55+
"User-Agent: React/alpha\r\n" .
56+
"\r\n";
57+
58+
$this->assertSame($expected, $requestData->__toString());
59+
}
60+
61+
/** @test */
62+
public function toStringReturnsHTTPRequestMessageWithOptionsAsteriskRequestForm()
63+
{
64+
$requestData = new RequestData('OPTIONS', 'http://www.example.com');
65+
66+
$expected = "OPTIONS * HTTP/1.0\r\n" .
67+
"Host: www.example.com\r\n" .
68+
"User-Agent: React/alpha\r\n" .
69+
"\r\n";
70+
71+
$this->assertSame($expected, $requestData->__toString());
72+
}
73+
2274
/** @test */
2375
public function toStringReturnsHTTPRequestMessageWithProtocolVersion()
2476
{

0 commit comments

Comments
 (0)