15820f308631a825a05feddddd0aafb962f272b9bPaul Hawkepackage fi.iki.elonen.integration;
25820f308631a825a05feddddd0aafb962f272b9bPaul Hawke
38b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie/*
48b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * #%L
58b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * NanoHttpd-Core
68b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * %%
78b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * Copyright (C) 2012 - 2015 nanohttpd
88b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * %%
98b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * Redistribution and use in source and binary forms, with or without modification,
108b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * are permitted provided that the following conditions are met:
119e1ec7bff40d70d31953a04dd448665aaf549395ritchie *
128b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * 1. Redistributions of source code must retain the above copyright notice, this
138b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    list of conditions and the following disclaimer.
149e1ec7bff40d70d31953a04dd448665aaf549395ritchie *
158b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * 2. Redistributions in binary form must reproduce the above copyright notice,
168b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    this list of conditions and the following disclaimer in the documentation
178b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    and/or other materials provided with the distribution.
189e1ec7bff40d70d31953a04dd448665aaf549395ritchie *
198b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * 3. Neither the name of the nanohttpd nor the names of its contributors
208b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    may be used to endorse or promote products derived from this software without
218b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    specific prior written permission.
229e1ec7bff40d70d31953a04dd448665aaf549395ritchie *
238b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
248b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
258b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
268b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
278b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
288b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
298b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
308b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
318b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
328b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * OF THE POSSIBILITY OF SUCH DAMAGE.
338b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * #L%
348b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie */
358b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie
365e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport static org.junit.Assert.assertEquals;
375e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport static org.junit.Assert.assertTrue;
385e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie
395e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport java.util.ArrayList;
405e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport java.util.Calendar;
415e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport java.util.List;
425e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie
435820f308631a825a05feddddd0aafb962f272b9bPaul Hawkeimport org.apache.http.client.CookieStore;
445820f308631a825a05feddddd0aafb962f272b9bPaul Hawkeimport org.apache.http.client.ResponseHandler;
455820f308631a825a05feddddd0aafb962f272b9bPaul Hawkeimport org.apache.http.client.methods.HttpGet;
465820f308631a825a05feddddd0aafb962f272b9bPaul Hawkeimport org.apache.http.impl.client.BasicResponseHandler;
475820f308631a825a05feddddd0aafb962f272b9bPaul Hawkeimport org.apache.http.impl.cookie.BasicClientCookie;
485820f308631a825a05feddddd0aafb962f272b9bPaul Hawkeimport org.junit.Test;
495820f308631a825a05feddddd0aafb962f272b9bPaul Hawke
505e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport fi.iki.elonen.NanoHTTPD;
515820f308631a825a05feddddd0aafb962f272b9bPaul Hawke
525820f308631a825a05feddddd0aafb962f272b9bPaul Hawke/**
539058464950a9734da0a7ff2dc47f3081bbb5117critchie * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/2/13 at 10:10 PM
545820f308631a825a05feddddd0aafb962f272b9bPaul Hawke */
555820f308631a825a05feddddd0aafb962f272b9bPaul Hawkepublic class CookieIntegrationTest extends IntegrationTestBase<CookieIntegrationTest.CookieTestServer> {
565820f308631a825a05feddddd0aafb962f272b9bPaul Hawke
575e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    public static class CookieTestServer extends NanoHTTPD {
585820f308631a825a05feddddd0aafb962f272b9bPaul Hawke
595e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        List<Cookie> cookiesReceived = new ArrayList<Cookie>();
605e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie
615e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        List<Cookie> cookiesToSend = new ArrayList<Cookie>();
625e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie
635e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        public CookieTestServer() {
645e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie            super(8192);
655e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        }
665e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie
675e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        @Override
685e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        public Response serve(IHTTPSession session) {
695e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie            CookieHandler cookies = session.getCookies();
705e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie            for (String cookieName : cookies) {
715e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie                this.cookiesReceived.add(new Cookie(cookieName, cookies.read(cookieName)));
725e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie            }
735e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie            for (Cookie c : this.cookiesToSend) {
745e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie                cookies.set(c);
755e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie            }
765e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie            return newFixedLengthResponse("Cookies!");
775e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        }
785e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    }
795e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie
805e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    @Override
815e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    public CookieTestServer createTestServer() {
825e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        return new CookieTestServer();
835820f308631a825a05feddddd0aafb962f272b9bPaul Hawke    }
845820f308631a825a05feddddd0aafb962f272b9bPaul Hawke
855820f308631a825a05feddddd0aafb962f272b9bPaul Hawke    @Test
865820f308631a825a05feddddd0aafb962f272b9bPaul Hawke    public void testCookieSentBackToClient() throws Exception {
875e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        this.testServer.cookiesToSend.add(new NanoHTTPD.Cookie("name", "value", 30));
885820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        HttpGet httpget = new HttpGet("http://localhost:8192/");
895820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        ResponseHandler<String> responseHandler = new BasicResponseHandler();
905e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        this.httpclient.execute(httpget, responseHandler);
915820f308631a825a05feddddd0aafb962f272b9bPaul Hawke
925e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        CookieStore cookies = this.httpclient.getCookieStore();
935820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        assertEquals(1, cookies.getCookies().size());
945820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        assertEquals("name", cookies.getCookies().get(0).getName());
955820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        assertEquals("value", cookies.getCookies().get(0).getValue());
965820f308631a825a05feddddd0aafb962f272b9bPaul Hawke    }
975820f308631a825a05feddddd0aafb962f272b9bPaul Hawke
985820f308631a825a05feddddd0aafb962f272b9bPaul Hawke    @Test
995e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    public void testNoCookies() throws Exception {
1005e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        HttpGet httpget = new HttpGet("http://localhost:8192/");
1015e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        ResponseHandler<String> responseHandler = new BasicResponseHandler();
1025e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        this.httpclient.execute(httpget, responseHandler);
1035e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie
1045e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        CookieStore cookies = this.httpclient.getCookieStore();
1055e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        assertEquals(0, cookies.getCookies().size());
1065e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    }
1075e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie
1085e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    @Test
1095820f308631a825a05feddddd0aafb962f272b9bPaul Hawke    public void testServerReceivesCookiesSentFromClient() throws Exception {
1105820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        BasicClientCookie clientCookie = new BasicClientCookie("name", "value");
1115820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        Calendar calendar = Calendar.getInstance();
1125820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        calendar.add(Calendar.DAY_OF_YEAR, 100);
1135820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        clientCookie.setExpiryDate(calendar.getTime());
1145820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        clientCookie.setDomain("localhost");
1155e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        this.httpclient.getCookieStore().addCookie(clientCookie);
1165820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        HttpGet httpget = new HttpGet("http://localhost:8192/");
1175820f308631a825a05feddddd0aafb962f272b9bPaul Hawke        ResponseHandler<String> responseHandler = new BasicResponseHandler();
1185e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        this.httpclient.execute(httpget, responseHandler);
1195820f308631a825a05feddddd0aafb962f272b9bPaul Hawke
1205e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        assertEquals(1, this.testServer.cookiesReceived.size());
1215e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        assertTrue(this.testServer.cookiesReceived.get(0).getHTTPHeader().contains("name=value"));
1225820f308631a825a05feddddd0aafb962f272b9bPaul Hawke    }
1235820f308631a825a05feddddd0aafb962f272b9bPaul Hawke}
124