|
11 | 11 | import com.azure.core.http.MockHttpResponse;
|
12 | 12 | import com.azure.core.http.rest.Response;
|
13 | 13 | import com.azure.core.http.rest.SimpleResponse;
|
| 14 | +import com.azure.core.util.Context; |
14 | 15 | import com.azure.core.util.serializer.TypeReference;
|
15 | 16 | import org.junit.jupiter.api.AfterEach;
|
| 17 | +import org.junit.jupiter.api.Assertions; |
16 | 18 | import org.junit.jupiter.api.BeforeEach;
|
17 | 19 | import org.junit.jupiter.api.Test;
|
| 20 | +import org.mockito.ArgumentCaptor; |
18 | 21 | import org.mockito.Mock;
|
19 | 22 | import org.mockito.Mockito;
|
20 | 23 | import org.mockito.MockitoAnnotations;
|
@@ -302,6 +305,68 @@ public void locationPollingStrategySucceedsOnPollWithPostLocationHeader() {
|
302 | 305 | assertEquals(1, activationCallCount[0]);
|
303 | 306 | }
|
304 | 307 |
|
| 308 | + @Test |
| 309 | + public void pollingStrategyPassContextToHttpClient() { |
| 310 | + int[] activationCallCount = new int[1]; |
| 311 | + activationCallCount[0] = 0; |
| 312 | + String mockPollUrl = "http://localhost/poll"; |
| 313 | + String finalResultUrl = "http://localhost/final"; |
| 314 | + when(activationOperation.get()).thenReturn(Mono.defer(() -> { |
| 315 | + activationCallCount[0]++; |
| 316 | + SimpleResponse<PollResult> response = new SimpleResponse<>( |
| 317 | + new HttpRequest(HttpMethod.POST, "http://localhost"), |
| 318 | + 200, |
| 319 | + new HttpHeaders().set("Location", mockPollUrl), |
| 320 | + new PollResult("InProgress")); |
| 321 | + return Mono.just(response); |
| 322 | + })); |
| 323 | + HttpRequest pollRequest = new HttpRequest(HttpMethod.GET, mockPollUrl); |
| 324 | + ArgumentCaptor<Context> contextArgument = ArgumentCaptor.forClass(Context.class); |
| 325 | + when(httpClient.send(any(), contextArgument.capture())) |
| 326 | + .thenAnswer(iom -> { |
| 327 | + HttpRequest req = iom.getArgument(0); |
| 328 | + if (mockPollUrl.equals(req.getUrl().toString())) { |
| 329 | + return Mono.just(new MockHttpResponse(pollRequest, 200, |
| 330 | + new HttpHeaders().set("Location", finalResultUrl), |
| 331 | + new PollResult("Succeeded"))); |
| 332 | + } else if (finalResultUrl.equals(req.getUrl().toString())) { |
| 333 | + return Mono.just(new MockHttpResponse(pollRequest, 200, new HttpHeaders(), |
| 334 | + new PollResult("final-state"))); |
| 335 | + } else { |
| 336 | + return Mono.error(new IllegalArgumentException("Unknown request URL " + req.getUrl())); |
| 337 | + } |
| 338 | + }); |
| 339 | + |
| 340 | + // PollingStrategy with context = Context.NONE |
| 341 | + PollerFlux<PollResult, PollResult> pollerFlux = PollerFlux.create( |
| 342 | + Duration.ofSeconds(1), |
| 343 | + () -> activationOperation.get(), |
| 344 | + new DefaultPollingStrategy<>(new HttpPipelineBuilder().httpClient(httpClient).build(), null, null), |
| 345 | + new TypeReference<PollResult>() { }, new TypeReference<PollResult>() { }); |
| 346 | + |
| 347 | + StepVerifier.create(pollerFlux.map(AsyncPollResponse::getStatus)) |
| 348 | + .expectSubscription() |
| 349 | + .expectNext(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) |
| 350 | + .verifyComplete(); |
| 351 | + Assertions.assertEquals(Context.NONE, contextArgument.getValue()); |
| 352 | + |
| 353 | + // PollingStrategy with context |
| 354 | + final Context context = new Context("key", "value"); |
| 355 | + pollerFlux = PollerFlux.create( |
| 356 | + Duration.ofSeconds(1), |
| 357 | + () -> activationOperation.get(), |
| 358 | + new DefaultPollingStrategy<>(new HttpPipelineBuilder().httpClient(httpClient).build(), null, context), |
| 359 | + new TypeReference<PollResult>() { }, new TypeReference<PollResult>() { }); |
| 360 | + |
| 361 | + StepVerifier.create(pollerFlux.map(AsyncPollResponse::getStatus)) |
| 362 | + .expectSubscription() |
| 363 | + .expectNext(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) |
| 364 | + .verifyComplete(); |
| 365 | + Assertions.assertEquals(context, contextArgument.getValue()); |
| 366 | + |
| 367 | + assertEquals(2, activationCallCount[0]); |
| 368 | + } |
| 369 | + |
305 | 370 | public static class PollResult {
|
306 | 371 | private String status;
|
307 | 372 | private String resourceLocation;
|
|
0 commit comments