blob: 230f59cbb34a3c24a465c5cc55fc882cc3ed01c0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.handler.AbstractRequestHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.handler.ResponseHandler;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import static com.yahoo.jdisc.Response.Status.METHOD_NOT_ALLOWED;
import static com.yahoo.jdisc.Response.Status.OK;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author Simon Thoresen Hult
*/
public class JDiscHttpServletTest {
@Test
public void requireThatServerRespondsToAllMethods() throws Exception {
final TestDriver driver = TestDrivers.newInstance(newEchoHandler());
final URI uri = driver.client().newUri("/status.html");
driver.client().execute(new HttpGet(uri))
.expectStatusCode(is(OK));
driver.client().execute(new HttpPost(uri))
.expectStatusCode(is(OK));
driver.client().execute(new HttpHead(uri))
.expectStatusCode(is(OK));
driver.client().execute(new HttpPut(uri))
.expectStatusCode(is(OK));
driver.client().execute(new HttpDelete(uri))
.expectStatusCode(is(OK));
driver.client().execute(new HttpOptions(uri))
.expectStatusCode(is(OK));
driver.client().execute(new HttpTrace(uri))
.expectStatusCode(is(OK));
driver.client().execute(new HttpPatch(uri))
.expectStatusCode(is(OK));
assertThat(driver.close(), is(true));
}
@Test
public void requireThatServerResponds405ToUnknownMethods() throws IOException {
TestDriver driver = TestDrivers.newInstance(newEchoHandler());
final URI uri = driver.client().newUri("/status.html");
driver.client().execute(new UnknownMethodHttpRequest(uri))
.expectStatusCode(is(METHOD_NOT_ALLOWED));
assertThat(driver.close(), is(true));
}
private static RequestHandler newEchoHandler() {
return new AbstractRequestHandler() {
@Override
public ContentChannel handleRequest(final Request request, final ResponseHandler handler) {
return handler.handleResponse(new Response(OK));
}
};
}
private static class UnknownMethodHttpRequest extends HttpRequestBase {
UnknownMethodHttpRequest(URI uri) { setURI(uri); }
@Override public String getMethod() { return "UNKNOWN_METHOD"; }
}
}
|