Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a cookie to DefaultHttpContext

I have a function that expect HttpRequest as parameter:

public string Read(HttpRequest req)
{
  if (req.Headers["X-Requested-With"] == "XMLHttpRequest")
  {
    return req.Headers[ConfigurationManager.AppSettings["ajaxsession"]];
  }

  return req.Cookies[ConfigurationManager.AppSettings["cookiename"]];
}

I wrote a unit test

[Test]
public void ReadSessionToken_BrowserRequest_ExpectSidToken()
{
  var repo = new DefaultHttpContext();
}

My question is, how to add a cookie to repo.Request.Cookies?

like image 387
softshipper Avatar asked Jul 20 '16 07:07

softshipper


1 Answers

I didn't test but in github repo there is a test for set and get cookie:

var newCookies = new[] { "name0=value0%2C", "%5Ename1=value1" };
request.Headers["Cookie"] = newCookies;

Update

Tested and worked.

like image 138
adem caglin Avatar answered Nov 06 '22 22:11

adem caglin