|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2019-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.datadog.api.client.auth; |
| 8 | + |
| 9 | +import static org.junit.Assert.*; |
| 10 | + |
| 11 | +import com.datadog.api.client.ApiClient; |
| 12 | +import com.datadog.api.client.ApiException; |
| 13 | +import com.datadog.api.client.Pair; |
| 14 | +import java.net.URI; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +public class HttpBearerAuthTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testSetAndGetBearerToken() { |
| 25 | + HttpBearerAuth auth = new HttpBearerAuth("bearer"); |
| 26 | + assertNull(auth.getBearerToken()); |
| 27 | + |
| 28 | + auth.setBearerToken("test-token-123"); |
| 29 | + assertEquals("test-token-123", auth.getBearerToken()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testApplyToParamsAddsBearerHeader() throws ApiException { |
| 34 | + HttpBearerAuth auth = new HttpBearerAuth("bearer"); |
| 35 | + auth.setBearerToken("my-pat-token"); |
| 36 | + |
| 37 | + List<Pair> queryParams = new ArrayList<>(); |
| 38 | + Map<String, String> headerParams = new HashMap<>(); |
| 39 | + Map<String, String> cookieParams = new HashMap<>(); |
| 40 | + |
| 41 | + auth.applyToParams( |
| 42 | + queryParams, headerParams, cookieParams, "", "", URI.create("https://example.com")); |
| 43 | + |
| 44 | + assertEquals("Bearer my-pat-token", headerParams.get("Authorization")); |
| 45 | + assertTrue(queryParams.isEmpty()); |
| 46 | + assertTrue(cookieParams.isEmpty()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testApplyToParamsNullTokenIsNoOp() throws ApiException { |
| 51 | + HttpBearerAuth auth = new HttpBearerAuth("bearer"); |
| 52 | + |
| 53 | + List<Pair> queryParams = new ArrayList<>(); |
| 54 | + Map<String, String> headerParams = new HashMap<>(); |
| 55 | + Map<String, String> cookieParams = new HashMap<>(); |
| 56 | + |
| 57 | + auth.applyToParams( |
| 58 | + queryParams, headerParams, cookieParams, "", "", URI.create("https://example.com")); |
| 59 | + |
| 60 | + assertFalse(headerParams.containsKey("Authorization")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testApiClientRegistersBearerAuth() { |
| 65 | + ApiClient client = new ApiClient(); |
| 66 | + // setBearerToken should not throw since bearerAuth is now registered |
| 67 | + client.setBearerToken("test-pat"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testApiClientSetBearerTokenApplied() throws ApiException { |
| 72 | + ApiClient client = new ApiClient(); |
| 73 | + client.setBearerToken("my-pat-token"); |
| 74 | + |
| 75 | + // Verify through the authentication map that the token was set |
| 76 | + Map<String, Authentication> auths = client.getAuthentications(); |
| 77 | + Authentication auth = auths.get("bearerAuth"); |
| 78 | + assertNotNull("bearerAuth should be registered", auth); |
| 79 | + assertTrue("bearerAuth should be HttpBearerAuth", auth instanceof HttpBearerAuth); |
| 80 | + assertEquals("my-pat-token", ((HttpBearerAuth) auth).getBearerToken()); |
| 81 | + } |
| 82 | +} |
0 commit comments