Skip to content

Commit

Permalink
adding a test
Browse files Browse the repository at this point in the history
  • Loading branch information
virajjasani committed Dec 25, 2021
1 parent 02cc4d6 commit 8501c77
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public class ProfileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(ProfileServlet.class);

private static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
private static final String ALLOWED_METHODS = "GET";
private static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
private static final String CONTENT_TYPE_TEXT = "text/plain; charset=utf-8";
private static final String ASYNC_PROFILER_HOME_ENV = "ASYNC_PROFILER_HOME";
private static final String ASYNC_PROFILER_HOME_SYSTEM_PROPERTY = "async.profiler.home";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.http;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.http.HttpServletResponse;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Small test to cover default disabled prof endpoint.
*/
public class TestDisabledProfileServlet extends HttpServerFunctionalTest {

private static HttpServer2 server;
private static URL baseUrl;

@BeforeClass
public static void setup() throws Exception {
server = createTestServer();
server.start();
baseUrl = getServerURL(server);
}

@AfterClass
public static void cleanup() throws Exception {
server.stop();
}

@Test
public void testQuery() throws Exception {
try {
readOutput(new URL(baseUrl, "/prof"));
throw new IllegalStateException("Should not reach here");
} catch (IOException e) {
assertTrue(e.getMessage()
.contains(HttpServletResponse.SC_INTERNAL_SERVER_ERROR + " for URL: " + baseUrl));
}

// CORS headers
HttpURLConnection conn =
(HttpURLConnection) new URL(baseUrl, "/prof").openConnection();
assertEquals("GET", conn.getHeaderField(ProfileServlet.ACCESS_CONTROL_ALLOW_METHODS));
assertNotNull(conn.getHeaderField(ProfileServlet.ACCESS_CONTROL_ALLOW_ORIGIN));
}

@Test
public void testRequestMethods() throws IOException {
assertEquals("Unexpected response code", HttpServletResponse.SC_METHOD_NOT_ALLOWED,
getConnection("PUT").getResponseCode());
assertEquals("Unexpected response code", HttpServletResponse.SC_METHOD_NOT_ALLOWED,
getConnection("POST").getResponseCode());
assertEquals("Unexpected response code", HttpServletResponse.SC_METHOD_NOT_ALLOWED,
getConnection("DELETE").getResponseCode());
assertEquals("Unexpected response code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getConnection("GET").getResponseCode());
}

private HttpURLConnection getConnection(final String method) throws IOException {
URL url = new URL(baseUrl, "/prof");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
return conn;
}

}

0 comments on commit 8501c77

Please sign in to comment.