4

I have an file upload endpoint (/document) in a controller defined as follows:

@RestController
public class FileUploadController {

    @Autowired
    private PersonCSVReaderService personCSVReaderService;

    @PostMapping(value = "/document", consumes= {MediaType.MULTIPART_FORM_DATA_VALUE})
    public void handleFileUpload3(@RequestPart("file") MultipartFile file, @RequestPart("metadata") DocumentMetadata metadata) {
        System.out.println(String.format("uploading file %s of %s bytes", file.getOriginalFilename(), file.getSize()));
        personCSVReaderService.readPersonCSV(file, metadata);
    }
}

I can test this endpoint using Advanced Rest Client (ARC) or Postman by defining the "file" part referencing the people.csv file and a text part specifying some sample metadata JSON.

Advanced Rest Client testing

Everything works fine and I get a 200 status back with the people.csv file contents being written to the console output by the service method:

uploading file people.csv of 256 bytes
{Address=1, City=2, Date of Birth=6, Name=0, Phone Number=5, State=3, Zipcode=4}
Person{name='John Brown', address='123 Main St.', city='Scottsdale', state='AZ', zipcode='85259', phoneNumber='555-1212', dateOfBirth='1965-01-01'}
Person{name='Jan Black', address='456 University Dr.', city='Atlanta', state='GA', zipcode='30306', phoneNumber='800-1111', dateOfBirth='1971-02-02'}
Person{name='Mary White', address='789 Possum Rd.', city='Nashville', state='TN', zipcode='37204', phoneNumber='888-2222', dateOfBirth='1980-03-03'}

Now, I want to run this as an automated Karate test. I have specified a MockConfig :

@Configuration
@EnableAutoConfiguration
@PropertySource("classpath:application.properties")
public class MockConfig {

    // Services ...
    @Bean
    public PersonCSVReaderService personCSVReaderService() {
        return new PersonCSVReaderService();
    }

    // Controllers ...
    @Bean
    public FileUploadController fileUploadController() {
        return new FileUploadController();
    }
}

I also have a MockSpringMvcServlet in the classpath and my karate-config.js is :

function fn() {    
   var env = karate.env; // get system property 'karate.env'
   if (!env) {
      env = 'dev';
   }
   karate.log('karate.env system property was:', env);

   var config = {
     env: env,
     myVarName: 'someValue',
     baseUrl: 'http://localhost:8080'
   }

   if (env == 'dev') {
      var Factory = Java.type('MockSpringMvcServlet');
      karate.configure('httpClientInstance', Factory.getMock());
      //var result = karate.callSingle('classpath:demo/headers/common-noheaders.feature', config);
   } else if (env == 'stg') {
      // customize
   } else if (env == 'prod') {
      // customize
   }

   return config;
}

Other karate tests run ok using the mock servlet.

However, when I try this test to test the /document endpoint:

Feature: file upload end-point

  Background:
    * url baseUrl
    * configure lowerCaseResponseHeaders = true

  Scenario: upload file
    Given path '/document'
    And header Content-Type = 'multipart/form-data'
    And multipart file file =  { read: 'people.csv', filename: 'people.csv', contentType: 'text/csv' }
    And multipart field metadata = { name: "joe", description: "stuff" }
    When method post
    Then status 200

I get this error:

16:14:42.674 [main] INFO  com.intuit.karate - karate.env system property was: dev 
16:14:42.718 [main] INFO  o.s.mock.web.MockServletContext - Initializing Spring DispatcherServlet ''
16:14:42.719 [main] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet ''
16:14:43.668 [main] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$a4c7d08f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
16:14:43.910 [main] INFO  o.h.validator.internal.util.Version - HV000001: Hibernate Validator 6.0.14.Final
16:14:44.483 [main] INFO  o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
16:14:44.968 [main] INFO  o.s.b.a.e.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator'
16:14:45.006 [main] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 2287 ms
16:14:45.066 [main] INFO  c.i.k.mock.servlet.MockHttpClient - making mock http client request: POST - http://localhost:8080/document
16:14:45.085 [main] DEBUG c.i.k.mock.servlet.MockHttpClient - 
1 > POST http://localhost:8080/document
1 > Content-Type: multipart/form-data

16:14:45.095 [main] ERROR com.intuit.karate - http request failed: null
file-upload.feature:13 - null
HTML report: (paste into browser to view) | Karate version: 0.9.2

I can only assume that the arguments did not conform to what my endpoint was expecting - I never entered the endpoint in debug mode.

I tried this:

    And multipart file file =  read('people.csv')
    And multipart field metadata = { name: "joe", description: "stuff" }

But that was a non-starter as well.

What am I doing wrong? The people.csv is in the same folder as fileupload.feature, so I am assuming it is finding the file. I also looked at upload.feature file in the Karate demo project given here:

Karate demo project upload.feature

But I could not make it work. Any help appreciated. Thanks in advance.

The Postman request looks like this: enter image description here

EDIT UPDATE:

I could not get the suggested answer to work.

Here is the feature file:

Feature: file upload

  Background:
    * url baseUrl
    * configure lowerCaseResponseHeaders = true

  Scenario: upload file
    Given path '/document'
    And header Content-Type = 'multipart/form-data'
    * def temp = karate.readAsString('people.csv')
    * print temp
    And multipart file file =  { value: '#(temp)', filename: 'people.csv', contentType: 'text/csv' }
    And multipart field metadata = { value: {name: 'joe', description: 'stuff'}, contentType: 'application/json' }
    When method post
    Then status 200

And here is the console output from running that test:

09:27:22.051 [main] INFO  com.intuit.karate - found scenario at line: 7 - ^upload file$
09:27:22.156 [main] INFO  com.intuit.karate - karate.env system property was: dev 
09:27:22.190 [main] INFO  o.s.mock.web.MockServletContext - Initializing Spring DispatcherServlet ''
09:27:22.190 [main] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet ''
09:27:23.084 [main] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$a4c7d08f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
09:27:23.327 [main] INFO  o.h.validator.internal.util.Version - HV000001: Hibernate Validator 6.0.14.Final
09:27:23.896 [main] INFO  o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
09:27:24.381 [main] INFO  o.s.b.a.e.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator'
09:27:24.418 [main] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 2228 ms
09:27:24.435 [main] INFO  com.intuit.karate - [print] Name,Address,City,State,Zipcode,Phone Number,Date of Birth
John Brown,123 Main St.,Scottsdale,AZ,85259,555-1212,1965-01-01
Jan Black,456 University Dr.,Atlanta,GA,30306,800-1111,1971-02-02
Mary White,789 Possum Rd.,Nashville,TN,37204,888-2222,1980-03-03

09:27:24.482 [main] INFO  c.i.k.mock.servlet.MockHttpClient - making mock http client request: POST - http://localhost:8080/document
09:27:24.500 [main] DEBUG c.i.k.mock.servlet.MockHttpClient - 
1 > POST http://localhost:8080/document
1 > Content-Type: multipart/form-data

09:27:24.510 [main] ERROR com.intuit.karate - http request failed: null
file-upload.feature:14 - null
HTML report: (paste into browser to view) | Karate version: 0.9.2

Note: people.csv file reads successfully and prints in console.

joe
  • 220
  • 2
  • 11

2 Answers2

1

Refer to this part of the docs: https://github.com/intuit/karate#read-file-as-string

So make this change:

* def temp = karate.readAsString('people.csv')
And multipart file file =  { value: '#(temp)', filename: 'people.csv', contentType: 'text/csv' }

EDIT: my bad, also refer: https://github.com/intuit/karate#multipart-file

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • The above results in an error as it tries to use the entire contents of the people.csv file as the filename. – joe Apr 15 '19 at 21:23
  • @joe apologies, my bad - see my edit. do read the docs - it explains all these things – Peter Thomas Apr 16 '19 at 00:36
  • Yes, I read the docs beforehand. I tried a bunch of variations not being able to get any of them to work I resorted to stack overflow for help. In either ARC or Postman I can get the request to work properly by specifying a file part as my file people.csv and a text part by setting content-type and providing the JSON as metadata as stated above. My apologies to you for not grasping how to make this work. – joe Apr 16 '19 at 13:40
  • Still fails. Maybe something is wrong with my mock setup. – joe Apr 16 '19 at 13:49
  • Peter, I could not get your suggestion to work. I edited the question above with results. – joe Apr 24 '19 at 13:33
  • that is a surprise, I have no choice now but to point you to this :( https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Apr 24 '19 at 14:01
  • opened a karate issue [karate issue #797](https://github.com/intuit/karate/issues/797) – joe Jun 05 '19 at 19:17
0

Feature: upload csv

Background: And def admin = read('classpath:com/project/data/adminLogin.json')

Scenario: Given url baseUrl

And header Authorization = admin.token

And multipart file residentDetails = { read:'classpath:com/project/data/ResidentApp_Details.csv', filename: 'ResidentApp_Details.csv' }

When method POST

Then status 200

Note: Add only one extra line i.e And multipart file residentDetails = { read:'classpath:com/project/data/ResidentApp_Details.csv', filename: 'ResidentApp_Details.csv' }

  • 1
    I do not understand what you are suggesting. Could you add more explanation? – Edgar H May 17 '19 at 14:25
  • When you want to add any csv or other file in Api request body, that time you add only following line in your code:. And multipart file residentDetails = { read:'classpath:com/project/data/ResidentApp_Details.csv', filename: 'ResidentApp_Details.csv' } – Pratiksha Mokal May 18 '19 at 15:07