|
20 | 20 | import java.nio.charset.StandardCharsets; |
21 | 21 | import java.util.ArrayList; |
22 | 22 | import java.util.Arrays; |
| 23 | +import java.util.Collections; |
23 | 24 | import java.util.List; |
24 | 25 | import java.util.Map; |
25 | 26 | import java.util.Set; |
|
32 | 33 | import org.mockito.ArgumentMatcher; |
33 | 34 | import org.mockito.Mockito; |
34 | 35 | import org.mockito.exceptions.base.MockitoAssertionError; |
| 36 | +import org.mockito.stubbing.Answer; |
35 | 37 |
|
36 | 38 | public class BaseStripeTest { |
37 | 39 | private static final String MOCK_MINIMUM_VERSION = "0.109.0"; |
@@ -348,6 +350,35 @@ public static <T extends StripeObjectInterface> void stubRequest( |
348 | 350 | Mockito.<Type>any()); |
349 | 351 | } |
350 | 352 |
|
| 353 | + /** |
| 354 | + * Stubs an API request. This should rarely be necessary, but some endpoints are not supported by |
| 355 | + * stripe-mock yet. |
| 356 | + * |
| 357 | + * @param method HTTP method (GET, POST or DELETE) |
| 358 | + * @param path request path (e.g. "/v1/charges"). Can also be an abolute URL. |
| 359 | + * @param params map containing the parameters. If null, the parameters are not checked. |
| 360 | + * @param options request options. If null, the options are not checked. |
| 361 | + * @param typeToken Class of the API resource that will be returned for the stubbed request. |
| 362 | + * @param response JSON payload of the API resource that will be returned for the stubbed request. |
| 363 | + */ |
| 364 | + public static <T extends StripeObjectInterface> void stubRequestReturnError( |
| 365 | + BaseAddress baseAddress, |
| 366 | + ApiResource.RequestMethod method, |
| 367 | + String path, |
| 368 | + Map<String, Object> params, |
| 369 | + RequestOptions options, |
| 370 | + String response, |
| 371 | + Integer code) |
| 372 | + throws StripeException { |
| 373 | + |
| 374 | + Mockito.doAnswer( |
| 375 | + (Answer<StripeResponse>) |
| 376 | + invocation -> |
| 377 | + new StripeResponse(400, HttpHeaders.of(Collections.emptyMap()), response)) |
| 378 | + .when(httpClientSpy) |
| 379 | + .request(Mockito.any()); |
| 380 | + } |
| 381 | + |
351 | 382 | /** Stubs an OAuth API request. stripe-mock does not supported OAuth endpoints at this time. */ |
352 | 383 | public static <T extends StripeObjectInterface> void stubOAuthRequest( |
353 | 384 | Class<T> clazz, String response) throws StripeException { |
|
0 commit comments