There could be a service that takes a file as an input and upload it to the server or cloud storage etc. It could be any kind of file like text data, image, etc. Let’s take this simple scenario, you’ve might be using some social media applications like Facebook, Instagram, etc. And, you might upload your images on those applications. Let’s assume there is a service behind it that handles the image upload. Now, Rest Assured has the capability to test that. In this example, we are going to see “How to upload a multi-part file and send it with the HTTP POST request using Rest Assured?”
Rest Assured has provided us this capability through ‘multiPart()’ method which could be used to upload the files.
NOTE:
In order to proceed further to look for the Rest Assured solution, first we need some running service which can be used to test it. Well, nothing to worry, I’ve made it easier for you. I wrote a sample Rest Service using Spring boot which takes a file as an input. You don’t need to waste your time surfing on the internet to look for any online service which you could use to test this Rest Assured feature.
What you just need to do is to download this project from GitHub by clicking on this link and import it in your IDE and just run it.
OR
You can have your own running service that you could use to test this feature.
Simple representation of uploading a file using RESTAssured...!!! Share on XCheck out: How to send GET, POST, PUT, PATCH and DELETE Requests using REST-assured
Here are some details about the service that we are going to test by uploading the file:
- Endpoint: http://localhost:8080/uploadFile
- Request Type: POST
- File to upload: any file (here we are uploading .png)
Objective:
At the above service endpoint URL, we are going to Upload a file and validate its response.
Finally, here is the code to upload a multipart file through the POST request to the above-mentioned REST API Service Endpoint:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response; import static io.restassured.RestAssured.given; import static org.testng.Assert.assertTrue; import java.io.File; public class UploadMultiPartFile { @Test public void uploadFile() { File testUploadFile = new File("C:\\temp\\testfile.png"); //Specify your own location and file RestAssured.baseURI = "http://localhost:8080"; Response response = given() .multiPart(testUploadFile) .when(). post("/uploadFile"); System.out.println(response.getStatusCode()); System.out.println(response.asString()); assertTrue(response.asString().contains("successfully uploaded")); } } |
Let’s try to understand the code:
1. Setting up Base URI
1 | RestAssured.baseURI = "http://localhost:8080"; |
Base URI is the root address of the Resource. And, by this particular line of code, we are specifying to REST assured to use “localhost:8080” as the root URL of the service.
2. Specifying the exact resource to look for and make a multipart file upload with the POST request to that resource
1 2 3 4 | Response response = given() .multiPart(testUploadFile) .when(). post("/uploadFile"); |
Here, using this code, we are looking to upload a test file using the multiPart method with the POST call to the exact resource which is “/uploadFile” in this case. Hence, the complete Service Endpoint would be “http://localhost:8080/uploadFile” to which we are sending a POST request with the multiPart file upload.
3. Response validation
1 2 3 4 5 | System.out.println(response.getStatusCode()); System.out.println(response.asString()); assertTrue(response.asString().contains("successfully uploaded")); |
a. response.getStatusCode() : This line of code would extract the status code from the response.
b. response.asString() : It displays the response in a string format
c. assertTrue(response.asString().contains(“successfully uploaded”)) : This line of code will throw true or false based on the condition if the string ‘successfully uploaded‘ present in the response or not.
Let’s have a look at the Eclipse Console Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [RemoteTestNG] detected TestNG version 6.14.3 200 TestUploadedFile_1561519032724 successfully uploaded. PASSED: uploadFile =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 =============================================== |
That’s it, it’s that simple to upload a file using REST Assured API: ?
Simple representation of uploading a file using RESTAssured...!!! Share on XIf you like this post, please check out my other useful blog posts on Rest Assured:
Other Useful References: