Skip to content

Commit

Permalink
Fixed issue perwendel#1024 implementation and unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Lepe committed Aug 11, 2022
1 parent 59495a0 commit ffab528
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 60 deletions.
19 changes: 6 additions & 13 deletions src/main/java/spark/embeddedserver/jetty/JettyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.session.SessionHandler;

Expand All @@ -50,27 +52,18 @@ public void doHandle(
HttpServletResponse response) throws IOException, ServletException {

HttpRequestWrapper wrapper = new HttpRequestWrapper(request);
final String[] METHODS = {"GET", "POST", "HEAD", "PUT", "OPTIONS", "DELETE", "TRACE", "CONNECT "};
boolean isValid = false;
for (String METHOD : METHODS) {
if (request.getMethod().equalsIgnoreCase(METHOD)) {
isValid = true;
break;
}
HttpMethod method = HttpMethod.fromString(request.getMethod().trim().toUpperCase());
if(method == null) {
response.sendError(HttpStatus.METHOD_NOT_ALLOWED_405);
return;
}
if (!isValid) return;

if(consume!=null && consume.contains(baseRequest.getRequestURI())){
wrapper.notConsumed(true);
} else {
filter.doFilter(wrapper, response, null);
}

if (wrapper.notConsumed()) {
baseRequest.setHandled(false);
} else {
baseRequest.setHandled(true);
}
baseRequest.setHandled(!wrapper.notConsumed());
}

Expand Down
117 changes: 72 additions & 45 deletions src/test/java/spark/InvalidRequestTest.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,88 @@
package spark;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.net.URI;

import static org.junit.Assert.assertEquals;
import spark.util.SparkTestUtil;

import static spark.Spark.halt;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spark.Spark.awaitInitialization;
import static spark.Spark.get;
import static spark.Spark.staticFileLocation;

public class InvalidRequestTest {
@Test
public void invalidRequestTest(){
Service service = Service.ignite().port(4567);
service.staticFiles.externalLocation("/Users/");

service.get("/", (req, res) -> {
if (!req.requestMethod().equalsIgnoreCase("GET")) {
halt(401, "invalid Http method");
}
return null;

public static class HttpFoo extends HttpRequestBase {
public final static String METHOD_NAME = "FOO";
@Override
public String getMethod() {
return METHOD_NAME;
}
public HttpFoo(final String uri) {
super();
setURI(URI.create(uri));
}
public String getName() {
return "FOO";
}
}

public static final String FILE = "/page.html";

public static final String SERVICE="/test";

public static final int PORT = 4567;

private static final SparkTestUtil http = new SparkTestUtil(PORT);

@Before
public void setup() {
staticFileLocation("/public");
get(SERVICE, (request, response) -> {
assertTrue(request.requestMethod().equalsIgnoreCase("GET"));
return "Hello";
});

String result = "";
String url = "http://localhost:4567";
BufferedReader in = null;
awaitInitialization();
}

public int requestPathWithInvalidMethod(String path) {
int code = 0;
try {
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("Method", "XYZ");
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpFoo fooMethod = new HttpFoo("http://localhost:" + PORT + path);
HttpResponse response = httpClient.execute(fooMethod);
code = response.getStatusLine().getStatusCode();
} catch (Exception e) {
return;
fail("Unexpected exception: " + e.getMessage());
}
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
return code;
}

@Test
public void invalidRequestTest(){
// Testing that file and service is up:
try {
SparkTestUtil.UrlResponse response = http.doMethod("GET", SERVICE, "");
assertEquals(200, response.status);
assertEquals("Hello", response.body);

response = http.doMethod("GET", FILE, "");
assertEquals(200, response.status);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception: " + e.getMessage());
}
assertEquals("", result);
// Testing wrong method (we cannot use http.doMethod as it can not handle invalid methods)
assertEquals(405, requestPathWithInvalidMethod(FILE));
assertEquals(405, requestPathWithInvalidMethod(SERVICE));
}
}
2 changes: 2 additions & 0 deletions src/test/java/spark/Issue1077Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public void setup() {
response.status(406);
return "Go Away!!!";
});

awaitInitialization();
}

// CS304 Issue link: https://github.com/perwendel/spark/issues/1077
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/spark/examples/hello/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
package spark.examples.hello;

import static spark.Spark.get;
import static spark.Spark.staticFileLocation;

/**
* Minimal example
*
* You can test from command with:
* > curl -i 'http://localhost:4567/'
*/
@SuppressWarnings("JavadocLinkAsPlainText")
public class HelloWorld {

public static void main(String[] args) {

staticFileLocation("/public/");
get("/", (request, response) -> "Hello World!");

}
Expand Down

0 comments on commit ffab528

Please sign in to comment.