简体   繁体   中英

Testing if download is successful with supertest

I'm testing my API endpoints with supertest<\/a> , and it works great, but i can't figure out how to test if a file download is successful.

app.get('/api/attachment/:id/file', attachment.getFile);

Either the problem has been fixed in the libraries, or there is a bug in some other part of your code. Your example runs fine, and gives

  when trying to download file
File found!
    ✓ should respond with "200 OK"

When testing for a download file, it is not enough to validate the response status from the server, it will be even better if you can somehow validate the response data.

For download data, the content of the file are usually passed in the http response as text , with the file type as Content-Type , and the attachment and filename stored in Content-Disposition .

Depending on how detailed you would like to go, you can try the following:

    const response = await request(url)
            .get('/api/attachment/' + attachment._id + '/file');
    expect(response.headers["content-type"]).toEqual("image/png");
    expect(response.text).toMatchSnapshot(); // Use only if the file is deterministic.

Using jest or any other snapshot framework, you can achieve a more reliable test.

This may be coming late, but I'm dropping this here for future reference and to help others who might be facing something similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM