X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FWebPageTest.java;h=e04e780d6c0bf3d64f764195adf537b54e9a831c;hb=8da648b3e2b33064cb72aa87c4dc198b60bf0a7f;hp=532c09adc5f5d36b0821cf5358f46f066131d04c;hpb=fdc047dd37e982776d55d827ca405868efe32c5a;p=Sone.git 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 532c09a..e04e780 100644 --- a/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java @@ -24,9 +24,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; @@ -92,6 +94,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 +124,7 @@ public abstract class WebPageTest { } @Before - public final void setupFreenetRequest() { + public final void setupFreenetRequest() throws SizeLimitExceededException { when(freenetRequest.getToadletContext()).thenReturn(toadletContext); when(freenetRequest.getHttpRequest()).thenReturn(httpRequest); when(httpRequest.getMultipleParam(anyString())).thenAnswer(new Answer() { @@ -130,13 +133,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 +197,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 { @@ -304,6 +316,7 @@ public abstract class WebPageTest { throw new RuntimeException(e); } when(freenetRequest.getMethod()).thenReturn(method); + when(httpRequest.getMethod()).thenReturn(method.name()); } protected void addHttpRequestHeader(@Nonnull String name, String value) { @@ -314,6 +327,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)); }