blob: 528ef7598ba08f2da9ffb480ec67c8f231e2b19e [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
2 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17import java.io.*;
18import java.net.*;
19import com.sun.ejte.ccl.reporter.*;
20
21/**
22 * Unit test for:
23 *
24 * https://issues.apache.org/bugzilla/show_bug.cgi?id=44391
25 * ("SSI handling of escaped characters broken")
26 *
27 */
28public class WebTest {
29
30 private static SimpleReporterAdapter stat
31 = new SimpleReporterAdapter("appserv-tests");
32
33 private static final String TEST_NAME
34 = "ssi-escape-character";
35
36 private static final String EXPECTED_CONTENT_TYPE = "content-type: text/html";
37 private static final String EXPECTED = "Happy""";
38
39 private String host;
40 private String port;
41 private String contextRoot;
42
43 public WebTest(String[] args) {
44 host = args[0];
45 port = args[1];
46 contextRoot = args[2];
47 }
David Matějčekf4dc06a2021-05-17 12:10:57 +020048
Vinay Vishal57171472018-09-18 20:22:00 +053049 public static void main(String[] args) {
50 stat.addDescription("Unit test for SSI Escape Character");
51 WebTest webTest = new WebTest(args);
52 webTest.doTest();
53 stat.printSummary(TEST_NAME);
54 }
55
David Matějčekf4dc06a2021-05-17 12:10:57 +020056 public void doTest() {
57 try {
Vinay Vishal57171472018-09-18 20:22:00 +053058 invoke();
59 } catch (Exception ex) {
60 stat.addStatus(TEST_NAME, stat.FAIL);
61 ex.printStackTrace();
62 }
63 }
64
65 private void invoke() throws Exception {
66
David Matějčekf4dc06a2021-05-17 12:10:57 +020067 System.out.println("Host=" + host + ", port=" + port);
Vinay Vishal57171472018-09-18 20:22:00 +053068 Socket sock = new Socket(host, new Integer(port).intValue());
69 OutputStream os = sock.getOutputStream();
70 String get = "GET " + contextRoot + "/index.shtml HTTP/1.1\n";
71 System.out.println(get);
72 os.write(get.getBytes());
73 os.write("Host: localhost\n".getBytes());
74 os.write("\n".getBytes());
75
76 InputStream is = sock.getInputStream();
77 BufferedReader bis = new BufferedReader(new InputStreamReader(is));
78
79 String line = null;
80 boolean hasExpectedContentType = false;
81 boolean hasExpectedResponse = false;
82 while ((line = bis.readLine()) != null) {
83 System.out.println(line);
84 if (!hasExpectedContentType) {
85 if (line.toLowerCase().startsWith(EXPECTED_CONTENT_TYPE)) {
86 hasExpectedContentType = true;
87 }
88 } else if (line.equals(EXPECTED)) {
89 hasExpectedResponse = true;
90 break;
91 }
92 }
93
94 if (hasExpectedContentType && hasExpectedResponse) {
95 stat.addStatus(TEST_NAME, stat.PASS);
96 } else {
97 if (!hasExpectedContentType) {
98 System.err.println("Missing expected content type: " + EXPECTED_CONTENT_TYPE);
99 }
100 if (!hasExpectedResponse) {
101 System.err.println("Missing expected response: " + EXPECTED);
102 }
103 stat.addStatus(TEST_NAME, stat.FAIL);
104 }
105 }
106}