Debian Codegraph @

jetty-ee10/jetty-ee10-glassfish-jstl/src/test/java/org/eclipse/jetty/ee10/jstl/JspIncludeTest.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
4 //
5 // This program and the accompanying materials are made available under the
6 // terms of the Eclipse Public License v. 2.0 which is available at
7 // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
8 // which is available at https://www.apache.org/licenses/LICENSE-2.0.
9 //
10 // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
11 // ========================================================================
12 //
13
14 package org.eclipse.jetty.ee10.jstl;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.io.StringWriter;
21 import java.net.HttpURLConnection;
22 import java.net.URI;
23 import java.nio.file.Path;
24
25 import org.eclipse.jetty.ee10.annotations.AnnotationConfiguration;
26 import org.eclipse.jetty.ee10.webapp.Configurations;
27 import org.eclipse.jetty.ee10.webapp.JettyWebXmlConfiguration;
28 import org.eclipse.jetty.ee10.webapp.WebAppContext;
29 import org.eclipse.jetty.server.Server;
30 import org.eclipse.jetty.server.ServerConnector;
31 import org.eclipse.jetty.toolchain.test.FS;
32 import org.eclipse.jetty.toolchain.test.IO;
33 import org.eclipse.jetty.toolchain.test.JAR;
34 import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
35 import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
36 import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
37 import org.junit.jupiter.api.AfterAll;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.extension.ExtendWith;
41
42 import static org.hamcrest.MatcherAssert.assertThat;
43 import static org.hamcrest.Matchers.containsString;
44 import static org.hamcrest.Matchers.is;
45
46 @ExtendWith(WorkDirExtension.class)
47 public class JspIncludeTest
48 {
49 private static Server server;
50 private static URI baseUri;
51
52 @BeforeAll
53 public static void startServer(WorkDir workDir) throws Exception
54 {
55 Path tmpPath = workDir.getEmptyPathDir();
56 // Setup Server
57 server = new Server();
58 ServerConnector connector = new ServerConnector(server);
59 connector.setPort(0);
60 server.addConnector(connector);
61
62 //Base dir for test
63 File testDir = tmpPath.toFile();
64 File testLibDir = new File(testDir, "WEB-INF/lib");
65 FS.ensureDirExists(testLibDir);
66
67 //Make a taglib jar
68 File srcTagLibDir = MavenTestingUtils.getProjectDir("src/test/taglibjar");
69 File scratchTagLibDir = new File(testDir, JspIncludeTest.class.getSimpleName() + "-taglib-scratch");
70 IO.copy(srcTagLibDir, scratchTagLibDir);
71 File tagLibJar = new File(testLibDir, "testtaglib.jar");
72 JAR.create(scratchTagLibDir, tagLibJar);
73
74 //Copy content
75 File destWebAppDir = new File(testDir, "webapp");
76 FS.ensureDirExists(destWebAppDir);
77 File srcWebAppDir = MavenTestingUtils.getProjectDir("src/test/webapp");
78 IO.copyDir(srcWebAppDir, destWebAppDir);
79
80 // Configure WebAppContext
81 Configurations.setServerDefault(server).add(new JettyWebXmlConfiguration(), new AnnotationConfiguration());
82
83 WebAppContext context = new WebAppContext();
84 context.setContextPath("/");
85
86 File scratchDir = new File(testDir, JspIncludeTest.class.getSimpleName() + "-scratch");
87 FS.ensureEmpty(scratchDir);
88 JspConfig.init(context, destWebAppDir.toURI(), scratchDir);
89
90 server.setHandler(context);
91
92 // Start Server
93 server.start();
94
95 // Figure out Base URI
96 String host = connector.getHost();
97 if (host == null)
98 {
99 host = "localhost";
100 }
101 int port = connector.getLocalPort();
102 baseUri = new URI(String.format("http://%s:%d/", host, port));
103 }
104
105 @AfterAll
106 public static void stopServer() throws Exception
107 {
108 server.stop();
109 }
110
111 @Test
112 public void testTopWithIncluded() throws IOException
113 {
114 URI uri = baseUri.resolve("/top.jsp");
115
116 InputStream in = null;
117 InputStreamReader reader = null;
118 HttpURLConnection connection = null;
119
120 try
121 {
122 connection = (HttpURLConnection)uri.toURL().openConnection();
123 connection.connect();
124 if (HttpURLConnection.HTTP_OK != connection.getResponseCode())
125 {
126 String body = getPotentialBody(connection);
127 String err = String.format("GET request failed (%d %s) %s%n%s", connection.getResponseCode(), connection.getResponseMessage(),
128 uri.toASCIIString(), body);
129 throw new IOException(err);
130 }
131 in = connection.getInputStream();
132 reader = new InputStreamReader(in);
133 StringWriter writer = new StringWriter();
134 IO.copy(reader, writer);
135
136 String response = writer.toString();
137 // System.out.printf("Response%n%s",response);
138 assertThat("Response", response, containsString("<h2> Hello, this is the top page."));
139 assertThat("Response", response, containsString("<h3> This is the included page"));
140 assertThat("Response Header[main-page-key]", connection.getHeaderField("main-page-key"), is("main-page-value"));
141 assertThat("Response Header[included-page-key]", connection.getHeaderField("included-page-key"), is("included-page-value"));
142 }
143 finally
144 {
145 IO.close(reader);
146 IO.close(in);
147 }
148 }
149
150 /**
151 * Attempt to obtain the body text if available. Do not throw an exception if body is unable to be fetched.
152 *
153 * @param connection the connection to fetch the body content from.
154 * @return the body content, if present.
155 */
156 private String getPotentialBody(HttpURLConnection connection)
157 {
158 InputStream in = null;
159 InputStreamReader reader = null;
160 try
161 {
162 in = connection.getInputStream();
163 reader = new InputStreamReader(in);
164 StringWriter writer = new StringWriter();
165 IO.copy(reader, writer);
166 return writer.toString();
167 }
168 catch (IOException e)
169 {
170 return "<no body:" + e.getMessage() + ">";
171 }
172 finally
173 {
174 IO.close(reader);
175 IO.close(in);
176 }
177 }
178 }