From: David ‘Bombe’ Roden Date: Sun, 2 Nov 2014 11:58:42 +0000 (+0100) Subject: Add unit test for HTTP request accessor. X-Git-Tag: 0.9-rc1^2~3^2~56 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=695f8b03dc664c79581c7a7e8eb56bedb1d8ba0c;p=Sone.git Add unit test for HTTP request accessor. --- diff --git a/src/test/java/net/pterodactylus/sone/template/HttpRequestAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/HttpRequestAccessorTest.java new file mode 100644 index 0000000..37c6260 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/HttpRequestAccessorTest.java @@ -0,0 +1,51 @@ +package net.pterodactylus.sone.template; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import net.pterodactylus.util.template.TemplateContext; + +import freenet.support.api.HTTPRequest; + +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link HttpRequestAccessor}. + * + * @author David ‘Bombe’ Roden + */ +public class HttpRequestAccessorTest { + + private static final String REQUEST_PATH = "/the/real/path"; + private static final String USER_AGENT = "Test/1.0"; + private static final String HEADER_PATH = "/some/path"; + private final HttpRequestAccessor accessor = new HttpRequestAccessor(); + private final TemplateContext context = mock(TemplateContext.class); + private final HTTPRequest httpRequest = mock(HTTPRequest.class); + + @Before + public void setupHttpRequest() { + when(httpRequest.getPath()).thenReturn(REQUEST_PATH); + when(httpRequest.getHeader("User-Agent")).thenReturn(USER_AGENT); + when(httpRequest.getHeader("Path")).thenReturn(HEADER_PATH); + } + + @Test + public void preferCallingMethodsInsteadOfReturningHeaders() { + assertThat(accessor.get(context, httpRequest, "path"), + Matchers.is(REQUEST_PATH)); + verify(httpRequest, never()).getHeader("Path"); + } + + @Test + public void headerIsReturnedCorrectly() { + assertThat(accessor.get(context, httpRequest, "User-Agent"), + Matchers.is(USER_AGENT)); + } + +}