Download File with Rest Assured

In the previous articles, we have seen how to pass the response using the rest assured. But in real-life scenarios, we not only just received a text-based response but also we will receive some files as response.

In this chapter, we are going to learn how to download a file using rest assured, Before we can start our chapter I want you to understand how the file works.

You could use a byte array to store a collection of binary data, for example, the contents of a file. For example when we say the size of a file is 70 KB then we are meaning that there is an array of bytes with the length of 70k.

So when we are downloading a file we have to extract the response as an array of bytes and then we can use it to store it as a file.

In this article, we are going to download a file and we are going to compare it with a base file that we have.

Download the base file

For downloading the base file visit the URL https://chercher.tech/files/ and click the minions.zip; Now you will the file downloading. Store the file in a place so we can use it to compare with the file which gets downloaded by the rest assured.
file-download-rest-assured

I have stored it in the project folder under the files folder.

base-file-rest-assured-file-download

Once you place the downloaded file, then we need to compare the base file with the rest downloaded file.

Copy the file path for the base file, if you have placed the file inside the java project then you can copy the qualified name in eclipse. (Right-click on file > Copy Qualified Name). nce you copy it, please remove the project name from the qualified name, so that path is right.

Qualified name: /FirstRestAssuredExamples/files/minion.zip
After project name removal : /files/minion.zip

qualfied-name-rest-assured

For the moment's sake, I am going to compare the file size of the base file and rest downloaded file.

Get the size of the base file.

public static long getFileSize() {
    File baseFile = new File("./files/minion.zip");
    return baseFile.length();
}

Download file using Rest Assured program

To download the file, we need to know the URL of the file. To copy the URL go to the https://chercher.tech/files/.

Right-click on the target file (here, minions.zip).
copy-link-address-rest-assured-file-download

The copied link address is

https://chercher.tech/files/minion.zip

If you enter the above URL in the browser, the browser automatically starts downloading the file.

So lets write the code to hit the above URL endpoint with our Rest Assured code. extract().asByteArray() will fetch the result in a byte array format. As we have learned earlier, files are nothing but arrays of bytes.

import java.io.File;
import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class FileDownloadExample {
	public static long getFileSize() {
		File baseFile = new File("./files/minion.zip");
		return baseFile.length();
	}
	@Test
	void test() {
		byte[] dowloadedFile = RestAssured.given().when()
								.get("https://chercher.tech/files/minion.zip")
								.then().extract().asByteArray();
		
		System.out.println("Actual File Size : "+getFileSize());
		System.out.println("Download File Size : "+dowloadedFile.length);
		assertEquals(getFileSize(), dowloadedFile.length);
	}
}

Output
output-rest-assured-download-file

Save File from Rest Assured response

In the book program, we have seen how to compare the file sizes; but sometimes we might need to save the response as a file in our local system. Let's see how to save the file into our local system now with the below program.

We can write the bytes arrays to the local system using the below code

FileOutputStream os = new FileOutputStream(new File("Target path to write"));
os.write(Byte array to be written);
os.close();

The complete program to download a file in rest assured

import java.io.File;
import java.io.FileOutputStream;
import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
public class FileDownloadExample {
	
	public static long getFileSize() {
		File baseFile = new File("./files/minion.zip");
		return baseFile.length();
	}
	@Test
	void test() {
		byte[] dowloadedFile = RestAssured.given().when()
								.get("https://chercher.tech/files/minion.zip")
								.then().extract().asByteArray();
		
		System.out.println("Actual File Size : "+getFileSize());
		System.out.println("Download File Size : "+dowloadedFile.length);
		
		try {
			FileOutputStream os = new FileOutputStream(new File("./files/minios-from-rest-assured.zip"));
			os.write(dowloadedFile);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

downloaded-file-rest-assured

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions