+ String parameter = invocation.getArgument(0);
+ int maxLength = invocation.getArgument(1);
+ Collection<String> values = requestParameters.get(parameter);
+ return requestParameters.containsKey(parameter) ? values.iterator().next().substring(0, Math.min(maxLength, values.iterator().next().length())) : "";
+ }
+ });
+ when(httpRequest.hasParameters()).thenAnswer(new Answer<Boolean>() {
+ @Override
+ public Boolean answer(InvocationOnMock invocation) throws Throwable {
+ return !requestParameters.isEmpty();
+ }
+ });
+ when(httpRequest.getParameterNames()).thenAnswer(new Answer<Collection<String>>() {
+ @Override
+ public Collection<String> answer(InvocationOnMock invocation) throws Throwable {
+ return requestParameters.keySet();
+ }
+ });
+ when(httpRequest.getParam(anyString())).thenAnswer(new Answer<String>() {
+ @Override
+ public String answer(InvocationOnMock invocation) throws Throwable {
+ String parameter = invocation.getArgument(0);
+ return requestParameters.containsKey(parameter) ? requestParameters.get(parameter).iterator().next() : "";
+ }
+ });
+ when(httpRequest.getParam(anyString(), ArgumentMatchers.<String>any())).thenAnswer(new Answer<String>() {
+ @Override
+ public String answer(InvocationOnMock invocation) throws Throwable {
+ String parameter = invocation.getArgument(0);
+ return requestParameters.containsKey(parameter) ? requestParameters.get(parameter).iterator().next() : invocation.<String>getArgument(1);
+ }
+ });
+ when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer<Boolean>() {
+ @Override
+ public Boolean answer(InvocationOnMock invocation) throws Throwable {
+ return requestParameters.containsKey(invocation.<String>getArgument(0)) &&
+ requestParameters.get(invocation.<String>getArgument(0)).iterator().next() != null;
+ }
+ });
+ when(httpRequest.getParts()).thenAnswer(new Answer<String[]>() {
+ @Override
+ public String[] answer(InvocationOnMock invocation) throws Throwable {
+ return requestParameters.keySet().toArray(new String[requestParameters.size()]);
+ }
+ });
+ when(httpRequest.getHeader(anyString())).thenAnswer(new Answer<String>() {
+ @Override
+ public String answer(InvocationOnMock invocation) throws Throwable {
+ return requestHeaders.get(invocation.<String>getArgument(0).toLowerCase());
+ }
+ });
+ when(httpRequest.getUploadedFile(anyString())).thenAnswer(new Answer<HTTPUploadedFile>() {
+ @Override
+ public HTTPUploadedFile answer(InvocationOnMock invocation) throws Throwable {
+ final String name = invocation.getArgument(0);
+ if (!uploadedFilesSources.containsKey(name)) {
+ return null;
+ }
+ return new HTTPUploadedFile() {
+ @Override
+ public String getContentType() {
+ return uploadedFilesContentTypes.get(name);
+ }
+
+ @Override
+ public Bucket getData() {
+ try (InputStream inputStream = getClass().getResourceAsStream(uploadedFilesSources.get(name))) {
+ byte[] bytes = ByteStreams.toByteArray(inputStream);
+ return new SimpleReadOnlyArrayBucket(bytes, 0, bytes.length);
+ } catch (IOException ioe1) {
+ return new NullBucket();
+ }
+ }
+
+ @Override
+ public String getFilename() {
+ return uploadedFilesNames.get(name);
+ }
+ };