|
| 1 | +package com.duosecurity.client; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.lang.reflect.Field; |
| 7 | +import okhttp3.Headers; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +public class HttpUserAgentTest { |
| 11 | + |
| 12 | + private Headers getHeaders(Http http) throws Exception { |
| 13 | + Field headersField = Http.class.getDeclaredField("headers"); |
| 14 | + headersField.setAccessible(true); |
| 15 | + Headers.Builder headersBuilder = (Headers.Builder) headersField.get(http); |
| 16 | + return headersBuilder.build(); |
| 17 | + } |
| 18 | + |
| 19 | + private String getUserAgent(Http http) throws Exception { |
| 20 | + return getHeaders(http).get("user-agent"); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testDefaultBuilder_includesCaBundleVersion() throws Exception { |
| 25 | + Http http = new Http.HttpBuilder("GET", "api-host.duosecurity.com", "/auth/v2/check") |
| 26 | + .build(); |
| 27 | + |
| 28 | + String userAgent = getUserAgent(http); |
| 29 | + assertTrue(userAgent.contains("ca_bundle/1.0")); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testDefaultBuilder_includesCaPinningEnabled() throws Exception { |
| 34 | + Http http = new Http.HttpBuilder("GET", "api-host.duosecurity.com", "/auth/v2/check") |
| 35 | + .build(); |
| 36 | + |
| 37 | + String userAgent = getUserAgent(http); |
| 38 | + assertTrue(userAgent.contains("(ca_pinning=enabled)")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testDisableCaPinning_includesCaPinningDisabled() throws Exception { |
| 43 | + Http http = new Http.HttpBuilder("GET", "api-host.duosecurity.com", "/auth/v2/check") |
| 44 | + .disableCaPinning() |
| 45 | + .build(); |
| 46 | + |
| 47 | + String userAgent = getUserAgent(http); |
| 48 | + assertTrue(userAgent.contains("(ca_pinning=disabled)")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testLegacyConstructor_includesCaBundleAndPinningEnabled() throws Exception { |
| 53 | + Http http = new Http("GET", "api-host.duosecurity.com", "/auth/v2/check"); |
| 54 | + |
| 55 | + String userAgent = getUserAgent(http); |
| 56 | + assertTrue(userAgent.contains("ca_bundle/1.0")); |
| 57 | + assertTrue(userAgent.contains("(ca_pinning=enabled)")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testCustomUserAgentHeader_isReplacedNotDuplicated() throws Exception { |
| 62 | + Http http = new Http.HttpBuilder("GET", "api-host.duosecurity.com", "/auth/v2/check") |
| 63 | + .addHeader("user-agent", "MyApp/1.0") |
| 64 | + .build(); |
| 65 | + |
| 66 | + Headers headers = getHeaders(http); |
| 67 | + assertEquals(1, headers.values("user-agent").size()); |
| 68 | + assertEquals(String.format("%s ca_bundle/1.0 (ca_pinning=enabled)", Http.UserAgentString), |
| 69 | + headers.get("user-agent")); |
| 70 | + } |
| 71 | +} |
0 commit comments