API Testing : Check file handling

Verify that the API correctly handles file uploads and downloads.

Language: Java

Framework: Rest assured

copy
1/​/​Assumptions: 2/​/​1. The API endpoint for file uploads and downloads is /​api/​files3/​/​2. A valid file is available in the local system at '/​home/​user/​testfile.txt'4/​/​3. The API endpoint requires authentication with an access token56import org.junit.jupiter.api.Test;7import static io.restassured.RestAssured.*;8import static org.hamcrest.Matchers.*;910public class FileHandlingAPITest {1112 @Test13 public void testFileUpload() {14 given()15 .header("Authorization", "Bearer <access_token>")16 .multiPart("file", "/​home/​user/​testfile.txt")17 .when()18 .post("/​api/​files")19 .then()20 .statusCode(201)21 .body("filePath", equalTo("/​api/​files/​testfile.txt"));22 }2324 @Test25 public void testFileDownload() {26 given()27 .header("Authorization", "Bearer <access_token>")28 .when()29 .get("/​api/​files/​testfile.txt")30 .then()31 .statusCode(200)32 .header("Content-Disposition", "attachment; filename=testfile.txt")33 .header("Content-Type", "text/​plain")34 .extract().response().asByteArray();3536 /​/​Code for remote client with desired capabilities37 /​/​DesiredCapabilities capabilities = DesiredCapabilities.firefox();38 /​/​RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);39 }40}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assuming the API has an endpoint for file upload and download4describe('File Handling', function (){5 it('should upload and download a file successfully', function (){6 /​/​Assuming file path is valid and file size is within limits7 /​/​Assuming API endpoint is https:/​/​localhost:3000/​api/​upload8 /​/​Assuming file type is jpg9 let filePath = '/​home/​user/​file.jpg'; 10 let endpoint = 'https:/​/​localhost:3000/​api/​upload';11 let remote = require('selenium-webdriver/​remote');12 let chromeOptions = new remote.Options().addArguments('--headless');13 let driver = new webdriver.Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();14 15 /​/​Navigating to the API endpoint16 driver.navigate().to(endpoint);1718 /​/​Finding the file upload input element19 let fileUploadButton = driver.findElement(webdriver.By.css('.file-upload'));20 fileUploadButton.sendKeys(filePath);2122 /​/​Finding and clicking the upload button23 let uploadButton = driver.findElement(webdriver.By.css('.upload-btn'));24 uploadButton.click();2526 /​/​Asserting that the uploaded file is downloaded successfully27 let downloadButton = driver.findElement(webdriver.By.css('.download-btn'));28 downloadButton.click();2930 /​/​Assuming the download file path is valid31 let downloadFilePath = '/​home/​user/​Downloads/​file.jpg';32 let fs = require('fs');33 let downloadedFile = fs.readFileSync(downloadFilePath);3435 /​/​Assuming the API returns the correct file36 let uploadedFile = fs.readFileSync(filePath);3738 /​/​Asserting that the downloaded file is the same as the uploaded file39 assert.equal(downloadedFile.toString(), uploadedFile.toString());4041 /​/​Closing the browser instance42 driver.quit();43 });44});

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing