public static RequestCookieCollection Parse(IList <string> values) { if (values.Count == 0) { return(Empty); } IList <CookieHeaderValue> cookies; if (CookieHeaderValue.TryParseList(values, out cookies)) { if (cookies.Count == 0) { return(Empty); } var collection = new RequestCookieCollection(cookies.Count); var store = collection.Store; for (var i = 0; i < cookies.Count; i++) { var cookie = cookies[i]; var name = Uri.UnescapeDataString(cookie.Name.Value); var value = Uri.UnescapeDataString(cookie.Value.Value); store[name] = value; } return(collection); } return(Empty); }
public void Cookies_GetAndSet() { var request = new DefaultProtoContext().Request; var cookieHeaders = request.Headers["Cookie"]; Assert.Empty(cookieHeaders); var cookies0 = request.Cookies; Assert.Empty(cookies0); Assert.Null(cookies0["key0"]); Assert.False(cookies0.ContainsKey("key0")); var newCookies = new[] { "name0=value0%2C", "%5Ename1=value1" }; request.Headers["Cookie"] = newCookies; cookies0 = RequestCookieCollection.Parse(newCookies); var cookies1 = request.Cookies; Assert.Equal(cookies0, cookies1); Assert.Equal(2, cookies1.Count); Assert.Equal("value0,", cookies1["name0"]); Assert.Equal("value1", cookies1["^name1"]); Assert.Equal(newCookies, request.Headers["Cookie"]); var cookies2 = new RequestCookieCollection(new Dictionary <string, string>() { { "name2", "value2" } }); request.Cookies = cookies2; Assert.Equal(cookies2, request.Cookies); Assert.Equal("value2", request.Cookies["name2"]); cookieHeaders = request.Headers["Cookie"]; Assert.Equal(new[] { "name2=value2" }, cookieHeaders); }