X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FWebPageTest.java;h=97749154d815abf8a42422cd77e2f5581f784fe7;hp=3c6b1d4b5b85294278f2b036eb7c8566b2c6d4d7;hb=655d836ea81170e883cf229573bc5edd0169cee7;hpb=de7568a82eb4150bf6d2b0553841b7b69f84c968 diff --git a/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java index 3c6b1d4..9774915 100644 --- a/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java @@ -2,6 +2,7 @@ package net.pterodactylus.sone.web.pages; import static net.pterodactylus.sone.test.GuiceKt.supply; import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo; +import static net.pterodactylus.util.web.Method.GET; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; @@ -24,9 +25,11 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.Set; import javax.annotation.Nonnull; +import javax.naming.SizeLimitExceededException; import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.core.Preferences; @@ -68,6 +71,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.rules.ExpectedException; import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -92,6 +96,7 @@ public abstract class WebPageTest { protected final TemplateContext templateContext = new TemplateContext(); protected final HTTPRequest httpRequest = mock(HTTPRequest.class); protected final Multimap requestParameters = ArrayListMultimap.create(); + protected final Map requestParts = new HashMap<>(); protected final Map requestHeaders = new HashMap<>(); private final Map uploadedFilesNames = new HashMap<>(); private final Map uploadedFilesContentTypes = new HashMap<>(); @@ -121,7 +126,8 @@ public abstract class WebPageTest { } @Before - public final void setupFreenetRequest() { + public final void setupFreenetRequest() throws SizeLimitExceededException { + setMethod(GET); when(freenetRequest.getToadletContext()).thenReturn(toadletContext); when(freenetRequest.getHttpRequest()).thenReturn(httpRequest); when(httpRequest.getMultipleParam(anyString())).thenAnswer(new Answer() { @@ -130,13 +136,35 @@ public abstract class WebPageTest { return requestParameters.get(invocation.getArgument(0)).toArray(new String[0]); } }); + when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer() { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + return requestParts.get(invocation.getArgument(0)) != null; + } + }); + when(httpRequest.getParts()).thenAnswer(new Answer() { + @Override + public String[] answer(InvocationOnMock invocation) throws Throwable { + return requestParts.keySet().toArray(new String[requestParts.size()]); + } + }); when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer() { @Override public String answer(InvocationOnMock invocation) throws Throwable { String parameter = invocation.getArgument(0); int maxLength = invocation.getArgument(1); - Collection values = requestParameters.get(parameter); - return requestParameters.containsKey(parameter) ? values.iterator().next().substring(0, Math.min(maxLength, values.iterator().next().length())) : ""; + String value = requestParts.get(parameter); + return requestParts.containsKey(parameter) ? value.substring(0, Math.min(maxLength, value.length())) : ""; + } + }); + when(httpRequest.getPartAsStringThrowing(anyString(), anyInt())).thenAnswer(new Answer() { + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + String partName = invocation.getArgument(0); + if (!requestParts.containsKey(partName)) throw new NoSuchElementException(); + String partValue = requestParts.get(partName); + if (partValue.length() > invocation.getArgument(1)) throw new SizeLimitExceededException(); + return partValue; } }); when(httpRequest.hasParameters()).thenAnswer(new Answer() { @@ -172,19 +200,6 @@ public abstract class WebPageTest { requestParameters.get(invocation.getArgument(0)).iterator().next() != null; } }); - when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer() { - @Override - public Boolean answer(InvocationOnMock invocation) throws Throwable { - return requestParameters.containsKey(invocation.getArgument(0)) && - requestParameters.get(invocation.getArgument(0)).iterator().next() != null; - } - }); - when(httpRequest.getParts()).thenAnswer(new Answer() { - @Override - public String[] answer(InvocationOnMock invocation) throws Throwable { - return requestParameters.keySet().toArray(new String[requestParameters.size()]); - } - }); when(httpRequest.getHeader(anyString())).thenAnswer(new Answer() { @Override public String answer(InvocationOnMock invocation) throws Throwable { @@ -274,10 +289,11 @@ public abstract class WebPageTest { @Before public final void setupWebInterface() { + when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone); when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone); when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone); when(webInterface.getNotification(anyString())).thenReturn(Optional.absent()); - when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList()); + when(webInterface.getNotifications(Mockito.any())).thenReturn(new ArrayList()); } @Before @@ -290,18 +306,23 @@ public abstract class WebPageTest { } protected void unsetCurrentSone() { + when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null); when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null); when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null); } - protected void request(String uri, Method method) { + protected void setMethod(Method method) { + when(freenetRequest.getMethod()).thenReturn(method); + when(httpRequest.getMethod()).thenReturn(method.name()); + } + + protected void request(String uri) { try { when(httpRequest.getPath()).thenReturn(uri); when(freenetRequest.getUri()).thenReturn(new URI(uri)); } catch (URISyntaxException e) { throw new RuntimeException(e); } - when(freenetRequest.getMethod()).thenReturn(method); } protected void addHttpRequestHeader(@Nonnull String name, String value) { @@ -312,6 +333,10 @@ public abstract class WebPageTest { requestParameters.put(name, value); } + protected void addHttpRequestPart(String name, String value) { + requestParts.put(name, value); + } + protected void addPost(String postId, Post post) { when(core.getPost(postId)).thenReturn(Optional.fromNullable(post)); } @@ -382,4 +407,13 @@ public abstract class WebPageTest { } } + protected void verifyNoRedirect(Runnable verification) throws RedirectException { + getPage().handleRequest(freenetRequest, templateContext); + verification.run(); + } + + protected void addTranslation(@Nonnull String key, @Nonnull String value) { + when(l10n.getString(key)).thenReturn(value); + } + }