mercredi 27 janvier 2016

How to shim HttpWebRequest Headers?

I am trying to Shim the following code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.Headers.Add("Authorization", "Bearer " + authToken.token.access_token);
request.Accept = "application/json";

But running the Unit Test throws an exception in this part: request.Headers.Add() because request.Headers is null. This, in spite of initializing Headers in my test:

ShimHttpWebRequest request = new ShimHttpWebRequest();
ShimWebRequest.CreateString = (urio) => {
   request.Instance.Headers = new WebHeaderCollection {
        {"Authorization", "Bearer abcd1234"}
   };
   //also tried initilizing it like this:
   //request.Instance.Headers = new WebHeaderCollection();
   return request.Instance;
};

But Headers is still null.

What am I missing?

Aucun commentaire:

Enregistrer un commentaire