|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +package org.opensearch.dataprepper.plugins.source.atlassian.rest; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.mockito.Mock; |
| 16 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 17 | +import org.opensearch.dataprepper.plugins.source.atlassian.rest.auth.AtlassianBearerTokenAuthConfig; |
| 18 | +import org.springframework.http.HttpHeaders; |
| 19 | +import org.springframework.http.HttpRequest; |
| 20 | +import org.springframework.http.client.ClientHttpRequestExecution; |
| 21 | +import org.springframework.http.client.ClientHttpResponse; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | + |
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | +import static org.hamcrest.Matchers.equalTo; |
| 27 | +import static org.mockito.Mockito.verify; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +@ExtendWith(MockitoExtension.class) |
| 31 | +class BearerTokenInterceptorTest { |
| 32 | + |
| 33 | + @Mock |
| 34 | + private AtlassianBearerTokenAuthConfig authConfig; |
| 35 | + |
| 36 | + @Mock |
| 37 | + private HttpRequest request; |
| 38 | + |
| 39 | + @Mock |
| 40 | + private ClientHttpRequestExecution execution; |
| 41 | + |
| 42 | + @Mock |
| 43 | + private ClientHttpResponse response; |
| 44 | + |
| 45 | + @Test |
| 46 | + void testInterceptSetsBearerAuthHeader() throws IOException { |
| 47 | + when(authConfig.getBearerToken()).thenReturn("my-token"); |
| 48 | + HttpHeaders headers = new HttpHeaders(); |
| 49 | + when(request.getHeaders()).thenReturn(headers); |
| 50 | + when(execution.execute(request, new byte[0])).thenReturn(response); |
| 51 | + |
| 52 | + BearerTokenInterceptor interceptor = new BearerTokenInterceptor(authConfig); |
| 53 | + interceptor.intercept(request, new byte[0], execution); |
| 54 | + |
| 55 | + verify(execution).execute(request, new byte[0]); |
| 56 | + assertThat(headers.getFirst(HttpHeaders.AUTHORIZATION), equalTo("Bearer my-token")); |
| 57 | + } |
| 58 | +} |
0 commit comments