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.jetty; 015 016import static org.junit.Assert.assertEquals; 017import static org.junit.Assert.assertNotNull; 018 019import java.io.OutputStreamWriter; 020import java.io.PrintWriter; 021import java.net.HttpURLConnection; 022import java.net.URL; 023import java.util.concurrent.TimeUnit; 024 025import org.junit.AfterClass; 026import org.junit.BeforeClass; 027import org.junit.Test; 028 029import ch.qos.logback.access.spi.IAccessEvent; 030import ch.qos.logback.access.spi.Util; 031import ch.qos.logback.access.testUtil.NotifyingListAppender; 032import ch.qos.logback.core.testUtil.RandomUtil; 033 034public class JettyBasicTest { 035 036 static RequestLogImpl REQUEST_LOG_IMPL; 037 static JettyFixtureWithListAndConsoleAppenders JETTY_FIXTURE; 038 039 private static final int TIMEOUT = 5; 040 static int RANDOM_SERVER_PORT = RandomUtil.getRandomServerPort(); 041 042 @BeforeClass 043 static public void startServer() throws Exception { 044 REQUEST_LOG_IMPL = new RequestLogImpl(); 045 JETTY_FIXTURE = new JettyFixtureWithListAndConsoleAppenders(REQUEST_LOG_IMPL, RANDOM_SERVER_PORT); 046 JETTY_FIXTURE.start(); 047 } 048 049 @AfterClass 050 static public void stopServer() throws Exception { 051 if (JETTY_FIXTURE != null) { 052 JETTY_FIXTURE.stop(); 053 } 054 } 055 056 @Test 057 public void getRequest() throws Exception { 058 URL url = new URL("http://localhost:" + RANDOM_SERVER_PORT + "/"); 059 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 060 connection.setDoInput(true); 061 062 String result = Util.readToString(connection.getInputStream()); 063 064 assertEquals("hello world", result); 065 066 NotifyingListAppender listAppender = (NotifyingListAppender) REQUEST_LOG_IMPL.getAppender("list"); 067 listAppender.list.clear(); 068 } 069 070 @Test 071 public void eventGoesToAppenders() throws Exception { 072 URL url = new URL(JETTY_FIXTURE.getUrl()); 073 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 074 connection.setDoInput(true); 075 076 String result = Util.readToString(connection.getInputStream()); 077 078 assertEquals("hello world", result); 079 080 NotifyingListAppender listAppender = (NotifyingListAppender) REQUEST_LOG_IMPL.getAppender("list"); 081 IAccessEvent event = listAppender.list.poll(TIMEOUT, TimeUnit.SECONDS); 082 assertNotNull("No events received", event); 083 084 assertEquals("127.0.0.1", event.getRemoteHost()); 085 assertEquals("localhost", event.getServerName()); 086 listAppender.list.clear(); 087 } 088 089 @Test 090 public void postContentConverter() throws Exception { 091 URL url = new URL(JETTY_FIXTURE.getUrl()); 092 String msg = "test message"; 093 094 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 095 // this line is necessary to make the stream aware of when the message is 096 // over. 097 connection.setFixedLengthStreamingMode(msg.getBytes().length); 098 ((HttpURLConnection) connection).setRequestMethod("POST"); 099 connection.setDoOutput(true); 100 connection.setDoInput(true); 101 connection.setUseCaches(false); 102 connection.setRequestProperty("Content-Type", "text/plain"); 103 104 PrintWriter output = new PrintWriter(new OutputStreamWriter(connection.getOutputStream())); 105 output.print(msg); 106 output.flush(); 107 output.close(); 108 109 // StatusPrinter.print(requestLogImpl.getStatusManager()); 110 111 NotifyingListAppender listAppender = (NotifyingListAppender) REQUEST_LOG_IMPL.getAppender("list"); 112 113 IAccessEvent event = listAppender.list.poll(TIMEOUT, TimeUnit.SECONDS); 114 assertNotNull("No events received", event); 115 116 // we should test the contents of the requests 117 // assertEquals(msg, event.getRequestContent()); 118 } 119}