1

In API Controller, it fetches HttpContextRequest header to get access token. When I using UnitTest how I will pass access_token value to controller action. Please help.

var _access_token = HttpContext.Current.Request.Headers["access_token"];
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Manas Kumar
  • 2,411
  • 3
  • 16
  • 23
  • Use the 'Authorization' http header. See this http://stackoverflow.com/questions/16526211/how-should-a-client-pass-a-facebook-access-token-to-the-server – lcryder Mar 21 '17 at 12:49
  • Provide a [mcve] that can be used to reproduce the problem and also the desired behavior. You have not provided enough information for us to be able to help you effectively. Also it looks like your web api code is tightly coupled to `HttpContext` which would make it very difficult to unit test in isolation. – Nkosi Mar 21 '17 at 13:42

1 Answers1

0

You can write like this,

//Arrange
var client = new HttpClient { BaseAddress = new Uri("http://localhost:55442/") 
                            };
client.DefaultRequestHeaders.Add("access_token", "YWtoaWw6YWtoaWw=");
Employee emp = new Employee {
            Id=1,
            name = "Test Name",
            salary = "2000"
        };
//Act
var _response = 
client.PostAsJsonAsync(client.BaseAddress +"/"+ "myController"+ "/"+ 
                       "methodName",emp).Result;

//Assert
Assert.IsTrue(true);

Cheers...

Basanta Matia
  • 1,504
  • 3
  • 15
  • 27