Commit b3eea197 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

possibility to modify curl samples that will replace -d 'parameters' with --data-binary @filename

parent 3b763c30
......@@ -26,7 +26,10 @@ import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.cli.CliDocumentation;
import org.springframework.restdocs.http.HttpDocumentation;
import org.springframework.restdocs.operation.preprocess.Preprocessors;
import org.springframework.restdocs.payload.PayloadDocumentation;
import org.springframework.restdocs.request.PathParametersSnippet;
import org.springframework.restdocs.request.RequestParametersSnippet;
import org.springframework.security.crypto.password.PasswordEncoder;
......@@ -62,6 +65,7 @@ import lcsb.mapviewer.persist.dao.user.UserDao;
import lcsb.mapviewer.services.interfaces.IModelService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.web.config.SpringWebConfig;
import lcsb.mapviewer.web.utils.CommandFormatterWithReplacingPostFilenameHeader;
@WebAppConfiguration
@ContextConfiguration(classes = SpringWebConfig.class)
......@@ -136,7 +140,14 @@ abstract public class ControllerIntegrationTest {
.operationPreprocessors()
.withResponseDefaults(Preprocessors.prettyPrint())
.and()
.uris().withHost("minerva-dev.lcsb.uni.lu").withPort(80))
.uris().withHost("minerva-dev.lcsb.uni.lu").withPort(80)
.and().snippets().withDefaults(
CliDocumentation.curlRequest(new CommandFormatterWithReplacingPostFilenameHeader(" \\%n ")),
CliDocumentation.httpieRequest(),
HttpDocumentation.httpRequest(),
HttpDocumentation.httpResponse(),
PayloadDocumentation.requestBody(),
PayloadDocumentation.responseBody()))
.build();
executorService = Executors.newFixedThreadPool(1);
......
package lcsb.mapviewer.web.utils;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.restdocs.cli.CommandFormatter;
import org.springframework.util.CollectionUtils;
/**
* This is a class for workaround the issue that when we put content of file
* inside the request we would like to provide human readable curl request.In
* such cases the curl example should not contain content of the file but simple
* reference @filename. The filename is taken from header "post-filename" which
* will be removed from the curl sample.
*
* @author Piotr Gawron
*
*/
public class CommandFormatterWithReplacingPostFilenameHeader implements CommandFormatter {
Logger logger = LogManager.getLogger();
private String separator;
public CommandFormatterWithReplacingPostFilenameHeader(String separator) {
this.separator = separator;
}
/**
* Concatenates a list of {@code String}s with a specified separator.
*
* @param elements
* a list of {@code String}s to be concatenated
* @return concatenated list of {@code String}s as one {@code String}
*/
@Override
public String format(List<String> elements) {
if (CollectionUtils.isEmpty(elements)) {
return "";
}
String contentFileName = null;
for (String element : elements) {
if (element.startsWith("-H 'post-filename:")) {
contentFileName = element.replace("-H 'post-filename: ", "").replace("'", "");
}
}
StringBuilder result = new StringBuilder();
for (String element : elements) {
if (element.startsWith("-d") && contentFileName != null) {
element = "--data-binary @" + contentFileName;
}
if (!element.startsWith("-H 'post-filename:")) {
result.append(String.format(this.separator));
result.append(element);
}
}
return result.toString();
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment