From 695f8b03dc664c79581c7a7e8eb56bedb1d8ba0c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 2 Nov 2014 12:58:42 +0100 Subject: [PATCH] Add unit test for HTTP request accessor. --- .../sone/template/HttpRequestAccessorTest.java | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/HttpRequestAccessorTest.java 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)); + } + +} -- 2.7.4