</>

Technology

API Automation

Difficulty

Advanced

Interview Question

You are testing a file-sharing app where uploaded files are occasionally corrupted. How would you investigate this?

Answer

Scenario: Investigating File Upload Corruption

Users report that uploaded files are occasionally corrupted. Here''s how to investigate using API testing.

Approach

1. Reproduce the Issue

Java
@Test
public void testFileUploadSmallFile() throws IOException {
    File smallFile = new File("src/test/resources/test_small.pdf");

    Response response = given()
                            .header("Authorization", "Bearer " + token)
                            .multiPart("file", smallFile, "application/pdf")
                            .when()
                            .post("/api/files/upload");

    assertEquals(response.getStatusCode(), 200);
    assertNotNull(response.jsonPath().getString("file_id"));
}

@Test
public void testFileUploadLargeFile() throws IOException {
    File largeFile = new File("src/test/resources/test_large_50mb.zip");

    Response response = given()
                            .header("Authorization", "Bearer " + token)
                            .multiPart("file", largeFile, "application/zip")
                            .when()
                            .post("/api/files/upload");

    assertEquals(response.getStatusCode(), 200);
}

2. Implement Checksum Verification

Java
@Test
public void testFileIntegrityWithChecksum() throws IOException, NoSuchAlgorithmException {
    File uploadFile = new File("src/test/resources/test_document.pdf");

    // Calculate MD5 before upload
    String originalMD5 = calculateMD5(uploadFile);

    // Upload the file
    Response uploadResponse = given()
                                .header("Authorization", "Bearer " + token)
                                .multiPart("file", uploadFile)
                                .when()
                                .post("/api/files/upload");

    String fileId = uploadResponse.jsonPath().getString("file_id");
    String serverChecksum = uploadResponse.jsonPath().getString("checksum");

    // Compare checksums
    assertEquals(originalMD5, serverChecksum,
        "File checksum mismatch — file may be corrupted during upload!");

    // Download and verify again
    byte[] downloadedFile = given()
                                .header("Authorization", "Bearer " + token)
                                .when()
                                .get("/api/files/" + fileId + "/download")
                                .asByteArray();

    String downloadedMD5 = calculateMD5ByteArray(downloadedFile);
    assertEquals(originalMD5, downloadedMD5, "Downloaded file is corrupted!");
}

private String calculateMD5(File file) throws IOException, NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] bytes = Files.readAllBytes(file.toPath());
    byte[] hash = md.digest(bytes);
    return DatatypeConverter.printHexBinary(hash).toLowerCase();
}

3. Test Different File Types

Java
@DataProvider(name = "fileTypes")
public Object[][] fileTypes() {
    return new Object[][] {
        {"test.pdf", "application/pdf"},
        {"test.jpg", "image/jpeg"},
        {"test.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
        {"test.zip", "application/zip"},
        {"test.csv", "text/csv"}
    };
}

@Test(dataProvider = "fileTypes")
public void testUploadByFileType(String fileName, String mimeType) {
    File file = new File("src/test/resources/" + fileName);

    given()
        .header("Authorization", "Bearer " + token)
        .multiPart("file", file, mimeType)
        .when()
        .post("/api/files/upload")
        .then()
        .statusCode(200)
        .body("status", equalTo("success"));
}

4. Test Network Condition Edge Cases

Java
@Test
public void testUploadWithSlowConnection() {
    // Simulate slow network — use connection throttling in test environment
    // Or test with a proxy like Charles set to throttle bandwidth

    File largeFile = new File("src/test/resources/large_file.pdf");

    Response response = given()
                            .config(RestAssured.config()
                                .httpClient(HttpClientConfig.httpClientConfig()
                                    .setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)
                                    .setParam(CoreConnectionPNames.SO_TIMEOUT, 60000)))
                            .multiPart("file", largeFile)
                            .when()
                            .post("/api/files/upload");

    // Should succeed even on slow connection
    assertEquals(response.getStatusCode(), 200);
}

5. Test Upload Interruption / Resume

Java
@Test
public void testUploadResumable() {
    // Test that partial uploads are handled gracefully
    given()
        .header("Authorization", "Bearer " + token)
        .header("Content-Range", "bytes 0-1023/10240")
        .when()
        .post("/api/files/upload/resumable")
        .then()
        .statusCode(206); // Partial Content — resumable upload supported
}

Investigation Findings Template

CheckStatusNotes
Small files (<1MB)✅ OKNo corruption
Large files (>10MB)❌ FailCorruption at 10MB+
PDF files✅ OKAll valid
Binary/ZIP files❌ FailEncoding issue suspected
MD5 checksum match❌ FailServer-side checksum differs
Slow network❌ FailTimeout causes partial write

Follow AutomateQA

Related Topics