Package java.net

Examples of java.net.HttpCookie


     * @tests java.net.HttpCookie#toString()
     *
     * @since 1.6
     */
    public void test_ToString() {
        HttpCookie cookie = new HttpCookie("test", "testValue");
        cookie.setComment("ABCd");
        cookie.setCommentURL("\u63DF");
        cookie.setDomain(".B.com");
        cookie.setDiscard(true);
        cookie.setMaxAge(Integer.MAX_VALUE);
        cookie.setPath("temp/22RuTh");
        cookie.setPortlist("80.562Ab");
        cookie.setSecure(true);
        cookie.setVersion(1);

        assertEquals(
                "test=\"testValue\";$Path=\"temp/22RuTh\";$Domain=\".b.com\";$Port=\"80.562Ab\"",
                cookie.toString());

        cookie.setPath(null);
        assertEquals(
                "test=\"testValue\";$Domain=\".b.com\";$Port=\"80.562Ab\"",
                cookie.toString());
        cookie.setComment(null);
        assertEquals(
                "test=\"testValue\";$Domain=\".b.com\";$Port=\"80.562Ab\"",
                cookie.toString());
        cookie.setPortlist(null);
        assertEquals("test=\"testValue\";$Domain=\".b.com\"", cookie.toString());
        cookie.setDomain(null);
        assertEquals("test=\"testValue\"", cookie.toString());

        cookie.setVersion(0);
        cookie.setPortlist("80,8000");
        assertEquals("test=testValue", cookie.toString());
    }
View Full Code Here


     * @tests java.net.HttpCookie#hashCode()
     *
     * @since 1.6
     */
    public void test_HashCode() {
        HttpCookie cookie = new HttpCookie("nAmW_1", "value_1");
        assertEquals(-1052814577, cookie.hashCode());

        cookie.setDomain("a.b.c.de");
        assertEquals(1222695220, cookie.hashCode());

        cookie.setPath("3kmxiq;1");
        assertEquals(-675006347, cookie.hashCode());
        cookie.setPath("3KmxiQ;1");
        assertEquals(989616181, cookie.hashCode());

        cookie.setValue("Vw0,22_789");
        assertEquals(989616181, cookie.hashCode());
        cookie.setComment("comment");
        assertEquals(989616181, cookie.hashCode());

        cookie.setDomain("");
        assertEquals(-1285893616, cookie.hashCode());
    }
View Full Code Here

     */
    public void test_Parse_locale() {
        Locale.setDefault(Locale.FRENCH);
        List<HttpCookie> list = HttpCookie
                .parse("Set-Cookie:name=test;expires=Thu, 30-Oct-2008 19:14:07 GMT;");
        HttpCookie cookie = list.get(0);
        assertEquals(0, cookie.getMaxAge());
        assertTrue(cookie.hasExpired());

        Locale.setDefault(Locale.GERMAN);
        list = HttpCookie
                .parse("Set-Cookie:name=test;expires=Sun, 30-Oct-2005 19:14:07 GMT;");
        cookie = list.get(0);
        assertEquals(0, cookie.getMaxAge());
        assertTrue(cookie.hasExpired());

        Locale.setDefault(Locale.KOREA);
        list = HttpCookie
                .parse("Set-Cookie:name=test;max-age=1234;expires=Sun, 30-Oct-2005 19:14:07 GMT;");
        cookie = list.get(0);
        assertEquals(0, cookie.getVersion());
        assertEquals(1234, cookie.getMaxAge());
        assertFalse(cookie.hasExpired());

        Locale.setDefault(Locale.TAIWAN);
        list = HttpCookie
                .parse("Set-Cookie:name=test;expires=Sun, 30-Oct-2005 19:14:07 GMT;max-age=-12345;");
        cookie = list.get(0);
        assertEquals(0, cookie.getMaxAge());
        assertTrue(cookie.hasExpired());

        // Locale does not affect version 1 cookie.
        Locale.setDefault(Locale.ITALIAN);
        list = HttpCookie.parse("Set-Cookie2:name=test;max-age=1000");
        cookie = list.get(0);
        assertEquals(1000, cookie.getMaxAge());
        assertFalse(cookie.hasExpired());
    }
View Full Code Here

     *
     * @since 1.6
     */
    public void test_Parse() {
        List<HttpCookie> list = HttpCookie.parse("test=\"null\"");
        HttpCookie cookie = list.get(0);
        // when two '"' presents, the parser ignores it.
        assertEquals("null", cookie.getValue());
        assertNull(cookie.getComment());
        assertNull(cookie.getCommentURL());
        assertFalse(cookie.getDiscard());
        assertNull(cookie.getDomain());
        assertEquals(-1, cookie.getMaxAge());
        assertNull(cookie.getPath());
        assertNull(cookie.getPortlist());
        assertFalse(cookie.getSecure());
        // default version is 0
        assertEquals(0, cookie.getVersion());

        list = HttpCookie.parse("Set-cookie2:name=\"tes,t\"");
        cookie = list.get(0);
        // when two '"' presents, the parser ignores it.
        assertEquals("tes,t", cookie.getValue());

        // If cookie header = Set-Cookie2, version = 1
        list = HttpCookie
                .parse("Set-cookie2:test=null\";;Port=abde,82;Path=/temp;;;Discard;commentURl=http://harmonytest.org;Max-age=-10;");
        cookie = list.get(0);
        assertEquals("null\"", cookie.getValue());
        assertEquals(1, cookie.getVersion());
        assertEquals("/temp", cookie.getPath());
        assertTrue(cookie.getDiscard());
        assertEquals("http://harmonytest.org", cookie.getCommentURL());
        assertEquals(-10l, cookie.getMaxAge());
        assertTrue(cookie.hasExpired());
        assertEquals("abde,82", cookie.getPortlist());
        // Version 0 cookie
        list = HttpCookie
                .parse("Set-Cookie:name=tes,t;Comment=version1-cookie;Discard=false;commentURL=vers\nion1-cookie-url;Domain=x.y;");
        cookie = list.get(0);
        assertEquals(0, cookie.getVersion());
        assertEquals("tes,t", cookie.getValue());
        assertEquals("name", cookie.getName());
        assertEquals("version1-cookie", cookie.getComment());
        assertEquals("vers\nion1-cookie-url", cookie.getCommentURL());
        assertEquals("x.y", cookie.getDomain());
        assertTrue(cookie.getDiscard());

        // Check value
        checkValidValue("Set-Cookie:", "val,ue");
        checkValidValue("Set-Cookie:", "val\nue");
        checkValidValue("Set-Cookie:", "value=value");
        checkValidValue("Set-Cookie2:", "val\nue");
        checkValidValue("Set-Cookie2:", "val\u2029ue");
        checkValidValue("Set-Cookie2:", "value=value");

        // Check comment
        // In RFC 2965 '=' is mandatory, but this is not the case in RI.
        list = HttpCookie.parse("Set-Cookie:name=tes,t;Comment;");
        cookie = list.get(0);
        assertNull(cookie.getComment());

        list = HttpCookie
                .parse("Set-Cookie:name=tes,t;Comment=sentence;Comment=anotherSentence");
        cookie = list.get(0);
        assertEquals("sentence", cookie.getComment());

        // Check CommentURL
        list = HttpCookie
                .parse("Set-Cookie:name=tes,t;Commenturl;commentuRL=(la,la)");
        cookie = list.get(0);
        assertEquals("(la,la)", cookie.getCommentURL());

        // Check Domain
        list = HttpCookie.parse("Set-Cookie:name=test;Domain=a_domain");
        cookie = list.get(0);
        assertEquals("a_domain", cookie.getDomain());

        // Check Path
        list = HttpCookie.parse("Set-Cookie:name=test;PaTh=pa$th");
        cookie = list.get(0);
        assertEquals("pa$th", cookie.getPath());

        // Check Max-Age
        list = HttpCookie.parse("Set-Cookie:name=test;Max-Age=1000");
        cookie = list.get(0);
        assertEquals(1000, cookie.getMaxAge());

        list = HttpCookie.parse("Set-Cookie:name=test;Max-Age=-1000");
        cookie = list.get(0);
        assertEquals(-1000, cookie.getMaxAge());

        // Check portlist
        list = HttpCookie.parse("Set-Cookie:name=tes,t;port");
        cookie = list.get(0);
        assertEquals(null, cookie.getPortlist());

        list = HttpCookie.parse("Set-Cookie:name=tes,t;port=");
        cookie = list.get(0);
        assertEquals("", cookie.getPortlist());

        list = HttpCookie.parse("Set-Cookie:name=tes,t;port=123 345");
        cookie = list.get(0);
        assertEquals("123 345", cookie.getPortlist());

        list = HttpCookie.parse("Set-Cookie:name=tes,t;port=123,345");
        cookie = list.get(0);
        assertEquals("123,345", cookie.getPortlist());

        // Check Secure
        list = HttpCookie.parse("Set-Cookie:name=test;secure");
        cookie = list.get(0);
        assertTrue(cookie.getSecure());

        list = HttpCookie.parse("Set-Cookie:name=test;secure=fa");
        cookie = list.get(0);
        assertTrue(cookie.getSecure());
        assertFalse(cookie.hasExpired());

        list = HttpCookie.parse("Set-Cookie2:name=test;secure=false");
        cookie = list.get(0);
        assertTrue(cookie.getSecure());

        // Check expire
        list = HttpCookie.parse("Set-Cookie:name=test;expires=2006-10-23");
        cookie = list.get(0);
        assertEquals(0, cookie.getMaxAge());
        assertTrue(cookie.hasExpired());

        // Also recognize invalid date
        list = HttpCookie
                .parse("Set-Cookie:name=test;expires=Sun, 29-Feb-1999 19:14:07 GMT");
        cookie = list.get(0);
        assertTrue(cookie.getMaxAge() < 0);
        assertTrue(cookie.hasExpired());

        // Parse multiple cookies
        list = HttpCookie
                .parse("Set-Cookie2:name=test;,Set-Cookie2:name2=test2;comment=c234;");
        cookie = list.get(0);
        assertEquals("name", cookie.getName());
        assertEquals(1, cookie.getVersion());
        assertEquals("test", cookie.getValue());
        cookie = list.get(1);
        assertEquals(1, cookie.getVersion());
        // From the second cookie, the "set-cookie2" header does not take effect
        assertEquals("Set-Cookie2:name2", cookie.getName());
        assertEquals("c234", cookie.getComment());

        list = HttpCookie.parse("Set-Cookie2:name=test,name2=test2");
        assertEquals(1, list.get(0).getVersion());
        assertEquals(1, list.get(1).getVersion());

        // Must begin with "set-cookie2" header
        list = HttpCookie.parse("name=test,Set-Cookie2:name2=test2");
        cookie = list.get(0);
        assertEquals(1, list.size());

        HttpCookie c = HttpCookie.parse(
        "Set-cookie:NAME2=VALUE2;path=/t;domain=.b.c;version=1").get(0);
        assertEquals(1, c.getVersion());
       
        c = HttpCookie.parse(
        "Set-cookie2:NAME2=VALUE2;path=/t;domain=.b.c;version=0")
        .get(0);
        assertEquals(1, c.getVersion());
       
        list = HttpCookie.parse("Set-cookie:null=;Domain=null;Port=null");
        cookie = list.get(0);

        assertNotNull(cookie.getValue());
View Full Code Here

    public void test_Parse_versionConflict() {
        // If attribute expires presents, cookie will be recognized as version
        // 0. No matter header is Set-cookie or Set-cookie2
        List<HttpCookie> list = HttpCookie
                .parse("Set-Cookie2:name=;expires=;discard");
        HttpCookie cookie = list.get(0);
        assertEquals(0, cookie.getVersion());
        assertTrue(cookie.getDiscard());

        list = HttpCookie.parse("Set-Cookie: name=value;port=80");
        cookie = list.get(0);
        assertEquals(0, cookie.getVersion());
        assertEquals("80", cookie.getPortlist());

        // In Set-Cookie header, max-age does not take effect when expires
        // exists.
        list = HttpCookie
                .parse("Set-Cookie:name=test;expires=Tue, 27-Jan-1998 19:14:07 GMT;Max-Age=1000");
        cookie = list.get(0);
        assertTrue(cookie.getMaxAge() < 0);
        assertTrue(cookie.hasExpired());
        assertFalse(cookie.getDiscard());
        // Reverse sequence. max-age takes effect and decides the result of
        // hasExpired() method.
        list = HttpCookie
                .parse("Set-Cookie:name=value;max-age=1000;expires=Tue, 17-Jan-1998 19:14:07 GMT;version=1");
        cookie = list.get(0);
        assertEquals(0, cookie.getVersion());
        assertEquals(1000, cookie.getMaxAge());
        assertFalse(cookie.hasExpired());

        // expires decides the version. Not take Set-cookie header, version into
        // consideration if expires exists.
        list = HttpCookie
                .parse("Set-Cookie2:name=value;max-age=1000;version=1;expires=Tue, 17-Jan-1998 19:07:14 GMT;");
        cookie = list.get(0);
        assertEquals(0, cookie.getVersion());
        assertEquals(1000, cookie.getMaxAge());
        assertFalse(cookie.hasExpired());

        // expires does not cover other version 1 attributes.
        list = HttpCookie
                .parse("Set-Cookie2: name=value;expires=Sun, 27-Jan-2018 19:14:07 GMT;comment=mycomment;port=80,8080");
        cookie = list.get(0);
        assertEquals(0, cookie.getVersion());
        assertEquals("80,8080", cookie.getPortlist());
        assertEquals("mycomment", cookie.getComment());

        // When expires does not exist, version takes effect.
        list = HttpCookie.parse("Set-Cookie:name=test;Version=1");
        cookie = list.get(0);
        assertEquals(1, cookie.getVersion());
        assertEquals(-1, cookie.getMaxAge());
        list = HttpCookie.parse("Set-Cookie:name=test;vERsion=0;Version=1;versioN=0;vErsIon=1");
        cookie = list.get(0);
        assertEquals(1, cookie.getVersion());
       
        // When expires does not exist, max-age takes effect.
        list = HttpCookie.parse("Set-Cookie:name=test;Max-Age=11");
        cookie = list.get(0);
        assertEquals(1, cookie.getVersion());
        assertEquals(11, cookie.getMaxAge());
        // other version 1 attributes does not take effect
        list = HttpCookie
                .parse("Set-Cookie:name=test;comment=mycomment;commentURL=url;discard;domain=a.b.com;path=temp;port=79;secure");
        cookie = list.get(0);
        assertEquals(0, cookie.getVersion());
    }
View Full Code Here

    }
   
    private void checkValidValue(String header, String value) {
        List<HttpCookie> list = HttpCookie
                .parse(header + "name=" + value + ";");
        HttpCookie cookie = list.get(0);
        assertEquals(value, cookie.getValue());
    }
View Full Code Here

        public void run() {
            try {
                for (int i = 0; i < 200; i++) {
                    List<HttpCookie> list = HttpCookie.parse("Set-cookie:PREF=test;path=/;domain=.b.c;");
                    assertEquals(1, list.size());
                    HttpCookie cookie = list.get(0);
                    assertEquals(0, cookie.getVersion());
                    assertEquals(".b.c", cookie.getDomain());
                }
            } catch (AssertionError e) {
                error = e;
            }
        }
View Full Code Here

      String expectedName, String expectedValue) {
    Map<String, List<String>> headerFields = proxyConn.getHeaderFields();
    List<String> cookiesHeader = headerFields.get("Set-Cookie");
    if (cookiesHeader != null) {
      for (String cookie : cookiesHeader) {
        HttpCookie c = HttpCookie.parse(cookie).get(0);
        if (c.getName().equals(expectedName)
            && c.getValue().equals(expectedValue)) {
          return true;
        }
      }
    }
    return false;
View Full Code Here

            MultiValueMap<String, String> cookies = jp_2col_req_cookies.getData();
            for(final String key: cookies.keySet()) {
                Collection<String> values = cookies.get(key);
                for(final String value: values) {
                    try {
                        HttpCookie cookie = new HttpCookie(key, value);
                        cookie.setVersion(jp_req_etc.getCookieVersion().getIntValue());
                        request.addCookie(cookie);
                    }
                    catch(IllegalArgumentException ex) {
                        throw new IllegalStateException(ex);
                    }
View Full Code Here

        });
    }

    public void executeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment() throws InterruptedException, ExecutionException {
        /* fetch user object with http cookies */
        UserAccount user = new GetUserAccountCommand(new HttpCookie("mockKey", "mockValueFromHttpRequest")).execute();

        /* fetch the payment information (asynchronously) for the user so the credit card payment can proceed */
        Future<PaymentInformation> paymentInformation = new GetPaymentInformationCommand(user).queue();

        /* fetch the order we're processing for the user */
 
View Full Code Here

TOP

Related Classes of java.net.HttpCookie

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.