Currently, I am retrieving a file by making a HTTP call with a HttpClient
.
mRetriever = new MyRetriever(new HttpClient());
result = mRetriever.MyRetriever("https://some.url/myFile.js");
I would like to mock this call. After looking here, I added this to my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace MyTestModule
{
public class FakeResponseHandler : DelegatingHandler
{
private readonly Dictionary<Uri, HttpResponseMessage> _FakeResponses = new Dictionary<Uri, HttpResponseMessage>();
public void AddFakeResponse(Uri uri, HttpResponseMessage responseMessage)
{
_FakeResponses.Add(uri, responseMessage);
}
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (_FakeResponses.ContainsKey(request.RequestUri))
{
return _FakeResponses[request.RequestUri];
}
else
{
return new HttpResponseMessage(HttpStatusCode.NotFound) { RequestMessage = request };
}
}
}
}
However, I'm not sure how to move on from here:
I added this code where myLocalFile
is the file I would like to return as a response.
FakeResponseHandler fakeResponseHandler = new FakeResponseHandler();
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.AddHeader("content-disposition", "attachment;filename=" + "./myLocalFile");
fakeResponseHandler.AddFakeResponse(new Uri("https://some.url/myFile.js"), response);
Mock<HttpClient> mockHttpClient = new Mock<HttpClient>();
HttpClient httpClient = new HttpClient(fakeResponseHandler);
However, I don't know:
1.How to reference a file from the local file system in the code.
2.How to add that file to HttpResponseMessage
.
The current way I am doing it:
response.AddHeader("content-disposition", "attachment;filename=" + "./myLocalFile");
throws this error:
'HttpResponseMessage' does not contain a definition for 'AddHeader' and no extension method 'AddHeader' accepting a first argument of type 'HttpResponseMessage' could be found(are you missing a using directive or an assembly reference?)