0

I have created a API tests using RestSharp for test automation

I want to create a test where I Assert that and I get the correct image from a GET request in the response

client = new RestClient(ConfigurationHelper.GetValue("BaseURL"));
client.Authenticator = new NtlmAuthenticator((Domain + User), Password);
request = new RestRequest(EndPoint, Method.GET);
IRestResponse restResponse = await client.ExecuteAsync(request);
var response = restResponse.Content;

However when I set a breakpoint at the last line, the response content is displayed as a string

enter image description here

How can I assert this correctly? Or is there a better way to send the GET request and assert the response as an image

IOF
  • 251
  • 6
  • 18
  • 1
    Instead of ExecuteAsync use DownloadData to get bytes of the file. You can assert equality of the generated hash of the file then. https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.hashalgorithm.computehash?view=net-5.0 – Hamed Jan 03 '21 at 12:06

1 Answers1

1

This is answear How to use restsharp to download file on your question.

But I will modify your test a little bit:

  1. Create request to download image
  2. You get array of image bytes
request = new RestRequest(EndPoint, Method.GET);
byte[] restResponse = client.DownloadData(request);
  1. And check image type Determine file type of an image
  2. Also, you can check length of image - if the image is static in your test.
DarkSideMoon
  • 835
  • 1
  • 11
  • 17