0

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?)

bsky
  • 19,326
  • 49
  • 155
  • 270
  • the actual error message is telling that HttpResponseMessage doesn't have an addHeader message, which you can see by looking at the docs: https://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.118).aspx. However `response.Headers.Add()` should do that part. https://msdn.microsoft.com/en-us/library/hh158900(v=vs.118).aspx . – ADyson Dec 15 '17 at 14:30
  • To clear up a misconception that I think you may be having: Your second argument should be the filename as you want it appear when downloaded to the client. This doesn't need to be the same as the name of the file on disk (indeed, there's no requirement that what you return has to originate from a file on disk at all - content can be retrieved from databases instead, just as one example). – ADyson Dec 15 '17 at 14:31
  • 2
    You really need to decide what it is you're trying to test. We know HttpClient works: Microsoft tests it and it's used millions of times per day. The thing that might break is *your code*. That's what you need to test. So don't ever use an actual HttpClient in your tests. Mock it. – mason Dec 15 '17 at 14:38

0 Answers0