Add unit test for HTTP request accessor.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 2 Nov 2014 11:58:42 +0000 (12:58 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 2 Nov 2014 11:58:42 +0000 (12:58 +0100)
src/test/java/net/pterodactylus/sone/template/HttpRequestAccessorTest.java [new file with mode: 0644]

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 (file)
index 0000000..37c6260
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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.<Object>is(REQUEST_PATH));
+               verify(httpRequest, never()).getHeader("Path");
+       }
+
+       @Test
+       public void headerIsReturnedCorrectly() {
+               assertThat(accessor.get(context, httpRequest, "User-Agent"),
+                               Matchers.<Object>is(USER_AGENT));
+       }
+
+}