2
2
3
3
namespace PhpDevCommunity \tests ;
4
4
5
+ use Exception ;
6
+ use LogicException ;
5
7
use PhpDevCommunity \HttpClient \HttpClient ;
6
8
use PHPUnit \Framework \TestCase ;
7
9
@@ -10,38 +12,46 @@ class HttpClientTest extends TestCase
10
12
const URL = 'http://localhost:4245 ' ;
11
13
12
14
protected static ?string $ serverProcess = null ;
15
+
13
16
public static function setUpBeforeClass (): void
14
17
{
15
- $ fileToRun = __DIR__ . DIRECTORY_SEPARATOR . 'test_server.php ' ;
16
- $ command = sprintf ('php -S %s %s > /dev/null 2>&1 & echo $!; ' ,str_replace ('http:// ' , '' , self ::URL ), $ fileToRun );
18
+ $ fileToRun = __DIR__ . DIRECTORY_SEPARATOR . 'test_server.php ' ;
19
+ $ command = sprintf ('php -S %s %s > /dev/null 2>&1 & echo $!; ' , str_replace ('http:// ' , '' , self ::URL ), $ fileToRun );
17
20
self ::$ serverProcess = exec ($ command );
18
21
if (empty (self ::$ serverProcess ) || !is_numeric (self ::$ serverProcess )) {
19
- throw new \ Exception ('Could not start test server ' );
22
+ throw new Exception ('Could not start test server ' );
20
23
}
21
24
sleep (1 );
22
25
}
23
26
24
27
public function testGetRequest ()
25
28
{
26
- $ response = http_client (['base_url ' => self ::URL , 'headers ' => ['Authorization ' => 'Bearer secret_token ' ]])->get ('/api/data ' );
27
- $ this ->assertEquals ( 200 , $ response ->getStatusCode () );
29
+ $ response = http_client (
30
+ ['base_url ' => self ::URL , 'headers ' => ['Authorization ' => 'Bearer secret_token ' ]],
31
+ function ($ info ) {
32
+ $ this ->assertEquals ( 'GET ' , $ info ['request ' ]['method ' ]);
33
+ $ this ->assertEquals ( 'Bearer secret_token ' , $ info ['request ' ]['headers ' ]['Authorization ' ]);
34
+ $ this ->assertEquals ( '{"message":"GET request received"} ' , $ info ['response ' ]['body ' ]);
35
+ }
36
+ )->get ('/api/data ' );
37
+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
28
38
$ this ->assertNotEmpty ($ response ->getBody ());
29
39
}
30
40
31
41
public function testGetWithQueryRequest ()
32
42
{
33
- $ client = new HttpClient (['base_url ' => self ::URL ,'headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
43
+ $ client = new HttpClient (['base_url ' => self ::URL , 'headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
34
44
$ response = $ client ->get ('/api/search ' , [
35
45
'name ' => 'foo ' ,
36
46
]);
37
47
38
- $ this ->assertEquals ( 200 , $ response ->getStatusCode () );
48
+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
39
49
$ this ->assertNotEmpty ($ response ->getBody ());
40
50
41
51
$ data = $ response ->bodyToArray ();
42
- $ this ->assertEquals ( 'foo ' , $ data ['name ' ] );
43
- $ this ->assertEquals ( 1 , $ data ['page ' ] );
44
- $ this ->assertEquals ( 10 , $ data ['limit ' ] );
52
+ $ this ->assertEquals ('foo ' , $ data ['name ' ]);
53
+ $ this ->assertEquals (1 , $ data ['page ' ]);
54
+ $ this ->assertEquals (10 , $ data ['limit ' ]);
45
55
46
56
47
57
$ response = $ client ->get ('/api/search ' , [
@@ -50,13 +60,13 @@ public function testGetWithQueryRequest()
50
60
'limit ' => 100
51
61
]);
52
62
53
- $ this ->assertEquals ( 200 , $ response ->getStatusCode () );
63
+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
54
64
$ this ->assertNotEmpty ($ response ->getBody ());
55
65
56
66
$ data = $ response ->bodyToArray ();
57
- $ this ->assertEquals ( 'foo ' , $ data ['name ' ] );
58
- $ this ->assertEquals ( 10 , $ data ['page ' ] );
59
- $ this ->assertEquals ( 100 , $ data ['limit ' ] );
67
+ $ this ->assertEquals ('foo ' , $ data ['name ' ]);
68
+ $ this ->assertEquals (10 , $ data ['page ' ]);
69
+ $ this ->assertEquals (100 , $ data ['limit ' ]);
60
70
}
61
71
62
72
public function testPostJsonRequest ()
@@ -67,14 +77,14 @@ public function testPostJsonRequest()
67
77
'userId ' => 1
68
78
];
69
79
$ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
70
- $ response = $ client ->post (self ::URL . '/api/post/data ' , [
80
+ $ response = $ client ->post (self ::URL . '/api/post/data ' , [
71
81
'title ' => 'foo ' ,
72
82
'body ' => 'bar ' ,
73
83
'userId ' => 1
74
84
], true );
75
85
76
- $ this ->assertEquals ( 200 , $ response ->getStatusCode ());
77
- $ this ->assertEquals ( $ dataToPost , $ response ->bodyToArray ());
86
+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
87
+ $ this ->assertEquals ($ dataToPost , $ response ->bodyToArray ());
78
88
}
79
89
80
90
public function testPostFormRequest ()
@@ -85,18 +95,50 @@ public function testPostFormRequest()
85
95
'userId ' => 1
86
96
];
87
97
$ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
88
- $ response = $ client ->post (self ::URL . '/api/post/data/form ' , $ dataToPost );
98
+ $ response = $ client ->post (self ::URL . '/api/post/data/form ' , $ dataToPost );
89
99
90
- $ this ->assertEquals ( 200 , $ response ->getStatusCode ());
91
- $ this ->assertEquals ( $ dataToPost , $ response ->bodyToArray ());
100
+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
101
+ $ this ->assertEquals ($ dataToPost , $ response ->bodyToArray ());
92
102
}
93
103
94
104
public function testPostEmptyFormRequest ()
95
105
{
96
106
$ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
97
- $ response = $ client ->post (self ::URL .'/api/post/data/form ' , []);
107
+ $ response = $ client ->post (self ::URL . '/api/post/data/form ' , []);
108
+
109
+ $ this ->assertEquals (400 , $ response ->getStatusCode ());
110
+ }
111
+
112
+ public function testWrongOptions ()
113
+ {
114
+ $ this ->expectException (LogicException::class);
115
+ new HttpClient (['headers ' => 'string ' ]);
116
+ }
117
+
118
+ public function testWrongOptions2 ()
119
+ {
120
+ $ this ->expectException (LogicException::class);
121
+ new HttpClient (['options_not_supported ' => 'value ' ]);
122
+ }
123
+
124
+ public function testWrongOptions3 ()
125
+ {
126
+ $ this ->expectException (LogicException::class);
127
+ new HttpClient (['timeout ' => 'string ' ]);
128
+ }
129
+
130
+ public function testWrongMethod ()
131
+ {
132
+ $ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
133
+ $ this ->expectException (LogicException::class);
134
+ $ client ->fetch (self ::URL . '/api/data ' , ['method ' => 'WRONG ' ]);
135
+ }
98
136
99
- $ this ->assertEquals ( 400 , $ response ->getStatusCode () );
137
+ public function testWrongUrl ()
138
+ {
139
+ $ client = new HttpClient (['headers ' => ['Authorization ' => 'Bearer secret_token ' ]]);
140
+ $ this ->expectException (LogicException::class);
141
+ $ client ->fetch ('WRONG_URL ' , ['method ' => 'GET ' ]);
100
142
}
101
143
102
144
0 commit comments