Skip to main content

How to Download Secure Files

Previously, we've gone over how to test file downloads without a browser by leveraging Selenium Webdriver and an HTTP library in tandem. This approach is great for testing general file downloads, but what if the file you want to download is behind authentication?

Advancedfundamentals5 tools

How to Download Secure Files

Intro​

Previously, we've gone over how to test file downloads without a browser by leveraging Selenium Webdriver and an HTTP library in tandem.

This approach is great for testing general file downloads, but what if the file you want to download is behind authentication?

A Solution​

To access secure files with our HTTP library we'll need to pass it the same authentication the browser is using. How we do that depends on how the site is secured:

  • With Basic HTTP Authentication, the browser sends the credentials on every request, so we pass the same credentials to the HTTP library.
  • With a login form, the site keeps an authenticated session in a cookie, so we pull the session cookie out of the browser and pass it into the HTTP library when we perform the request.

Let's continue with some examples.

Example​

NOTE: We are using a secure file download example (behind Basic Auth, with admin as username and password) and a login form example found on the-internet.

There are two tests:

  • Files behind Basic Auth: we load the secure downloads page with the credentials in the URL and grab the links of all the files. Then, for each link, we perform a HEAD request with the HTTP library, passing the same credentials in an Authorization header, and check that the response is successful (200). The files on this page are uploaded by users, so we don't assume which types they are.
  • Page behind a form login: we log in with Selenium and grab the session cookie (rack.session) from it. Then we request the secure area with the HTTP library and the cookie, and check that the response is successful (200). To show that the cookie is what gets us in, we also request it without the cookie and get a redirect (302) to the login page.

Expected Behavior​

  • Browser opens
  • The secure file downloads page loads
  • All download URLs on the page are found
  • The HTTP library performs a HEAD request against each download link with the credentials
  • The response is checked to make sure it is successful
  • Browser logs in through the login form
  • The session cookie is retrieved from the browser
  • The HTTP library requests the secure page with and without the cookie, and the responses are checked
  • Browser closes

Summary​

From here, it's simple enough to add checks for the content types and file sizes you expect.

Hopefully this helps save you some time, enabling you to build a more lean and fast set of download tests.

Happy Testing!

Authors​

Last updated on by Diego Molina
Next tipTake Screenshot on FailureRead next