Skip to content

[HttpClient][WDT] Add missing response content #35317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ private function collectOnClient(TraceableHttpClient $client): array
}

$debugInfo = array_diff_key($info, $baseInfo);
$info = array_diff_key($info, $debugInfo) + ['debug_info' => $debugInfo];
unset($traces[$i]['info']); // break PHP reference used by TraceableHttpClient
$responseContent = isset($trace['response_content']) ? $trace['response_content'] : null;
$info = array_diff_key($info, $debugInfo) + ['response_content' => $responseContent, 'debug_info' => $debugInfo];
unset($traces[$i]['info'], $traces[$i]['response_content']); // break PHP reference used by TraceableHttpClient
$traces[$i]['info'] = $this->cloneVar($info);
$traces[$i]['options'] = $this->cloneVar($trace['options']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testItTracesRequest()
return true;
})
)
->willReturn(MockResponse::fromRequest('GET', '/foo/bar', ['options1' => 'foo'], new MockResponse()))
->willReturn(MockResponse::fromRequest('GET', '/foo/bar', ['options1' => 'foo'], new MockResponse('{"foo": "bar"}')))
;
$sut = new TraceableHttpClient($httpClient);
$sut->request('GET', '/foo/bar', ['options1' => 'foo']);
Expand All @@ -47,6 +47,7 @@ public function testItTracesRequest()
'url' => '/foo/bar',
'options' => ['options1' => 'foo'],
'info' => [],
'response_content' => ['foo' => 'bar'],
], $actualTracedRequest);
}

Expand All @@ -58,6 +59,7 @@ public function testItCollectsInfoOnRealRequest()
$actualTracedRequest = $tracedRequests[0];
$this->assertSame('GET', $actualTracedRequest['info']['http_method']);
$this->assertSame('http://localhost:8057/', $actualTracedRequest['info']['url']);
$this->assertNull($actualTracedRequest['response_content']);
}

public function testItExecutesOnProgressOption()
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/HttpClient/TraceableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public function __construct(HttpClientInterface $client)
public function request(string $method, string $url, array $options = []): ResponseInterface
{
$traceInfo = [];
$responseContent = null;
$this->tracedRequests[] = [
'method' => $method,
'url' => $url,
'options' => $options,
'info' => &$traceInfo,
'response_content' => &$responseContent,
];
$onProgress = $options['on_progress'] ?? null;

Expand All @@ -51,7 +53,15 @@ public function request(string $method, string $url, array $options = []): Respo
}
};

return $this->client->request($method, $url, $options);
$response = $this->client->request($method, $url, $options);

// Try to convert response to array if possible
try {
$responseContent = $response->toArray(false);
} catch (\Throwable $e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never ever do this: catch throwable and silence any errors
check https://twitter.com/nikita_ppv/status/1042676176836341760 also

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explain with catching.

}

return $response;
}

/**
Expand Down