1   package ch.qos.logback.access.jetty;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   
6   import java.io.OutputStreamWriter;
7   import java.io.PrintWriter;
8   import java.net.HttpURLConnection;
9   import java.net.URL;
10  
11  import org.junit.AfterClass;
12  import org.junit.BeforeClass;
13  import org.junit.Test;
14  
15  import ch.qos.logback.access.spi.AccessEvent;
16  import ch.qos.logback.access.spi.Util;
17  import ch.qos.logback.access.testUtil.NotifyingListAppender;
18  import ch.qos.logback.core.testUtil.RandomUtil;
19  
20  public class JettyBasicTest {
21  
22    static RequestLogImpl REQYEST_LOG_IMPL;
23    static JettyFixture JETTY_FIXTURE;
24  
25    static int RANDOM_SERVER_PORT = RandomUtil.getRandomServerPort();
26    
27    @BeforeClass
28    static public void startServer() throws Exception {
29      // System.out.println("*** JettyBasicTest.startServer called");
30      REQYEST_LOG_IMPL = new RequestLogImpl();
31      JETTY_FIXTURE = new JettyFixture(REQYEST_LOG_IMPL, RANDOM_SERVER_PORT);
32      JETTY_FIXTURE.start();
33    }
34  
35    @AfterClass
36    static public void stopServer() throws Exception {
37      // System.out.println("*** JettyBasicTest.stopServer called");
38      if (JETTY_FIXTURE != null) {
39        JETTY_FIXTURE.stop();
40      }
41    }
42  
43    @Test
44    public void getRequest() throws Exception {
45      URL url = new URL("http://localhost:" + RANDOM_SERVER_PORT + "/");
46      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
47      connection.setDoInput(true);
48  
49      String result = Util.readToString(connection.getInputStream());
50  
51      assertEquals("hello world", result);
52  
53      NotifyingListAppender listAppender = (NotifyingListAppender) REQYEST_LOG_IMPL
54          .getAppender("list");
55      listAppender.list.clear();
56    }
57  
58    @Test
59    public void eventGoesToAppenders() throws Exception {
60      URL url = new URL(JETTY_FIXTURE.getUrl());
61      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
62      connection.setDoInput(true);
63  
64      String result = Util.readToString(connection.getInputStream());
65  
66      assertEquals("hello world", result);
67  
68      NotifyingListAppender listAppender = (NotifyingListAppender) REQYEST_LOG_IMPL
69          .getAppender("list");
70      synchronized (listAppender) {
71        listAppender.wait(100);
72      }
73  
74      assertTrue(listAppender.list.size() > 0);
75      AccessEvent event = (AccessEvent) listAppender.list.get(0);
76      assertEquals("127.0.0.1", event.getRemoteHost());
77      assertEquals("localhost", event.getServerName());
78      listAppender.list.clear();
79    }
80  
81    @Test
82    public void postContentConverter() throws Exception {
83      URL url = new URL(JETTY_FIXTURE.getUrl());
84      String msg = "test message";
85  
86      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
87      // this line is necessary to make the stream aware of when the message is
88      // over.
89      connection.setFixedLengthStreamingMode(msg.getBytes().length);
90      ((HttpURLConnection) connection).setRequestMethod("POST");
91      connection.setDoOutput(true);
92      connection.setDoInput(true);
93      connection.setUseCaches(false);
94      connection.setRequestProperty("Content-Type", "text/plain");
95  
96      PrintWriter output = new PrintWriter(new OutputStreamWriter(connection
97          .getOutputStream()));
98      output.print(msg);
99      output.flush();
100     output.close();
101 
102     // StatusPrinter.print(requestLogImpl.getStatusManager());
103 
104     NotifyingListAppender listAppender = (NotifyingListAppender) REQYEST_LOG_IMPL
105         .getAppender("list");
106 
107     synchronized (listAppender) {
108       listAppender.wait(100);
109     }
110 
111     @SuppressWarnings("unused")
112     AccessEvent event = (AccessEvent) listAppender.list.get(0);
113 
114     // we should test the contents of the requests
115     // assertEquals(msg, event.getRequestContent());
116   }
117 }