Course – LS (cat=REST)

Get started with Spring and Spring Boot, through the reference Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll use the RestAssured library to send multipart requests to a server. This can be useful for testing multipart controllers in Spring or writing an integration test against an already deployed server.

2. What Is a Multipart Request?

Multipart requests are a type of HTTP POST request. They allow sending various files or data in a single request.

In a multipart request, the data is divided into multiple parts. Each part has a name and starts with its own set of headers that indicates the type of data it contains. The data and boundaries between each part are encoded.

3. Setup

Let’s setup the libraries we’ll use.

3.1. The RestAssured Test Library

RestAssured is a Java-based library that provides a domain-specific language for writing automated tests for RESTful web services.

Let’s start by adding the RestAssured library to our pom.xml:

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.3.0</version>
    <scope>test</scope>
</dependency>

3.2. Setup a Wiremock Server

We’ll send multipart requests via RestAssured. In a real-world situation, those requests would reach the target server we want to test. For the sake of our example, we’ll replace this real server with a mock server and use Wiremock for this purpose.

WireMock is an open-source library for mocking web services. It allows us to create stubs for testing client-server interactions.

Let’s add the Wiremock library to our pom.xml:

<dependency>
    <groupId>org.wiremock</groupId>
    <artifactId>wiremock-standalone</artifactId>
    <version>3.3.1</version>
    <scope>test</scope>
</dependency>

We can now configure a Wiremock server in our JUnit test. We’ll make it so the server starts before and stops after each test method:

private WireMockServer wireMockServer;

@BeforeEach
void startServer() {
    wireMockServer = new WireMockServer();
    wireMockServer.start();
}

@AfterEach
void stopServer() {
    wireMockServer.stop();
}

4. Sending Files With RestAssured

We’ll quickly setup our mock server and then move to write our RestAssured test.

4.1. File Handling

Let’s create a file baeldung.txt located in the test/resources directory. To prepare the sending of the file, let’s write two utility methods:

  • given a file name, getFile() retrieves the corresponding File object
  • getFileContent() reads the file content

Here are our methods:

File getFile(String fileName) throws IOException {
    return new ClassPathResource(fileName).getFile();
}

String getFileContent(String fileName) throws IOException {
    return new String(Files.readAllBytes(Paths.get(getFile(fileName).getPath())));
}

4.2. Stub Server Creation

We’ll send the baeldung.txt file to the URL /upload. We’ll set file as the control name of the file in the multipart request.

Firstly, we’ll create a stub that expects such a request. Additionally, we’ll check that the Content-Type header is multipart/form-data. When a request meets all these conditions, we’ll respond with a 200 response status:

stubFor(post(urlEqualTo("/upload"))
  .withHeader("Content-Type", containing("multipart/form-data"))
  .withRequestBody(containing("file"))
  .withRequestBody(containing(getFileContent("baeldung.txt")))
  .willReturn(aResponse().withStatus(200)));

4.3. RestAssured Test Request

It’s now time to focus on the multipart request we’ll send to the server. With RestAssured, the request specifications follow the given-when-then paradigm.

First, we’ll use the multipart() method to add the multipart. Let’s see its parameters:

  • file, the control name associated with the file in the request
  • the file content

Then, we’ll use the post() method to make an HTTP POST request. Its argument is the /upload target URL. Lastly, we’ll set the expected response status with the statusCode() method:

given()
  .multiPart("file", getFile("/baeldung.txt"))
  .when()
  .post("/upload")
  .then()
  .statusCode(200);

We can now test this request against the previous stub: the status code is correct!

It’s possible to add more files to the test request by repetitively calling the multipart() method. For instance, we can add a new file helloworld.txt and name the corresponding part helloworld in the request:

given() 
  .multiPart("file", getFile("/baeldung.txt"))
  .multiPart("helloworld", getFile("/helloworld.txt"))
  .when() 
  .post("/upload") 
  .then() 
  .statusCode(200);

5. Building Our Own Multipart Specification

Sometimes, we want to provide a more detailed multipart in our request. Let’s see how to do that.

5.1. Stub Server Update

Let’s quickly update our Wiremock stub. This time, we’ll expect the request to contain a multipart called file. The name of the file will be file.txt. The Content-Type header of the request will be text/plain and the body will contain File content.

We’ll use a MultipartValuePatternBuilder to complete our mock server specifications:

MultipartValuePatternBuilder multipartValuePatternBuilder = aMultipart()
  .withName("file")
  .withHeader("Content-Disposition", containing("file.txt"))
  .withBody(equalTo("File content"))
  .withHeader("Content-Type", containing("text/plain"));

stubFor(post(urlEqualTo("/upload"))
  .withMultipartRequestBody(multipartValuePatternBuilder)
  .willReturn(aResponse().withStatus(200)));

5.2. Multipart Specification

Let’s now jump to writing our test request. Thanks to the MultipartSpecification class of the RestAssured library, we can set the control name as long as the name, MIME type, charset, and content of the file. The control name identifies the part on the server side, while the file name is set by the sender.

Thus, we’ll build a simple example where:

  • the part will be called file
  • the name given to the file is file.txt
  • the MIME type of the file is text/plain

As a consequence, Content-Type of the request is text/plain instead of multipart/form-data. Nevertheless, this is still a multipart request since the content is encoded inside a part. Besides, we don’t need to use an existing file on the disk. To showcase that, we’ll generate the content on the fly from a String:

MultiPartSpecification multiPartSpecification = new MultiPartSpecBuilder("File content".getBytes())
  .fileName("file.txt")
  .controlName("file")
  .mimeType("text/plain")
  .build();

We can now update our RestAssured request specification by using the overloaded method multipart() directly with the MultipartSpecification as a parameter:

given()
  .multiPart(multiPartSpecification)
  .when()
  .post("/upload")
  .then()
  .statusCode(200);

We now know how to bring more granularity to our RestAssured multipart requests.

6. Conclusion

In this article, we used RestAssured to send multipart requests to a mock server.

We saw how to send files with just the content and control name, then switched to building more complex parts.

As always, the code is available over on GitHub.

Course – LS (cat=REST)

Get started with Spring and Spring Boot, through the Learn Spring course :

>> CHECK OUT THE COURSE
res – REST (eBook) (cat=REST)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.