001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.spi;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertNotNull;
018
019import java.io.ByteArrayInputStream;
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022import java.io.ObjectOutputStream;
023
024import org.junit.Test;
025
026import ch.qos.logback.access.dummy.DummyAccessEventBuilder;
027import ch.qos.logback.access.dummy.DummyRequest;
028import ch.qos.logback.access.dummy.DummyResponse;
029import ch.qos.logback.access.dummy.DummyServerAdapter;
030import ch.qos.logback.access.net.HardenedAccessEventInputStream;
031
032public class AccessEventSerializationTest {
033
034    private Object buildSerializedAccessEvent() throws IOException, ClassNotFoundException {
035        ByteArrayOutputStream baos = new ByteArrayOutputStream();
036        ObjectOutputStream oos = new ObjectOutputStream(baos);
037        IAccessEvent ae = DummyAccessEventBuilder.buildNewAccessEvent();
038        // average time for the next method: 5000 nanos
039        ae.prepareForDeferredProcessing();
040        oos.writeObject(ae);
041        oos.flush();
042
043        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
044        HardenedAccessEventInputStream hardenedOIS = new HardenedAccessEventInputStream(bais);
045
046        Object sae = hardenedOIS.readObject();
047        hardenedOIS.close();
048        return sae;
049    }
050
051    @Test
052    public void testSerialization() throws IOException, ClassNotFoundException {
053        Object o = buildSerializedAccessEvent();
054        assertNotNull(o);
055        IAccessEvent aeBack = (IAccessEvent) o;
056
057        assertEquals(DummyResponse.DUMMY_DEFAULT_HDEADER_MAP, aeBack.getResponseHeaderMap());
058        assertEquals(DummyResponse.DUMMY_DEFAULT_HDEADER_MAP.get("x"), aeBack.getResponseHeader("x"));
059        assertEquals(DummyResponse.DUMMY_DEFAULT_HDEADER_MAP.get("headerName1"), aeBack.getResponseHeader("headerName1"));
060        assertEquals(DummyResponse.DUMMY_DEFAULT_HDEADER_MAP.size(), aeBack.getResponseHeaderNameList().size());
061        assertEquals(DummyResponse.DUMMY_DEFAULT_CONTENT_COUNT, aeBack.getContentLength());
062        assertEquals(DummyResponse.DUMMY_DEFAULT_STATUS, aeBack.getStatusCode());
063
064        assertEquals(DummyRequest.DUMMY_CONTENT_STRING, aeBack.getRequestContent());
065
066        assertEquals(DummyRequest.DUMMY_RESPONSE_CONTENT_STRING, aeBack.getResponseContent());
067
068        assertEquals(DummyRequest.DUMMY_DEFAULT_ATTR_MAP.get("testKey"), aeBack.getAttribute("testKey"));
069    }
070
071    // Web containers may (and will) recycle requests objects. So we must make sure that after
072    // we prepared an event for deferred processing it won't be using data from the original
073    // HttpRequest object which may at that time represent another request
074    @Test
075    public void testAttributesAreNotTakenFromRecycledRequestWhenProcessingDeferred() {
076
077        DummyRequest request = new DummyRequest();
078        DummyResponse response = new DummyResponse();
079        DummyServerAdapter adapter = new DummyServerAdapter(request, response);
080
081        IAccessEvent event = new AccessEvent(request, response, adapter);
082
083        request.setAttribute("testKey", "ORIGINAL");
084
085        event.prepareForDeferredProcessing();
086
087        request.setAttribute("testKey", "NEW");
088
089        // Event should capture the original value
090        assertEquals("ORIGINAL", event.getAttribute("testKey"));
091    }
092}