public void InvalidKeysThrow(string key)
        {
            var headers = new HeaderDictionary();
            var cookies = new ResponseCookies(headers);

            Assert.Throws <ArgumentException>(() => cookies.Append(key, "1"));
        }
        public void EscapesValuesBeforeSettingCookie(string value, string expected)
        {
            var headers = new HeaderDictionary();
            var cookies = new ResponseCookies(headers);

            cookies.Append("key", value);

            var cookieHeaderValues = headers[HeaderNames.SetCookie];

            Assert.Single(cookieHeaderValues);
            Assert.StartsWith(expected, cookieHeaderValues[0]);
        }
        public void AppContextSwitchEscapesKeysAndValuesBeforeSettingCookie(string key, string value, string expected)
        {
            var headers = new HeaderDictionary();
            var cookies = new ResponseCookies(headers);

            cookies._enableCookieNameEncoding = true;

            cookies.Append(key, value);

            var cookieHeaderValues = headers[HeaderNames.SetCookie];

            Assert.Single(cookieHeaderValues);
            Assert.StartsWith(expected, cookieHeaderValues[0]);
        }
        public void EscapesKeyValuesBeforeSettingCookie(
            string key,
            string value,
            string expected)
        {
            var headers = new HeaderDictionary();
            var cookies = new ResponseCookies(headers, null);

            cookies.Append(key, value);

            var cookieHeaderValues = headers[HeaderNames.SetCookie];

            Assert.Equal(1, cookieHeaderValues.Count);
            Assert.StartsWith(expected, cookieHeaderValues[0]);
        }
        public void NoParamsDeleteRemovesCookieCreatedByAdd()
        {
            var headers    = new HeaderDictionary();
            var cookies    = new ResponseCookies(headers, null);
            var testcookie = "TestCookie";

            cookies.Append(testcookie, testcookie);
            cookies.Delete(testcookie);

            var cookieHeaderValues = headers[HeaderNames.SetCookie];

            Assert.Equal(1, cookieHeaderValues.Count);
            Assert.StartsWith(testcookie, cookieHeaderValues[0]);
            Assert.Contains("path=/", cookieHeaderValues[0]);
            Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookieHeaderValues[0]);
        }
        public void ProvidesMaxAgeWithCookieOptionsArgumentExpectMaxAgeToBeSet()
        {
            var headers       = new HeaderDictionary();
            var cookies       = new ResponseCookies(headers, null);
            var cookieOptions = new CookieOptions();
            var maxAgeTime    = TimeSpan.FromHours(1);

            cookieOptions.MaxAge = TimeSpan.FromHours(1);
            var testcookie = "TestCookie";

            cookies.Append(testcookie, testcookie, cookieOptions);

            var cookieHeaderValues = headers[HeaderNames.SetCookie];

            Assert.Equal(1, cookieHeaderValues.Count);
            Assert.Contains($"max-age={maxAgeTime.TotalSeconds.ToString()}", cookieHeaderValues[0]);
        }
Beispiel #7
0
        public void NoParamsDeleteRemovesCookieCreatedByAdd()
        {
            var headers    = (IHeaderDictionary) new HeaderDictionary();
            var features   = MakeFeatures(headers);
            var cookies    = new ResponseCookies(features);
            var testCookie = "TestCookie";

            cookies.Append(testCookie, testCookie);
            cookies.Delete(testCookie);

            var cookieHeaderValues = headers.SetCookie;

            Assert.Single(cookieHeaderValues);
            Assert.StartsWith(testCookie, cookieHeaderValues[0]);
            Assert.Contains("path=/", cookieHeaderValues[0]);
            Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookieHeaderValues[0]);
        }
Beispiel #8
0
        public void ProvidesMaxAgeWithCookieOptionsArgumentExpectMaxAgeToBeSet()
        {
            var headers       = (IHeaderDictionary) new HeaderDictionary();
            var features      = MakeFeatures(headers);
            var cookies       = new ResponseCookies(features);
            var cookieOptions = new CookieOptions();
            var maxAgeTime    = TimeSpan.FromHours(1);

            cookieOptions.MaxAge = TimeSpan.FromHours(1);
            var testCookie = "TestCookie";

            cookies.Append(testCookie, testCookie, cookieOptions);

            var cookieHeaderValues = headers.SetCookie;

            Assert.Single(cookieHeaderValues);
            Assert.Contains($"max-age={maxAgeTime.TotalSeconds}", cookieHeaderValues[0]);
        }
Beispiel #9
0
    public void AppendWithExtensions()
    {
        var headers    = (IHeaderDictionary) new HeaderDictionary();
        var features   = MakeFeatures(headers);
        var cookies    = new ResponseCookies(features);
        var testCookie = "TestCookie";

        cookies.Append(testCookie, "value", new CookieOptions()
        {
            Extensions = { "simple", "key=value" }
        });

        var cookieHeaderValues = headers.SetCookie;

        Assert.Single(cookieHeaderValues);
        Assert.StartsWith(testCookie, cookieHeaderValues[0]);
        Assert.Contains("path=/", cookieHeaderValues[0]);
        Assert.Contains("simple;", cookieHeaderValues[0]);
        Assert.EndsWith("key=value", cookieHeaderValues[0]);
    }
Beispiel #10
0
        public void AppendSameSiteNoneWithoutSecureLogsWarning()
        {
            var headers  = (IHeaderDictionary) new HeaderDictionary();
            var features = MakeFeatures(headers);
            var services = new ServiceCollection();

            var sink          = new TestSink(TestSink.EnableWithTypeName <ResponseCookies>);
            var loggerFactory = new TestLoggerFactory(sink, enabled: true);

            services.AddLogging();
            services.AddSingleton <ILoggerFactory>(loggerFactory);

            features.Set <IServiceProvidersFeature>(new ServiceProvidersFeature()
            {
                RequestServices = services.BuildServiceProvider()
            });

            var cookies    = new ResponseCookies(features);
            var testCookie = "TestCookie";

            cookies.Append(testCookie, "value", new CookieOptions()
            {
                SameSite = SameSiteMode.None,
            });

            var cookieHeaderValues = headers.SetCookie;

            Assert.Single(cookieHeaderValues);
            Assert.StartsWith(testCookie, cookieHeaderValues[0]);
            Assert.Contains("path=/", cookieHeaderValues[0]);
            Assert.Contains("samesite=none", cookieHeaderValues[0]);
            Assert.DoesNotContain("secure", cookieHeaderValues[0]);

            var writeContext = Assert.Single(sink.Writes);

            Assert.Equal("The cookie 'TestCookie' has set 'SameSite=None' and must also set 'Secure'.", writeContext.Message);
        }
Beispiel #11
0
        public void TestMethod(string key, string value)
        {
            var responseCookies = new ResponseCookies();

            responseCookies.Append(key, value, GetCookieOptions());
        }