Created
December 8, 2016 20:48
-
-
Save yupadhyay/32f17fc2d32e830ac689908005753c0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wemblog.util; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.assertNull; | |
import static org.junit.Assert.assertTrue; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.when; | |
import javax.servlet.http.Cookie; | |
import javax.servlet.jsp.PageContext; | |
import org.apache.sling.api.SlingHttpServletRequest; | |
import org.apache.sling.api.resource.NonExistingResource; | |
import org.apache.sling.api.resource.Resource; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.Mock; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import com.day.cq.commons.Externalizer; | |
@RunWith(MockitoJUnitRunner.class) | |
public class LiCommonRequestUtilsTest { | |
@Mock | |
SlingHttpServletRequest _httpServletRequest; | |
@Mock | |
ResourceResolver _resourceResolver; | |
@Mock | |
Resource _resource; | |
@Mock | |
Cookie _bCookie; | |
@Mock | |
Externalizer _externalizer; | |
@Before | |
public void setUp() { | |
when(_httpServletRequest.getServerName()).thenReturn("www.test.com"); | |
when(_httpServletRequest.getRequestURI()).thenReturn("/something/andsomething"); | |
} | |
/** | |
* 1. When there is Host header 2. No Host Header 3. Host Header Null 4. Host Header Empty | |
* | |
*/ | |
@Test | |
public void getServerNameTest() { | |
assertNull(LiCommonRequestUtils.getServerName(null)); | |
assertEquals(LiCommonRequestUtils.getServerName(_httpServletRequest), "www.test.com"); | |
when(_httpServletRequest.getHeader("Host")).thenReturn("www.test2.com"); | |
assertEquals(LiCommonRequestUtils.getServerName(_httpServletRequest), "www.test2.com"); | |
when(_httpServletRequest.getHeader("Host")).thenReturn(""); | |
assertEquals(LiCommonRequestUtils.getServerName(_httpServletRequest), "www.test.com"); | |
when(_httpServletRequest.getHeader("Host")).thenReturn(null); | |
assertEquals(LiCommonRequestUtils.getServerName(_httpServletRequest), "www.test.com"); | |
} | |
/** | |
* 1. Null test 2. With http header only 3. With http header and netscalar header 4. With https header 5. With https | |
* and null netscalar header | |
*/ | |
@Test | |
public void isHttpOrHttpsRequestTest() { | |
// For null input | |
assertFalse(LiCommonRequestUtils.isHTTPRequest(null)); | |
assertFalse(LiCommonRequestUtils.isHTTPSRequest(null)); | |
.thenReturn(LiCommonRequestUtils.HTTPS_PROTOCOL); | |
assertFalse(LiCommonRequestUtils.isHTTPRequest(_httpServletRequest)); | |
assertTrue(LiCommonRequestUtils.isHTTPSRequest(_httpServletRequest)); | |
when(_httpServletRequest.getProtocol()).thenReturn("HTTP/1.1"); | |
assertTrue(LiCommonRequestUtils.isHTTPRequest(_httpServletRequest)); | |
assertFalse(LiCommonRequestUtils.isHTTPSRequest(_httpServletRequest)); | |
// If it is https request | |
when(_httpServletRequest.getProtocol()).thenReturn("HTTPS/1.1"); | |
assertFalse(LiCommonRequestUtils.isHTTPRequest(_httpServletRequest)); | |
assertTrue(LiCommonRequestUtils.isHTTPSRequest(_httpServletRequest)); | |
} | |
@Test | |
public void getCompleteResourcePathTest() { | |
// Null check | |
assertNull(LiCommonRequestUtils.getCompleteResourcePath(null)); | |
// Exact Path check | |
when(_httpServletRequest.getRequestURI()).thenReturn("/content/test/test"); | |
when(_resource.getPath()).thenReturn("/content/test/test"); | |
when(_resourceResolver.resolve("/content/test/test")).thenReturn(_resource); | |
when(_httpServletRequest.getResourceResolver()).thenReturn(_resourceResolver); | |
assertEquals(LiCommonRequestUtils.getCompleteResourcePath(_httpServletRequest), "/content/test/test"); | |
// Vanity Path check | |
when(_httpServletRequest.getRequestURI()).thenReturn("/vanity/test/test"); | |
when(_resourceResolver.resolve("/vanity/test/test")).thenReturn(_resource); | |
assertEquals(LiCommonRequestUtils.getCompleteResourcePath(_httpServletRequest), "/content/test/test"); | |
// Non existance Resource check | |
when(_resourceResolver.resolve("/vanity/test/test")).thenReturn(new NonExistingResource(_resourceResolver, null)); | |
assertNull(LiCommonRequestUtils.getCompleteResourcePath(_httpServletRequest)); | |
} | |
/** | |
* 1. True 2. True 3. False 4. False 5. False 6. False 7. False | |
*/ | |
@Test | |
public void isValidIPAddressTest() { | |
// true for valid IPV4 | |
assertTrue(LiCommonRequestUtils.isValidIPAddress("199.101.162.221")); | |
// true for valid IPV6 | |
assertTrue(LiCommonRequestUtils.isValidIPAddress("2620:109:c00d:100::c765:a381")); | |
// false if IP is null | |
assertFalse(LiCommonRequestUtils.isValidIPAddress(null)); | |
// false if ip is blank | |
assertFalse(LiCommonRequestUtils.isValidIPAddress("")); | |
// false if its a random string | |
assertFalse(LiCommonRequestUtils.isValidIPAddress("1.3.1.1.1.1.1.1.1")); | |
// false if its a random string | |
assertFalse(LiCommonRequestUtils.isValidIPAddress("abc.f.ve.c")); | |
// false if its a random string | |
assertFalse(LiCommonRequestUtils.isValidIPAddress("test")); | |
} | |
/** | |
* 1. True 2. True 3. False 4. False 5. False 6. False 7. False 8. False | |
*/ | |
@Test | |
public void isIPV6AddressTest() { | |
// true for valid compressed IPV6 | |
assertTrue(LiCommonRequestUtils.isIPV6Address("2620:109:c00d:100::c765:a381")); | |
// true for valid standard IPV6 | |
assertTrue(LiCommonRequestUtils.isIPV6Address("3ffe:1900:4545:3:200:f8ff:fe21:67cf")); | |
// false if IP is null | |
assertFalse(LiCommonRequestUtils.isIPV6Address(null)); | |
// false if ip is blank | |
assertFalse(LiCommonRequestUtils.isIPV6Address("")); | |
// false if its a random string | |
assertFalse(LiCommonRequestUtils.isIPV6Address("1.3.1.1.1.1.1.1.1")); | |
// false if its a random string | |
assertFalse(LiCommonRequestUtils.isIPV6Address("abc.f.ve.c")); | |
// false if its a random string | |
assertFalse(LiCommonRequestUtils.isIPV6Address("test")); | |
// true for valid IPV4 | |
assertFalse(LiCommonRequestUtils.isIPV6Address("199.101.162.221")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment