From e53910155b3eda1026e123bd80d3037b9363eb06 Mon Sep 17 00:00:00 2001 From: umeding Date: Wed, 18 Sep 2013 08:12:42 -0400 Subject: [PATCH] added cookie manager example --- .../restfuse/example/CookieManagerTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 com.eclipsesource.restfuse.example/src/com/eclipsesource/restfuse/example/CookieManagerTest.java diff --git a/com.eclipsesource.restfuse.example/src/com/eclipsesource/restfuse/example/CookieManagerTest.java b/com.eclipsesource.restfuse.example/src/com/eclipsesource/restfuse/example/CookieManagerTest.java new file mode 100644 index 0000000..73f2e37 --- /dev/null +++ b/com.eclipsesource.restfuse.example/src/com/eclipsesource/restfuse/example/CookieManagerTest.java @@ -0,0 +1,74 @@ +/** + * ***************************************************************************** + * Copyright (c) 2013 Meding Software Technik + * + * All rights reserved. This program and the accompanying materials are made + * available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Uwe B. Meding + * **************************************************************************** + */ +package com.eclipsesource.restfuse.example; + +import com.eclipsesource.restfuse.Assert; +import com.eclipsesource.restfuse.Destination; +import com.eclipsesource.restfuse.HttpJUnitRunner; +import com.eclipsesource.restfuse.Method; +import com.eclipsesource.restfuse.annotation.Context; +import com.eclipsesource.restfuse.annotation.HttpTest; +import java.net.CookieHandler; +import java.net.CookieManager; +import java.net.CookiePolicy; +import java.net.CookieStore; +import java.net.HttpCookie; +import java.net.URI; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.runner.RunWith; + +/** + * Run a test to show how we can manage cookies + * + * @author uwe + */ +@RunWith(HttpJUnitRunner.class) +public class CookieManagerTest { + + @Rule + public Destination dest = new Destination(this, "http://www.google.com"); + + @Context + private com.eclipsesource.restfuse.Response response; + + // Cookie manager must be static. + private static CookieManager cookieManager; + + /** + * Setup the cookie manager for this test + */ + @BeforeClass + public static void setupCookieManager() { + cookieManager = new CookieManager(); + cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); + CookieHandler.setDefault(cookieManager); + } + + @HttpTest(method = Method.GET, path = "/") + public void getSessionId() { + Assert.assertOk(response); + dumpCookies(); + } + + private void dumpCookies() { + System.out.println("Cookies:"); + CookieStore cookieJar = cookieManager.getCookieStore(); + for (URI uri : cookieJar.getURIs()) { + System.out.println("uri: " + uri.getHost()); + } + for (HttpCookie cookie : cookieJar.getCookies()) { + System.out.println("cookie: " + cookie); + } + } +}