How do you check cookies in MVC.net?

http://stephenwalther.com/blog/archive/2008/07/01/asp-net-mvc-tip-12-faking-the-controller-context.aspx

This post shows how to check your cookie settings and then view it in ViewData. What I should do is see if the correct cookies (values ​​and name) were written. Any answer, blog post or article will be appreciated.

+3
source share
3 answers

Are you looking for something similar? (unverified, just typed it in the answer window)

var cookies = new HttpCookieCollection();
controller.ControllerContext = new FakeControllerContext(controller, cookies);
var result = controller.TestCookie() as ViewResult;
Assert.AreEqual("somevaluethatshouldbethere", cookies["somecookieitem"].Value);

Like in, did you mean that you want to check the spelling of the cookie instead of reading it? Please make your request more clear if possible :)

+7
source

, Fake Response, cookie, , .

+1
function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
-2
source

Source: https://habr.com/ru/post/1696926/


All Articles