View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.jetty;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  
19  import java.io.OutputStreamWriter;
20  import java.io.PrintWriter;
21  import java.net.HttpURLConnection;
22  import java.net.URL;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.junit.AfterClass;
26  import org.junit.BeforeClass;
27  import org.junit.Test;
28  
29  import ch.qos.logback.access.spi.IAccessEvent;
30  import ch.qos.logback.access.spi.Util;
31  import ch.qos.logback.access.testUtil.NotifyingListAppender;
32  import ch.qos.logback.core.testUtil.RandomUtil;
33  
34  public class JettyBasicTest {
35  
36      static RequestLogImpl REQUEST_LOG_IMPL;
37      static JettyFixtureWithListAndConsoleAppenders JETTY_FIXTURE;
38  
39      private static final int TIMEOUT = 5;
40      static int RANDOM_SERVER_PORT = RandomUtil.getRandomServerPort();
41  
42      @BeforeClass
43      static public void startServer() throws Exception {
44          REQUEST_LOG_IMPL = new RequestLogImpl();
45          JETTY_FIXTURE = new JettyFixtureWithListAndConsoleAppenders(REQUEST_LOG_IMPL, RANDOM_SERVER_PORT);
46          JETTY_FIXTURE.start();
47      }
48  
49      @AfterClass
50      static public void stopServer() throws Exception {
51          if (JETTY_FIXTURE != null) {
52              JETTY_FIXTURE.stop();
53          }
54      }
55  
56      @Test
57      public void getRequest() throws Exception {
58          URL url = new URL("http://localhost:" + RANDOM_SERVER_PORT + "/");
59          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
60          connection.setDoInput(true);
61  
62          String result = Util.readToString(connection.getInputStream());
63  
64          assertEquals("hello world", result);
65  
66          NotifyingListAppender listAppender = (NotifyingListAppender) REQUEST_LOG_IMPL.getAppender("list");
67          listAppender.list.clear();
68      }
69  
70      @Test
71      public void eventGoesToAppenders() throws Exception {
72          URL url = new URL(JETTY_FIXTURE.getUrl());
73          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
74          connection.setDoInput(true);
75  
76          String result = Util.readToString(connection.getInputStream());
77  
78          assertEquals("hello world", result);
79  
80          NotifyingListAppender listAppender = (NotifyingListAppender) REQUEST_LOG_IMPL.getAppender("list");
81          IAccessEvent event = listAppender.list.poll(TIMEOUT, TimeUnit.SECONDS);
82          assertNotNull("No events received", event);
83  
84          assertEquals("127.0.0.1", event.getRemoteHost());
85          assertEquals("localhost", event.getServerName());
86          listAppender.list.clear();
87      }
88  
89      @Test
90      public void postContentConverter() throws Exception {
91          URL url = new URL(JETTY_FIXTURE.getUrl());
92          String msg = "test message";
93  
94          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
95          // this line is necessary to make the stream aware of when the message is
96          // over.
97          connection.setFixedLengthStreamingMode(msg.getBytes().length);
98          ((HttpURLConnection) connection).setRequestMethod("POST");
99          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 }