|
| 1 | +package com.adyen.httpclient; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.junit.Assert.fail; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import java.net.URI; |
| 10 | +import org.apache.hc.core5.http.Header; |
| 11 | +import org.apache.hc.core5.http.HttpException; |
| 12 | +import org.apache.hc.core5.http.HttpRequest; |
| 13 | +import org.apache.hc.core5.http.HttpResponse; |
| 14 | +import org.apache.hc.core5.http.protocol.HttpContext; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.mockito.Mock; |
| 18 | +import org.mockito.junit.MockitoJUnitRunner; |
| 19 | + |
| 20 | +@RunWith(MockitoJUnitRunner.class) |
| 21 | +public class AdyenCustomRedirectStrategyTest { |
| 22 | + |
| 23 | + private final AdyenCustomRedirectStrategy redirectStrategy = new AdyenCustomRedirectStrategy(); |
| 24 | + |
| 25 | + @Mock private HttpRequest request; |
| 26 | + @Mock private HttpResponse response; |
| 27 | + @Mock private HttpContext context; |
| 28 | + @Mock private Header locationHeader; |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testIsVerifyLocationAdyenDomain() { |
| 32 | + assertTrue(redirectStrategy.isVerifyLocation("https://test.adyen.com/redirect")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testIsVerifyLocationAdyenPaymentsDomain() { |
| 37 | + assertTrue(redirectStrategy.isVerifyLocation("https://test.adyenpayments.com/redirect")); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testIsVerifyLocationMixedCase() { |
| 42 | + assertTrue(redirectStrategy.isVerifyLocation("https://TEST.ADYEN.COM/redirect")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testIsVerifyLocationInvalidDomain() { |
| 47 | + assertFalse(redirectStrategy.isVerifyLocation("https://example.com/redirect")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testIsVerifyLocationMaliciousDomain() { |
| 52 | + assertFalse(redirectStrategy.isVerifyLocation("https://adyen.com.evil.com")); |
| 53 | + assertFalse(redirectStrategy.isVerifyLocation("https://evil.com?q=.adyen.com")); |
| 54 | + assertFalse(redirectStrategy.isVerifyLocation("https://evil.com/.adyen.com")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testIsVerifyLocationEmpty() { |
| 59 | + assertFalse(redirectStrategy.isVerifyLocation("")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testGetLocationURI308AdyenDomain() throws HttpException { |
| 64 | + when(response.getCode()).thenReturn(308); |
| 65 | + when(response.getFirstHeader("Location")).thenReturn(locationHeader); |
| 66 | + when(locationHeader.getValue()).thenReturn("https://api.adyen.com"); |
| 67 | + |
| 68 | + URI uri = redirectStrategy.getLocationURI(request, response, context); |
| 69 | + assertEquals("https://api.adyen.com/", uri.toString()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testGetLocationURI308AdyenPaymentsDomain() throws HttpException { |
| 74 | + when(response.getCode()).thenReturn(308); |
| 75 | + when(response.getFirstHeader("Location")).thenReturn(locationHeader); |
| 76 | + when(locationHeader.getValue()).thenReturn("https://api.adyenpayments.com"); |
| 77 | + |
| 78 | + URI uri = redirectStrategy.getLocationURI(request, response, context); |
| 79 | + assertEquals("https://api.adyenpayments.com/", uri.toString()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGetLocationURI308InvadidDomainThrowsException() { |
| 84 | + when(response.getCode()).thenReturn(308); |
| 85 | + when(response.getFirstHeader("Location")).thenReturn(locationHeader); |
| 86 | + String location = "https://test.example.com/"; |
| 87 | + when(locationHeader.getValue()).thenReturn(location); |
| 88 | + |
| 89 | + try { |
| 90 | + redirectStrategy.getLocationURI(request, response, context); |
| 91 | + fail("Expected HttpException was not thrown"); |
| 92 | + } catch (HttpException e) { |
| 93 | + assertEquals("Redirect location is invalid: " + location, e.getMessage()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments