~ubuntu-branches/ubuntu/trusty/tomcat7/trusty-security

« back to all changes in this revision

Viewing changes to test/org/apache/catalina/core/TestAsyncContextImpl.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2012-06-07 22:43:21 UTC
  • mfrom: (11.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20120607224321-cfev8j681yueyov3
Tags: 7.0.27-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
import static org.junit.Assert.assertEquals;
38
38
import static org.junit.Assert.assertNotNull;
 
39
import static org.junit.Assert.assertTrue;
39
40
import static org.junit.Assert.fail;
40
41
 
41
42
import org.junit.Test;
938
939
            if (flush) {
939
940
                resp.flushBuffer();
940
941
            }
 
942
            try {
 
943
                // Give the original thread a chance to exit the
 
944
                // ErrorReportValve before we throw this exception
 
945
                Thread.sleep(500);
 
946
            } catch (InterruptedException e) {
 
947
                throw new ServletException(e);
 
948
            }
941
949
            throw new ServletException("Opps.");
942
950
        }
943
951
    }
1168
1176
    }
1169
1177
 
1170
1178
    @Test
1171
 
    public void testBug51197() throws Exception {
 
1179
    public void testBug51197a() throws Exception {
 
1180
        doTestBug51197(false);
 
1181
    }
 
1182
 
 
1183
    @Test
 
1184
    public void testBug51197b() throws Exception {
 
1185
        doTestBug51197(true);
 
1186
    }
 
1187
 
 
1188
    private void doTestBug51197(boolean threaded) throws Exception {
1172
1189
        // Setup Tomcat instance
1173
1190
        Tomcat tomcat = getTomcatInstance();
1174
1191
 
1178
1195
        Context ctx = tomcat.addContext("", docBase.getAbsolutePath());
1179
1196
 
1180
1197
        AsyncErrorServlet asyncErrorServlet =
1181
 
            new AsyncErrorServlet(HttpServletResponse.SC_BAD_REQUEST);
 
1198
            new AsyncErrorServlet(HttpServletResponse.SC_BAD_REQUEST, threaded);
1182
1199
        Wrapper wrapper =
1183
1200
            Tomcat.addServlet(ctx, "asyncErrorServlet", asyncErrorServlet);
1184
1201
        wrapper.setAsyncSupported(true);
1194
1211
        url.append(getPort());
1195
1212
        url.append("/asyncErrorServlet");
1196
1213
 
1197
 
        int rc = getUrl(url.toString(), new ByteChunk(), null);
 
1214
        ByteChunk res = new ByteChunk();
 
1215
        int rc = getUrl(url.toString(), res, null);
1198
1216
 
1199
1217
        assertEquals(HttpServletResponse.SC_BAD_REQUEST, rc);
1200
1218
 
 
1219
        // SRV 10.9.2 - Writing the response is entirely the application's
 
1220
        // responsibility when an error occurs on an application thread.
 
1221
        // The test servlet writes no content in this case.
 
1222
        if (threaded) {
 
1223
            assertEquals(0, res.getLength());
 
1224
        } else {
 
1225
            assertTrue(res.getLength() > 0);
 
1226
        }
 
1227
 
1201
1228
        // Without this test may complete before access log has a chance to log
1202
1229
        // the request
1203
1230
        Thread.sleep(REQUEST_TIME);
1204
1231
 
1205
1232
        // Check the access log
1206
 
        alv.validateAccessLog(1, HttpServletResponse.SC_BAD_REQUEST, TIMEOUT,
1207
 
                TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
1208
 
 
 
1233
        alv.validateAccessLog(1, HttpServletResponse.SC_BAD_REQUEST, 0,
 
1234
                REQUEST_TIME);
1209
1235
    }
1210
1236
 
1211
1237
    private static class AsyncErrorServlet extends HttpServlet {
1212
1238
 
1213
1239
        private static final long serialVersionUID = 1L;
1214
1240
 
1215
 
        private int status = 200;
1216
 
 
1217
 
        public AsyncErrorServlet(int status) {
 
1241
        public static final String ERROR_MESSAGE = "It broke.";
 
1242
 
 
1243
        private int status;
 
1244
        private boolean threaded;
 
1245
 
 
1246
        public AsyncErrorServlet(int status, boolean threaded) {
1218
1247
            this.status = status;
 
1248
            this.threaded = threaded;
1219
1249
        }
1220
1250
 
1221
1251
        @Override
1224
1254
 
1225
1255
            final AsyncContext actxt = req.startAsync();
1226
1256
            actxt.setTimeout(TIMEOUT);
1227
 
            actxt.start(new Runnable() {
1228
 
                @Override
1229
 
                public void run() {
1230
 
                    try {
1231
 
                        ((HttpServletResponse) actxt.getResponse()).sendError(
1232
 
                                status);
1233
 
                    } catch (IOException e) {
1234
 
                        // Ignore
 
1257
            if (threaded) {
 
1258
                actxt.start(new Runnable() {
 
1259
                    @Override
 
1260
                    public void run() {
 
1261
                        try {
 
1262
                            HttpServletResponse resp =
 
1263
                                    (HttpServletResponse) actxt.getResponse();
 
1264
                            resp.sendError(status, ERROR_MESSAGE);
 
1265
                            // Complete so there is no delay waiting for the
 
1266
                            // timeout
 
1267
                            actxt.complete();
 
1268
                        } catch (IOException e) {
 
1269
                            // Ignore
 
1270
                        }
1235
1271
                    }
1236
 
                }
1237
 
            });
 
1272
                });
 
1273
            } else {
 
1274
                resp.sendError(status);
 
1275
            }
1238
1276
        }
1239
1277
    }
1240
 
 
1241
1278
}