0

I'm struggling with creating contract for multipart/mixed controller method, which looks like this below:

@PostMapping("/posts")
public Json createNew(@RequestPart MultipartFile header,
                      @RequestPart MultipartFile photo,
                      @RequestPart Json info) {
     // logic
    return new Json("ok");
}

Problem is, using Groovy DSL, i can't(or didn't find a method to) specify request part's content-type. Without it, autogenerated test would look like this:

// given:
MockMvcRequestSpecification request = given()
                .header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryBTX23kTw0Z5a5bsF")
                .multiPart("header", "filename1", "content1".getBytes())
                .multiPart("photo", "filename1", "content1".getBytes())
                .multiPart("info", "filename1", "{\r\n  \"status\" : \"new upload\"\r\n}".getBytes());

Which fails, because "info" part would not be deserialized to Json object without content-type specified.

I've tried this code below but it didn't work:

request {
    method 'POST'
    url '/posts'
    multipart(
            [
                    ...
                    ...
                    info  : named(
                            name: value(consumer(regex(nonEmpty())), producer('filename1')),
                            content: value(consumer(regex(nonEmpty())), new ServerDslProperty(file("info.json"),
                                    headers {contentType(applicationJson())})))
            ]
    )

    headers {
        contentType('multipart/form-data; boundary=----WebKitFormBoundaryBTX23kTw0Z5a5bsF')
    }
}

So, my question is: how i can specify a multipart request's part content-type?

Edit As @Marcin said, this is a bug Issue on github

MoOFueL
  • 85
  • 12
  • Have a look https://stackoverflow.com/questions/21800726/using-spring-mvc-test-to-unit-test-multipart-post-request – abdul Mar 26 '18 at 06:47
  • Thanks for suggestion, @abdul, I know how to write integration test for this method. I was asking specifically about how to write contract with sc-contract. – MoOFueL Mar 26 '18 at 07:25
  • Can we move this to https://gitter.im/spring-cloud/spring-cloud-contract ? I wonder if you have setup RestAssured properly, and if that's the case then most likely it's a bug – Marcin Grzejszczak Mar 26 '18 at 07:33
  • @MarcinGrzejszczak, if I manually change autogenerated test and add mimetype specifier(like this: `.multiPart("info", "filename1", "{\r\n \"status\" : \"new upload\"\r\n}))".getBytes(), "application/json");`) - test passes – MoOFueL Mar 26 '18 at 07:41

1 Answers1

1

The problem got fixed with closing this issue https://github.com/spring-cloud/spring-cloud-contract/issues/599 . Just use the latest 2.0.0 version (via Finchley release train).

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32