aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/JDiscHttpServletTest.java
blob: 348bfd6183e87dd26072944f77764a9222e982cd (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
// Copyright Yahoo. 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.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpHead;
import org.apache.hc.client5.http.classic.methods.HttpOptions;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.classic.methods.HttpTrace;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.junit.jupiter.api.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
    void requireThatServerRespondsToAllMethods() throws Exception {
        final JettyTestDriver driver = JettyTestDriver.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
    void requireThatServerResponds405ToUnknownMethods() throws IOException {
        JettyTestDriver driver = JettyTestDriver.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 HttpUriRequestBase {
        UnknownMethodHttpRequest(URI uri) { super("UNKNOWN_METHOD", uri); }
    }
}