1
public IList<FormResponse> GetForms(HttpRequestMessage request)
{
       string storeCode = ExtractBasicAuthUserFromHeader(request);
       List<Form> forms = _apiRepository.GetForms(storeCode);     ///23424324  
       return forms;
}

 private string ExtractBasicAuthUserFromHeader(HttpRequestMessage reqeust)
 {
    Encoding encoding = Encoding.GetEncoding("iso-8859-1");
    string usernamePassword = encoding.GetString(Convert.FromBase64String(reqeust.Headers.Authorization.Parameter 
     ));

      return usernamePassword.Substring(0, usernamePassword.IndexOf(':'));
  }

I wrote following test to test above

private readonly Mock<IApiRepository> _apiRepository = new Mock<IApiRepository>();


[TestInitialize]
public void Init()
{
    _apiRepository.Setup(x => x.GetForms("23424324")).Returns(_forms); //skipping FromBase64String converation for understanding
}


 [TestMethod]
 public void GetForms_ReturnFormList()
 {
  HttpRequestMessage reqeust = new HttpRequestMessage(); ;

  //Error   CS0200  Property or indexer 'AuthenticationHeaderValue.Parameter' cannot be assigned to -- it is read only  
reqeust.Headers.Authorization.Parameter = "23424324:12341234123"; 

  IList<FormResponse> formList = _formService.GetForms(reqeust);

  Assert.AreEqual(formList.Count, 2);
 }

getting following error

Error CS0200 Property or indexer 'AuthenticationHeaderValue.Parameter' cannot be assigned to -- it is read only

wondering how can I mock HttpRequestMessage reqeust = new HttpRequestMessage(); to add reqeust.Headers.Authorization.Parameter base string to test GetForms functionality in services class

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Developer
  • 25,073
  • 20
  • 81
  • 128
  • 3
    You should set the entire value of `reqeust.Headers.Authorization` as `request.Headers.Authorization = new AuthenticationHeaderValue(somesceme, someparameter");`. – Chetan Sep 17 '18 at 03:49
  • That solve my problem, Did not realize it was that simple :) please add it as answer, – Developer Sep 17 '18 at 04:02

2 Answers2

2

request.Headers.Authorization is of type AuthenticationHeaderValue and this class has properties Scheme and Parameter. These properties are ready-only.

You are getting error because you are trying to assign value to these ready-only properties.

What you need to do is to assign value to request.Headers.Authorization by creating an object of AuthenticationHeaderValue class.

Consider doing following.

request.Headers.Authorization = new AuthenticationHeaderValue(somescheme, someparameter);

You need to put actual values in place of somescheme and someparameter in above code.

You can get details about HttpRequestMessage and AuthenticationHeaderValue classes at below mentioned links.

https://msdn.microsoft.com/en-us/library/system.net.http.headers.authenticationheadervalue(v=vs.118).aspx

https://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessage(v=vs.118).aspx

https://msdn.microsoft.com/en-us/library/system.net.http.headers.httprequestheaders(v=vs.118).aspx

Chetan
  • 6,711
  • 3
  • 22
  • 32
0

You can use something like this:

client.DefaultRequestHeaders.Authorization =
       new AuthenticationHeaderValue("key", "=" + value);

More here: Setting Authorization Header of HttpClient

Gauravsa
  • 6,330
  • 2
  • 21
  • 30