Skip to content

Commit 8501c77

Browse files
committed
adding a test
1 parent 02cc4d6 commit 8501c77

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/ProfileServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ public class ProfileServlet extends HttpServlet {
100100
private static final long serialVersionUID = 1L;
101101
private static final Logger LOG = LoggerFactory.getLogger(ProfileServlet.class);
102102

103-
private static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
103+
static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
104+
static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
104105
private static final String ALLOWED_METHODS = "GET";
105-
private static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
106106
private static final String CONTENT_TYPE_TEXT = "text/plain; charset=utf-8";
107107
private static final String ASYNC_PROFILER_HOME_ENV = "ASYNC_PROFILER_HOME";
108108
private static final String ASYNC_PROFILER_HOME_SYSTEM_PROPERTY = "async.profiler.home";
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.http;
20+
21+
import java.io.IOException;
22+
import java.net.HttpURLConnection;
23+
import java.net.URL;
24+
import javax.servlet.http.HttpServletResponse;
25+
26+
import org.junit.AfterClass;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
/**
31+
* Small test to cover default disabled prof endpoint.
32+
*/
33+
public class TestDisabledProfileServlet extends HttpServerFunctionalTest {
34+
35+
private static HttpServer2 server;
36+
private static URL baseUrl;
37+
38+
@BeforeClass
39+
public static void setup() throws Exception {
40+
server = createTestServer();
41+
server.start();
42+
baseUrl = getServerURL(server);
43+
}
44+
45+
@AfterClass
46+
public static void cleanup() throws Exception {
47+
server.stop();
48+
}
49+
50+
@Test
51+
public void testQuery() throws Exception {
52+
try {
53+
readOutput(new URL(baseUrl, "/prof"));
54+
throw new IllegalStateException("Should not reach here");
55+
} catch (IOException e) {
56+
assertTrue(e.getMessage()
57+
.contains(HttpServletResponse.SC_INTERNAL_SERVER_ERROR + " for URL: " + baseUrl));
58+
}
59+
60+
// CORS headers
61+
HttpURLConnection conn =
62+
(HttpURLConnection) new URL(baseUrl, "/prof").openConnection();
63+
assertEquals("GET", conn.getHeaderField(ProfileServlet.ACCESS_CONTROL_ALLOW_METHODS));
64+
assertNotNull(conn.getHeaderField(ProfileServlet.ACCESS_CONTROL_ALLOW_ORIGIN));
65+
}
66+
67+
@Test
68+
public void testRequestMethods() throws IOException {
69+
assertEquals("Unexpected response code", HttpServletResponse.SC_METHOD_NOT_ALLOWED,
70+
getConnection("PUT").getResponseCode());
71+
assertEquals("Unexpected response code", HttpServletResponse.SC_METHOD_NOT_ALLOWED,
72+
getConnection("POST").getResponseCode());
73+
assertEquals("Unexpected response code", HttpServletResponse.SC_METHOD_NOT_ALLOWED,
74+
getConnection("DELETE").getResponseCode());
75+
assertEquals("Unexpected response code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
76+
getConnection("GET").getResponseCode());
77+
}
78+
79+
private HttpURLConnection getConnection(final String method) throws IOException {
80+
URL url = new URL(baseUrl, "/prof");
81+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
82+
conn.setRequestMethod(method);
83+
return conn;
84+
}
85+
86+
}

0 commit comments

Comments
 (0)