Commit 924211b4 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

information about authentication token is put in the generated curl commands

parent f6f99c65
......@@ -66,6 +66,7 @@ import lcsb.mapviewer.services.interfaces.IModelService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.web.config.SpringWebConfig;
import lcsb.mapviewer.web.utils.CommandFormatterWithReplacingPostFilenameHeader;
import lcsb.mapviewer.web.utils.CustomCurlRequestSnippet;
@WebAppConfiguration
@ContextConfiguration(classes = SpringWebConfig.class)
......@@ -142,7 +143,7 @@ abstract public class ControllerIntegrationTest {
.and()
.uris().withHost("minerva-service.lcsb.uni.lu/minerva/api").withPort(443).withScheme("https")
.and().snippets().withDefaults(
CliDocumentation.curlRequest(new CommandFormatterWithReplacingPostFilenameHeader(" \\%n ")),
new CustomCurlRequestSnippet(null, new CommandFormatterWithReplacingPostFilenameHeader(" \\%n ")),
CliDocumentation.httpieRequest(),
HttpDocumentation.httpRequest(),
HttpDocumentation.httpResponse(),
......
......@@ -7,6 +7,8 @@ import org.apache.logging.log4j.Logger;
import org.springframework.restdocs.cli.CommandFormatter;
import org.springframework.util.CollectionUtils;
import lcsb.mapviewer.common.Configuration;
/**
* 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
......@@ -23,6 +25,12 @@ public class CommandFormatterWithReplacingPostFilenameHeader implements CommandF
private String separator;
/**
* Information if in the formatted list of string there should also be
* information about session.
*/
private boolean session = false;
public CommandFormatterWithReplacingPostFilenameHeader(String separator) {
this.separator = separator;
}
......@@ -45,6 +53,9 @@ public class CommandFormatterWithReplacingPostFilenameHeader implements CommandF
contentFileName = element.replace("-H 'post-filename: ", "").replace("'", "");
}
}
if (session) {
elements.add("--cookie \"" + Configuration.AUTH_TOKEN + "=xxxxxxxx\"");
}
StringBuilder result = new StringBuilder();
for (String element : elements) {
......@@ -59,4 +70,8 @@ public class CommandFormatterWithReplacingPostFilenameHeader implements CommandF
return result.toString();
}
public void setSessionAvailable(boolean session) {
this.session = session;
}
}
\ No newline at end of file
package lcsb.mapviewer.web.utils;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.restdocs.cli.CommandFormatter;
import org.springframework.restdocs.cli.CurlRequestSnippet;
import org.springframework.restdocs.operation.Operation;
/**
* Custom modification of {@link CurlRequestSnippet} that documents the curl
* command for a request. The modification adds to the formatter information if
* session object was present in the query. This allows a formatter to add
* information about authentication token to the generated curl commands.
*
* @author Piotr Gawron
*
*/
public class CustomCurlRequestSnippet extends CurlRequestSnippet {
Logger logger = LogManager.getLogger();
private final CommandFormatter commandFormatter;
public CustomCurlRequestSnippet(CommandFormatter commandFormatter) {
super(commandFormatter);
this.commandFormatter = commandFormatter;
}
public CustomCurlRequestSnippet(Map<String, Object> attributes, CommandFormatter commandFormatter) {
super(attributes, commandFormatter);
this.commandFormatter = commandFormatter;
}
@Override
protected Map<String, Object> createModel(Operation operation) {
Object obj = operation.getAttributes().get("org.springframework.mock.web.MockHttpServletRequest");
boolean session = false;
if (obj instanceof org.springframework.mock.web.MockHttpServletRequest) {
session = (((org.springframework.mock.web.MockHttpServletRequest) obj).getSession() != null);
}
if (commandFormatter instanceof CommandFormatterWithReplacingPostFilenameHeader) {
((CommandFormatterWithReplacingPostFilenameHeader) commandFormatter).setSessionAvailable(session);
}
return super.createModel(operation);
}
}
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