|
| 1 | +package com.cloudbees.jenkins; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import javax.servlet.FilterChain; |
| 7 | +import javax.servlet.http.HttpServletRequest; |
| 8 | +import javax.servlet.http.HttpServletResponse; |
| 9 | + |
| 10 | +import static junit.framework.Assert.assertFalse; |
| 11 | +import static junit.framework.TestCase.assertTrue; |
| 12 | +import static org.mockito.Mockito.mock; |
| 13 | +import static org.mockito.Mockito.never; |
| 14 | +import static org.mockito.Mockito.times; |
| 15 | +import static org.mockito.Mockito.verify; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +public class GitHubWebHookCrumbExclusionTest { |
| 19 | + |
| 20 | + private GitHubWebHookCrumbExclusion exclusion; |
| 21 | + private HttpServletRequest req; |
| 22 | + private HttpServletResponse resp; |
| 23 | + private FilterChain chain; |
| 24 | + |
| 25 | + @Before |
| 26 | + public void before() { |
| 27 | + exclusion = new GitHubWebHookCrumbExclusion(); |
| 28 | + req = mock(HttpServletRequest.class); |
| 29 | + resp = mock(HttpServletResponse.class); |
| 30 | + chain = mock(FilterChain.class); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testFullPath() throws Exception { |
| 35 | + when(req.getPathInfo()).thenReturn("/github-webhook/"); |
| 36 | + assertTrue(exclusion.process(req, resp, chain)); |
| 37 | + verify(chain, times(1)).doFilter(req, resp); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testFullPathWithoutSlash() throws Exception { |
| 42 | + when(req.getPathInfo()).thenReturn("/github-webhook"); |
| 43 | + assertTrue(exclusion.process(req, resp, chain)); |
| 44 | + verify(chain, times(1)).doFilter(req, resp); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testInvalidPath() throws Exception { |
| 49 | + when(req.getPathInfo()).thenReturn("/some-other-url/"); |
| 50 | + assertFalse(exclusion.process(req, resp, chain)); |
| 51 | + verify(chain, never()).doFilter(req, resp); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testNullPath() throws Exception { |
| 56 | + when(req.getPathInfo()).thenReturn(null); |
| 57 | + assertFalse(exclusion.process(req, resp, chain)); |
| 58 | + verify(chain, never()).doFilter(req, resp); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testEmptyPath() throws Exception { |
| 63 | + when(req.getPathInfo()).thenReturn(""); |
| 64 | + assertFalse(exclusion.process(req, resp, chain)); |
| 65 | + verify(chain, never()).doFilter(req, resp); |
| 66 | + } |
| 67 | +} |
0 commit comments