File tree 2 files changed +25
-12
lines changed
src/Symfony/Component/HttpClient
2 files changed +25
-12
lines changed Original file line number Diff line number Diff line change @@ -52,24 +52,24 @@ public function getHeaders(bool $throw = true): array
52
52
53
53
public function getContent (bool $ throw = true ): string
54
54
{
55
- $ this ->content = $ this ->response ->getContent (false );
56
-
57
- if ($ throw ) {
58
- $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
55
+ try {
56
+ return $ this ->content = $ this ->response ->getContent (false );
57
+ } finally {
58
+ if ($ throw ) {
59
+ $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
60
+ }
59
61
}
60
-
61
- return $ this ->content ;
62
62
}
63
63
64
64
public function toArray (bool $ throw = true ): array
65
65
{
66
- $ this ->content = $ this ->response ->toArray (false );
67
-
68
- if ($ throw ) {
69
- $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
66
+ try {
67
+ return $ this ->content = $ this ->response ->toArray (false );
68
+ } finally {
69
+ if ($ throw ) {
70
+ $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
71
+ }
70
72
}
71
-
72
- return $ this ->content ;
73
73
}
74
74
75
75
public function cancel (): void
Original file line number Diff line number Diff line change 16
16
use Symfony \Component \HttpClient \NativeHttpClient ;
17
17
use Symfony \Component \HttpClient \Response \MockResponse ;
18
18
use Symfony \Component \HttpClient \TraceableHttpClient ;
19
+ use Symfony \Contracts \HttpClient \Exception \ClientExceptionInterface ;
19
20
use Symfony \Contracts \HttpClient \HttpClientInterface ;
20
21
use Symfony \Contracts \HttpClient \Test \TestHttpServer ;
21
22
@@ -100,4 +101,16 @@ public function testStream()
100
101
$ this ->assertGreaterThan (1 , \count ($ chunks ));
101
102
$ this ->assertSame ('Symfony is awesome! ' , implode ('' , $ chunks ));
102
103
}
104
+
105
+ public function testToArrayChecksStatusCodeBeforeDecoding ()
106
+ {
107
+ $ this ->expectException (ClientExceptionInterface::class);
108
+
109
+ $ sut = new TraceableHttpClient (new MockHttpClient ($ responseFactory = function (): MockResponse {
110
+ return new MockResponse ('Errored. ' , ['http_code ' => 400 ]);
111
+ }));
112
+
113
+ $ response = $ sut ->request ('GET ' , 'https://example.com/foo/bar ' );
114
+ $ response ->toArray ();
115
+ }
103
116
}
You can’t perform that action at this time.
0 commit comments