From f8dce48157c64eec96d7c98d517bcf8d90092710 Mon Sep 17 00:00:00 2001 From: Morille Jerome Date: Fri, 19 Aug 2016 10:27:06 +0200 Subject: [PATCH] Add multi multipart files --- .gitignore | 3 +- pom.xml | 2 +- .../rest/fitnesse/fixture/RestFixture.java | 73 ++++++++++++++++++- 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ddffdeb..4b28db9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ target TODO .settings .m2 - +*.iml +.idea diff --git a/pom.xml b/pom.xml index 8eea3e2..d916c94 100644 --- a/pom.xml +++ b/pom.xml @@ -63,7 +63,7 @@ smartrics.restfixture smartrics-RestClient - 2.1 + 2.2-SNAPSHOT org.mozilla diff --git a/src/main/java/smartrics/rest/fitnesse/fixture/RestFixture.java b/src/main/java/smartrics/rest/fitnesse/fixture/RestFixture.java index c11bbb4..0b36afb 100644 --- a/src/main/java/smartrics/rest/fitnesse/fixture/RestFixture.java +++ b/src/main/java/smartrics/rest/fitnesse/fixture/RestFixture.java @@ -27,6 +27,7 @@ import org.slf4j.LoggerFactory; import smartrics.rest.client.RestClient; import smartrics.rest.client.RestData.Header; +import smartrics.rest.client.RestMultipart; import smartrics.rest.client.RestRequest; import smartrics.rest.client.RestResponse; import smartrics.rest.fitnesse.fixture.support.*; @@ -238,6 +239,8 @@ public Variables createRunnerVariables() { protected String multipartFileParameterName = FILE; + protected Map multiFileNameByParamName = new LinkedHashMap<>(); + protected String requestBody; protected boolean resourceUrisAreEscaped = false; @@ -470,6 +473,57 @@ public void setMultipartFileName() { } } + /** + * Allows setting of the name of the multi-part file to upload. + *

+ * | setMultipartFileName | Name of file | Name of form parameter for the uploaded file | ContentType of file | CharSet of file | + *

+ * body text should be location of file which needs to be sent + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void addMultipartFileName() { + CellWrapper cellFileName = row.getCell(1); + CellWrapper cellParamName = row.getCell(2); + CellWrapper cellContentType = row.getCell(3); + CellWrapper cellCharset = row.getCell(4); + if (cellFileName == null) { + getFormatter().exception(row.getCell(0), + "You must pass a multipart file name to set"); + } else { + // Param Name + String multipartParamName = FILE; + if (cellParamName !=null ) { + multipartParamName = GLOBALS.substitute(cellParamName.text()); + } + // FileName + String multipartFileName = GLOBALS.substitute(cellFileName.text()); + // ContentType + String multipartContentType = null; + if (cellContentType !=null ) { + multipartContentType = GLOBALS.substitute(cellContentType.text()); + } + // Charset + String multipartCharSet = null; + if (cellCharset !=null ) { + multipartCharSet = GLOBALS.substitute(cellCharset.text()); + } + // Register Multipart + RestMultipart restMultipart = new RestMultipart(multipartFileName, multipartContentType, multipartCharSet); + multiFileNameByParamName.put(multipartParamName, restMultipart); + // Display Replacement + renderReplacement(cellFileName, multipartFileName); + if (cellParamName!=null) { + renderReplacement(cellParamName, multipartParamName); + } + if (cellContentType!=null) { + renderReplacement(cellContentType, multipartContentType); + } + if (cellCharset!=null) { + renderReplacement(cellCharset, multipartCharSet); + } + } + } + /** * @return the multipart filename */ @@ -1023,11 +1077,24 @@ protected void doMethod(String method, String resUrl, Map header if (fileName != null) { getLastRequest().setFileName(fileName); } + + // Set multiFileName if (multipartFileName != null) { - getLastRequest().setMultipartFileName(multipartFileName); + RestMultipart restMultipart = new RestMultipart(multipartFileName ); + getLastRequest().addMultipartFile( multipartFileParameterName , restMultipart); +// getLastRequest().setMultipartFileName(multipartFileName); +// getLastRequest().setMultipartFileParameterName(multipartFileParameterName); + + } + + // Add multiFileName + if (!multiFileNameByParamName.isEmpty()) { + for (Map.Entry entryMultipart : multiFileNameByParamName.entrySet()) { + getLastRequest().addMultipartFile( entryMultipart.getKey(), entryMultipart.getValue()); + LOG.info(" addMultipartFileName : paramName = {} , fileName = {}", entryMultipart.getKey(), entryMultipart.getValue()); + } } - getLastRequest().setMultipartFileParameterName( - multipartFileParameterName); + String[] uri = resUrl.split("\\?", 2); String[] thisRequestUrlParts = buildThisRequestUrl(uri[0]);