File tree 2 files changed +27
-12
lines changed
src/Symfony/Component/HttpClient
2 files changed +27
-12
lines changed Original file line number Diff line number Diff line change 16
16
use Symfony \Component \HttpClient \Exception \ServerException ;
17
17
use Symfony \Component \HttpClient \TraceableHttpClient ;
18
18
use Symfony \Contracts \HttpClient \Exception \ClientExceptionInterface ;
19
+ use Symfony \Contracts \HttpClient \Exception \DecodingExceptionInterface ;
20
+ use Symfony \Contracts \HttpClient \Exception \HttpExceptionInterface ;
19
21
use Symfony \Contracts \HttpClient \Exception \RedirectionExceptionInterface ;
20
22
use Symfony \Contracts \HttpClient \Exception \ServerExceptionInterface ;
21
23
use Symfony \Contracts \HttpClient \Exception \TransportExceptionInterface ;
@@ -52,24 +54,24 @@ public function getHeaders(bool $throw = true): array
52
54
53
55
public function getContent (bool $ throw = true ): string
54
56
{
55
- $ this ->content = $ this ->response ->getContent (false );
56
-
57
- if ($ throw ) {
58
- $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
57
+ try {
58
+ return $ this ->content = $ this ->response ->getContent (false );
59
+ } finally {
60
+ if ($ throw ) {
61
+ $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
62
+ }
59
63
}
60
-
61
- return $ this ->content ;
62
64
}
63
65
64
66
public function toArray (bool $ throw = true ): array
65
67
{
66
- $ this ->content = $ this ->response ->toArray (false );
67
-
68
- if ($ throw ) {
69
- $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
68
+ try {
69
+ return $ this ->content = $ this ->response ->toArray (false );
70
+ } finally {
71
+ if ($ throw ) {
72
+ $ this ->checkStatusCode ($ this ->response ->getStatusCode ());
73
+ }
70
74
}
71
-
72
- return $ this ->content ;
73
75
}
74
76
75
77
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