/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/ package org.apache.tomcat.util.http;
import java.nio.charset.StandardCharsets;
import jakarta.servlet.http.Cookie;
import org.junit.Assert; import org.junit.Test;
import org.apache.tomcat.util.buf.MessageBytes;
publicclass TestCookies { privatefinal Cookie FOO = new Cookie("foo", "bar"); privatefinal Cookie FOO_EMPTY = new Cookie("foo", ""); privatefinal Cookie FOO_CONTROL = new Cookie("foo", "b\u00e1r"); privatefinal Cookie BAR = new Cookie("bar", "rab"); privatefinal Cookie BAR_EMPTY = new Cookie("bar", ""); privatefinal Cookie A = new Cookie("a", "b"); privatefinal Cookie HASH_EMPTY = new Cookie("#", ""); privatefinal Cookie $PORT = new Cookie("$Port", "8080"); // RFC 2109 attributes are generally interpreted as additional cookies by // RFC 6265 privatefinal Cookie $VERSION_0 = new Cookie("$Version", "0"); privatefinal Cookie $VERSION_1 = new Cookie("$Version", "1"); privatefinal Cookie $DOMAIN_ASF = new Cookie("$Domain", "apache.org"); privatefinal Cookie $DOMAIN_YAHOO = new Cookie("$Domain", "yahoo.com"); privatefinal Cookie $PATH = new Cookie("$Path", "/examples");
@Test publicvoid testNameOnlyAreDroppedRfc6265() { // Name only cookies are not dropped in RFC6265
test("foo=;a=b; ;", FOO_EMPTY, A);
test("foo;a=b; ;", FOO_EMPTY, A);
test("foo;a=b;bar", FOO_EMPTY, A, BAR_EMPTY);
test("foo;a=b;bar;", FOO_EMPTY, A, BAR_EMPTY);
test("foo;a=b;bar ", FOO_EMPTY, A, BAR_EMPTY);
test("foo;a=b;bar ;", FOO_EMPTY, A, BAR_EMPTY);
// Bug 49000
Cookie fred = new Cookie("fred", "1");
Cookie jim = new Cookie("jim", "2");
Cookie bobEmpty = new Cookie("bob", "");
Cookie george = new Cookie("george", "3");
test("fred=1; jim=2; bob", fred, jim, bobEmpty);
test("fred=1; jim=2; bob; george=3", fred, jim, bobEmpty, george);
test("fred=1; jim=2; bob=; george=3", fred, jim, bobEmpty, george);
test("fred=1; jim=2; bob=", fred, jim, bobEmpty);
}
@Test publicvoid v1EscapedDQuoteInValueRfc6265() { // RFC 2109 considers the 2nd cookie to be correctly escaped. // RFC 6265 considers the 2nd cookie to be invalid
test("$Version=1;foo=\"b\\\"ar\";a=b", $VERSION_1, A);
}
@Test publicvoid eightBitControlInV1UnquotedValue() { // Bug 55917 // RFC 6265 considers this to be a valid UTF-8 value
test("$Version=1; foo=b\u0088r", $VERSION_1, new Cookie("foo", "b\u0088r"));
}
@Test publicvoid testSkipSemicolonOrComma() { // RFC 2109 cookies can also use commas to separate cookies // RFC 6265 considers the second cookie invalid and skips it
test("$Version=1;x\tx=yyy,foo=bar;a=b", $VERSION_1, A);
}
@Test publicvoid testBug60788Rfc6265() {
Cookie c1 = new Cookie("userId", "foo");
Cookie c2 = new Cookie("$Path", "/");
Cookie c3 = new Cookie("$Domain", "www.example.org");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.