Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0bd8acfd8933d09b807bdf5cbbfbc061c55216d0 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
//  ========================================================================
//  Copyright (c) 1995-2013 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.client;

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Test;

/* ------------------------------------------------------------ */
public class ErrorStatusTest
    extends ContentExchangeTest
{

    /* ------------------------------------------------------------ */
    @Test
    public void testPutBadRequest()
        throws Exception
    {
        doPutFail(HttpStatus.BAD_REQUEST_400);
    }
    
    /* ------------------------------------------------------------ */
    @Test
    public void testPutUnauthorized()
        throws Exception
    {
        doPutFail(HttpStatus.UNAUTHORIZED_401);
    }
    
    /* ------------------------------------------------------------ */
    @Test
    public void testPutForbidden()
        throws Exception
    {
        doPutFail(HttpStatus.FORBIDDEN_403);
    }
    
    /* ------------------------------------------------------------ */
    @Test
    public void testPutNotFound()
        throws Exception
    {
        doPutFail(HttpStatus.NOT_FOUND_404);    
    }
    
    /* ------------------------------------------------------------ */
    @Test
    public void testPutServerError()
        throws Exception
    {
        doPutFail(HttpStatus.INTERNAL_SERVER_ERROR_500);    
    }
    
    /* ------------------------------------------------------------ */
    @Test
    public void testGetBadRequest()
        throws Exception
    {
        doGetFail(HttpStatus.BAD_REQUEST_400);
    }
    
    /* ------------------------------------------------------------ */
    @Test
    public void testGetUnauthorized()
        throws Exception
    {
        doGetFail(HttpStatus.UNAUTHORIZED_401);
    }
    
    /* ------------------------------------------------------------ */
    @Test
    public void testGetNotFound()
        throws Exception
    {
        doGetFail(HttpStatus.NOT_FOUND_404);    
    }
    
    /* ------------------------------------------------------------ */
    @Test
    public void testGetServerError()
        throws Exception
    {
        doGetFail(HttpStatus.INTERNAL_SERVER_ERROR_500);    
    }
    
    /* ------------------------------------------------------------ */
    @Test
    public void testPostBadRequest() 
        throws Exception
    {
        doPostFail(HttpStatus.BAD_REQUEST_400);
    }

    /* ------------------------------------------------------------ */
    @Test
    public void testPostUnauthorized()
        throws Exception
    {
        doPostFail(HttpStatus.UNAUTHORIZED_401);
    }

    /* ------------------------------------------------------------ */
    @Test
    public void testPostForbidden()
        throws Exception
    {
        doPostFail(HttpStatus.FORBIDDEN_403);
    }

    /* ------------------------------------------------------------ */
    @Test
    public void testPostNotFound()
        throws Exception
    {
        doPostFail(HttpStatus.NOT_FOUND_404);
    }

    /* ------------------------------------------------------------ */
    @Test
    public void testPostServerError()
        throws Exception
    {
        doPostFail(HttpStatus.INTERNAL_SERVER_ERROR_500);
    }

    /* ------------------------------------------------------------ */
    protected void doPutFail(int status)
        throws Exception
    {
        startClient(getRealm());

        ContentExchange putExchange = new ContentExchange();
        putExchange.setURL(getBaseUrl() + "output.txt");
        putExchange.setMethod(HttpMethods.PUT);
        putExchange.setRequestHeader("X-Response-Status",Integer.toString(status));
        putExchange.setRequestContent(new ByteArrayBuffer(getContent().getBytes()));
        
        getClient().send(putExchange);
        int state = putExchange.waitForDone();
                
        int responseStatus = putExchange.getResponseStatus();
        
        stopClient();

        assertEquals(status, responseStatus);            
    }
    
    /* ------------------------------------------------------------ */
    protected void doGetFail(int status)
        throws Exception
    {
        startClient(getRealm());

        ContentExchange getExchange = new ContentExchange();
        getExchange.setURL(getBaseUrl() + "input.txt");
        getExchange.setMethod(HttpMethods.GET);
        getExchange.setRequestHeader("X-Response-Status",Integer.toString(status));
       
        getClient().send(getExchange);
        int state = getExchange.waitForDone();
        
        String content;
        int responseStatus = getExchange.getResponseStatus();
        if (responseStatus == HttpStatus.OK_200) {
            content = getExchange.getResponseContent();
        }
        
        stopClient();

        assertEquals(status, responseStatus);
    }
    
    /* ------------------------------------------------------------ */
    protected void doPostFail(int status)
        throws Exception
    {
        startClient(getRealm());
    
        ContentExchange postExchange = new ContentExchange();
        postExchange.setURL(getBaseUrl() + "test");
        postExchange.setMethod(HttpMethods.POST);
        postExchange.setRequestHeader("X-Response-Status",Integer.toString(status));
        postExchange.setRequestContent(new ByteArrayBuffer(getContent().getBytes()));
        
        getClient().send(postExchange);
        int state = postExchange.waitForDone();
                
        int responseStatus = postExchange.getResponseStatus();
        
        stopClient();
    
        assertEquals(status, responseStatus);            
    }

    /* ------------------------------------------------------------ */
    protected void configureServer(Server server)
        throws Exception
    {
        setProtocol("http");
    
        SelectChannelConnector connector = new SelectChannelConnector();
        server.addConnector(connector);
        
        ServletContextHandler root = new ServletContextHandler();
        root.setContextPath("/");
        root.setResourceBase(getBasePath());
        ServletHolder servletHolder = new ServletHolder( new DefaultServlet() );
        servletHolder.setInitParameter( "gzip", "true" );
        root.addServlet( servletHolder, "/*" );    

        Handler status = new StatusHandler();
        Handler test = new TestHandler(getBasePath());
        
        HandlerCollection handlers = new HandlerCollection();
        handlers.setHandlers(new Handler[]{status, test, root});
        server.setHandler( handlers ); 
    }
    
    /* ------------------------------------------------------------ */
    protected static class StatusHandler extends AbstractHandler {
        /* ------------------------------------------------------------ */
        public void handle(String target, Request baseRequest,
                HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
        {
            if (baseRequest.isHandled())
                return;

            int statusValue = 0;
            String statusHeader = request.getHeader("X-Response-Status");
            if (statusHeader != null)
            {
                statusValue = Integer.parseInt(statusHeader);
            }
            if (statusValue != 0)
            {
                response.setStatus(statusValue);
                baseRequest.setHandled(true);
            }
        }
    }
}

Back to the top