1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http; |
4
|
|
|
|
5
|
|
|
use Koded\Http\Uri; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class UriSerializationTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
public function test_json_serialization_with_all_the_things() |
11
|
|
|
{ |
12
|
|
|
$uri = new Uri('https://username:[email protected]:8080/foo/bar?a=1234&b=5678#baz'); |
13
|
|
|
; |
14
|
|
|
$this->assertSame([ |
15
|
|
|
'scheme' => 'https', |
16
|
|
|
'host' => 'example.com', |
17
|
|
|
'port' => 8080, |
18
|
|
|
'path' => '/foo/bar', |
19
|
|
|
'user' => 'username', |
20
|
|
|
'pass' => 'password', |
21
|
|
|
'fragment' => 'baz', |
22
|
|
|
'query' => 'a=1234&b=5678' |
23
|
|
|
], $uri->jsonSerialize()); |
24
|
|
|
|
25
|
|
|
$this->assertJsonStringEqualsJsonString( |
26
|
|
|
'{"scheme":"https","host":"example.com","port":8080,"path":"/foo/bar","user":"username","pass":"password","fragment":"baz","query":"a=1234&b=5678"}', |
27
|
|
|
json_encode($uri, JSON_UNESCAPED_SLASHES) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function test_json_serialization_with_username_and_host() |
32
|
|
|
{ |
33
|
|
|
$uri = new Uri('http://[email protected]'); |
34
|
|
|
|
35
|
|
|
$this->assertSame([ |
36
|
|
|
'scheme' => 'http', |
37
|
|
|
'host' => 'example.com', |
38
|
|
|
'path' => '/', |
39
|
|
|
'user' => 'username', |
40
|
|
|
], $uri->jsonSerialize()); |
41
|
|
|
|
42
|
|
|
$this->assertJsonStringEqualsJsonString( |
43
|
|
|
'{"scheme":"http","host":"example.com","path":"/","user":"username"}', |
44
|
|
|
json_encode($uri, JSON_UNESCAPED_SLASHES) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function test_json_serialization_with_no_path() |
49
|
|
|
{ |
50
|
|
|
$uri = new Uri('https://example.com'); |
51
|
|
|
|
52
|
|
|
$this->assertSame([ |
53
|
|
|
'scheme' => 'https', |
54
|
|
|
'host' => 'example.com', |
55
|
|
|
], $uri->jsonSerialize()); |
56
|
|
|
|
57
|
|
|
$this->assertJsonStringEqualsJsonString( |
58
|
|
|
'{"scheme":"https","host":"example.com"}', |
59
|
|
|
json_encode($uri, JSON_UNESCAPED_SLASHES) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function test_json_serialization_with_slash_path() |
64
|
|
|
{ |
65
|
|
|
$uri = new Uri('https://example.com/'); |
66
|
|
|
|
67
|
|
|
$this->assertSame([ |
68
|
|
|
'scheme' => 'https', |
69
|
|
|
'host' => 'example.com', |
70
|
|
|
'path' => '/' |
71
|
|
|
], $uri->jsonSerialize()); |
72
|
|
|
|
73
|
|
|
$this->assertJsonStringEqualsJsonString( |
74
|
|
|
'{"scheme":"https","host":"example.com","path":"/"}', |
75
|
|
|
json_encode($uri, JSON_UNESCAPED_SLASHES) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function test_json_serialization_with_standard_port() |
80
|
|
|
{ |
81
|
|
|
$uri = new Uri('https://example.com:21'); |
82
|
|
|
|
83
|
|
|
$this->assertSame([ |
84
|
|
|
'scheme' => 'https', |
85
|
|
|
'host' => 'example.com', |
86
|
|
|
], $uri->jsonSerialize(), |
87
|
|
|
'The standard port is omitted' |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->assertJsonStringEqualsJsonString( |
91
|
|
|
'{"scheme":"https","host":"example.com"}', |
92
|
|
|
json_encode($uri, JSON_UNESCAPED_SLASHES) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|