File tree 4 files changed +32
-8
lines changed
4 files changed +32
-8
lines changed Original file line number Diff line number Diff line change @@ -246,20 +246,19 @@ public function setRequestTarget(string $requestTarget): static
246
246
public function toString (bool $ withoutBody = false ): string
247
247
{
248
248
$ headerString = '' ;
249
- if (! $ withoutBody ) {
250
- foreach ($ this ->getStandardHeaders () as $ key => $ values ) {
251
- foreach ($ values as $ value ) {
252
- $ headerString .= sprintf ("%s: %s \r\n" , $ key , $ value );
253
- }
249
+ foreach ($ this ->getStandardHeaders () as $ key => $ values ) {
250
+ foreach ($ values as $ value ) {
251
+ $ headerString .= sprintf ("%s: %s \r\n" , $ key , $ value );
254
252
}
255
253
}
254
+
256
255
return sprintf (
257
256
"%s %s HTTP/%s \r\n%s \r\n%s " ,
258
257
$ this ->getMethod (),
259
258
$ this ->getUri ()->getPath (),
260
259
$ this ->getProtocolVersion (),
261
260
$ headerString ,
262
- $ this ->getBody ()
261
+ $ withoutBody ? '' : $ this ->getBody ()
263
262
);
264
263
}
265
264
Original file line number Diff line number Diff line change @@ -354,7 +354,7 @@ public function toString(bool $withoutBody = false): string
354
354
$ this ->getStatusCode (),
355
355
$ this ->getReasonPhrase (),
356
356
$ headerString ,
357
- $ this ->getBody ()
357
+ $ withoutBody ? '' : $ this ->getBody ()
358
358
);
359
359
}
360
360
Original file line number Diff line number Diff line change @@ -181,7 +181,7 @@ public function toString(bool $withoutBody = false): string
181
181
$ this ->getStatusCode (),
182
182
$ this ->getReasonPhrase (),
183
183
$ headerString ,
184
- $ this ->getBody ()
184
+ $ withoutBody ? '' : $ this ->getBody ()
185
185
);
186
186
}
187
187
Original file line number Diff line number Diff line change 12
12
13
13
namespace HyperfTest \HttpMessage ;
14
14
15
+ use Hyperf \Codec \Json ;
15
16
use Hyperf \Engine \Http \WritableConnection ;
16
17
use Hyperf \HttpMessage \Cookie \Cookie ;
17
18
use Hyperf \HttpMessage \Server \Response ;
19
+ use Hyperf \HttpMessage \Stream \SwooleStream ;
18
20
use Mockery ;
19
21
use PHPUnit \Framework \Attributes \CoversNothing ;
20
22
use PHPUnit \Framework \TestCase ;
21
23
use Swoole \Http \Response as SwooleResponse ;
24
+ use Swow \Psr7 \Message \ResponsePlusInterface ;
22
25
23
26
/**
24
27
* @internal
@@ -70,6 +73,28 @@ public function testWrite()
70
73
$ this ->assertTrue ($ status );
71
74
}
72
75
76
+ public function testToString ()
77
+ {
78
+ $ response = $ this ->newResponse ();
79
+ if (! $ response instanceof ResponsePlusInterface) {
80
+ $ this ->markTestSkipped ('Don \'t assert response which not instanceof ResponsePlusInterface ' );
81
+ }
82
+
83
+ $ response ->setStatus (200 )->setHeaders (['Content-Type ' => 'application/json ' ])->setBody (new SwooleStream (Json::encode (['id ' => $ id = uniqid ()])));
84
+ $ this ->assertEquals ("HTTP/1.1 200 OK \r
85
+ Content-Type: application/json \r
86
+ Connection: close \r
87
+ Content-Length: 22 \r
88
+ \r
89
+ { \"id \": \"" . $ id . '"} ' , $ response ->toString ());
90
+ $ this ->assertSame ("HTTP/1.1 200 OK \r
91
+ Content-Type: application/json \r
92
+ Connection: close \r
93
+ Content-Length: 22 \r
94
+ \r
95
+ " , $ response ->toString (true ));
96
+ }
97
+
73
98
protected function newResponse ()
74
99
{
75
100
return new Response ();
You can’t perform that action at this time.
0 commit comments