Http Response Message

 Microsoft does not make it easy to mock the HttpResponseMessage .  It is an abstract, but still.


private static HttpResponseMessage BuildFakeResponse()
{
   HttpResponseMessage response = new()
   {
       Content = new StringContent("Test Content ", Encoding.UTF8, "text/csv")
   };
   response.Content.Headers.Add("Content-Disposition", "attachment; filename = yourname.csv");
   return response;
}

Here is one that worked with a model and using Json.

private static HttpResponseMessage BuildFakeResponse()
{
   XXXXX xxxx = new();
   string jsonXXXX = JsonConvert.SerializeObject(xxxx);
   HttpResponseMessage response = new()
   {
       Content = new StringContent(jsonXXXX, Encoding.UTF8, "application/json")
   };
   response.Content.Headers.Add("Content-Disposition", "attachment; filename = yourname.csv");
   return response;
}


Comments

Popular posts from this blog

Upgrading to .NET8 from desktop versions 4.8.X

GHL Chat Bots for Webpage

GHL > Set website so shorter URL address