File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -95,6 +95,10 @@ public function getContent(bool $throw = true): string
95
95
if ($ this ->event && $ this ->event ->isStarted ()) {
96
96
$ this ->event ->stop ();
97
97
}
98
+
99
+ if ($ throw ) {
100
+ $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
101
+ }
98
102
}
99
103
}
100
104
@@ -116,6 +120,10 @@ public function toArray(bool $throw = true): array
116
120
if ($ this ->event && $ this ->event ->isStarted ()) {
117
121
$ this ->event ->stop ();
118
122
}
123
+
124
+ if ($ throw ) {
125
+ $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
126
+ }
119
127
}
120
128
}
121
129
Original file line number Diff line number Diff line change 18
18
use Symfony \Component \HttpClient \Response \MockResponse ;
19
19
use Symfony \Component \HttpClient \TraceableHttpClient ;
20
20
use Symfony \Component \Stopwatch \Stopwatch ;
21
+ use Symfony \Contracts \HttpClient \Exception \ClientExceptionInterface ;
21
22
use Symfony \Contracts \HttpClient \HttpClientInterface ;
22
23
use Symfony \Contracts \HttpClient \Test \TestHttpServer ;
23
24
@@ -118,6 +119,18 @@ public function testStream()
118
119
$ this ->assertSame ('Symfony is awesome! ' , implode ('' , $ chunks ));
119
120
}
120
121
122
+ public function testToArrayChecksStatusCodeBeforeDecoding ()
123
+ {
124
+ $ this ->expectException (ClientExceptionInterface::class);
125
+
126
+ $ sut = new TraceableHttpClient (new MockHttpClient ($ responseFactory = function (): MockResponse {
127
+ return new MockResponse ('Errored. ' , ['http_code ' => 400 ]);
128
+ }));
129
+
130
+ $ response = $ sut ->request ('GET ' , 'https://example.com/foo/bar ' );
131
+ $ response ->toArray ();
132
+ }
133
+
121
134
public function testStopwatch ()
122
135
{
123
136
$ sw = new Stopwatch (true );
You can’t perform that action at this time.
0 commit comments