Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0c4fae7fa72c1f31ea090242c938a4fff70b2afd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// ========================================================================
// Copyright (c) 2009 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at 
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses. 
// ========================================================================
package org.eclipse.jetty.servlet;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.StatisticsHandler;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

public class StatisticsServletTest extends TestCase
{
    Server server;
    LocalConnector connector;
    ServletContextHandler context;
    
    protected void setUp() throws Exception
    {
        super.setUp();

        server = new Server();
        server.setSendServerVersion(false);
        context = new ServletContextHandler();
        context.setContextPath("/");
        ServletHolder holder = new ServletHolder();
        holder.setServlet(new org.eclipse.jetty.servlet.StatisticsServlet());
        holder.setInitParameter("restrictToLocalhost", "false");
        context.addServlet(holder, "/stats");
        
        server.setHandler(context);
        connector = new LocalConnector();
        server.addConnector(connector);
    }

    protected void tearDown() throws Exception
    {
        super.tearDown();

        if (server != null)
        {
            server.stop();
        }
    }
    
    
    public void testNoHandler () throws Exception
    { 
        server.start();

        StringBuffer req1 = new StringBuffer();
        req1.append("GET /stats HTTP/1.1\n");
        req1.append("Host: localhost\n");
        req1.append("\n");

        String response = connector.getResponses(req1.toString());
        assertResponseContains("503", response);    
    }
    
    public void testWithHandler () throws Exception
    {
        StatisticsHandler statsHandler = new StatisticsHandler();
        statsHandler.setHandler(context);
        server.setHandler(statsHandler);
        server.start();
        
        StringBuffer req1 = new StringBuffer();
        req1.append("GET /stats HTTP/1.1\n");
        req1.append("Host: localhost\n");
        req1.append("\n");
        
        String response = connector.getResponses(req1.toString());
        assertResponseContains("Statistics gathering started ", response);   
    }
  

    private void assertResponseContains(String expected, String response)
    {
        int idx = response.indexOf(expected);
        if (idx == (-1))
        {
            // Not found
            StringBuffer err = new StringBuffer();
            err.append("Response does not contain expected string \"").append(expected).append("\"");
            err.append("\n").append(response);

            System.err.println(err);
            throw new AssertionFailedError(err.toString());
        }
    }
}

Back to the top