You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I guess a pretty common use case for this project is to create a proxy/adapter for some HTTP based API. This would for example look like this:
class OpenWeatherMapProxy {
privatereadonlyHttpClient$client;
publicfunction__construct(#[\SensitiveParameter] string$apiKey, string$lang = 'en') {
$this->client = (newHttpClientBuilder())
->retry(3)
->intercept(newAddRequestHeader('Accept', 'application/json'))
->intercept(newModifyRequest(function(Request$request) use($apiKey, $lang): Request {
$request->setQueryParameter('appid', $apiKey); // add API key to every request$request->setQueryParameter('units', 'metric'); // add preferred unit system to every request$request->setQueryParameter('lang', $lang); // add preferred language to every requestreturn$request;
}))
->build();
}
// [...]
}
Now, how to unit or integration test this?
Note: we do not want to actually send requests, since the API might have a strict rate limit or is priced per API call. In addition, that would miss the point of unit and integration tests.
Approaches:
Refactor the class to utilize Dependency Injection pattern: This would move the whole HttpClientBuilder part to the caller, which is weird, since we do some API specific things there. Or is there a way to extend a HttpClient with interceptors and stuff after its initial build? Maybe using new InterceptedHttpClient($paramClient, $myInterceptor1)?
Insert a mocked HttpClient into the proxy and replace the $client using Reflection: This would make it impossible to test the HttpClient settings from the constructor and it is impossible due to readonly, which - apart from that - makes absolutely sense here.
Either way, in my opinion there should be a way to test stuff like this and it should be documented how to do it properly in the README.md. Thanks for helping me out here!
The text was updated successfully, but these errors were encountered:
I guess a pretty common use case for this project is to create a proxy/adapter for some HTTP based API. This would for example look like this:
Now, how to unit or integration test this?
Note: we do not want to actually send requests, since the API might have a strict rate limit or is priced per API call. In addition, that would miss the point of unit and integration tests.
Approaches:
HttpClientBuilder
part to the caller, which is weird, since we do some API specific things there. Or is there a way to extend aHttpClient
with interceptors and stuff after its initial build? Maybe usingnew InterceptedHttpClient($paramClient, $myInterceptor1)
?HttpClient
into the proxy and replace the$client
using Reflection: This would make it impossible to test theHttpClient
settings from the constructor and it is impossible due toreadonly
, which - apart from that - makes absolutely sense here.Either way, in my opinion there should be a way to test stuff like this and it should be documented how to do it properly in the README.md. Thanks for helping me out here!
The text was updated successfully, but these errors were encountered: