aboutsummaryrefslogtreecommitdiffstats
path: root/application/src/test/java/com/yahoo/application/container/ContainerRequestTest.java
blob: 24cae99b5ab331c28a666271e80c9e78cac02af2 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application.container;

import com.yahoo.application.Networking;
import com.yahoo.application.container.handler.Request;
import com.yahoo.application.container.handler.Response;
import com.yahoo.application.container.handlers.DelayedThrowingInWriteRequestHandler;
import com.yahoo.application.container.handlers.DelayedWriteException;
import com.yahoo.application.container.handlers.EchoRequestHandler;
import com.yahoo.application.container.handlers.HeaderEchoRequestHandler;
import com.yahoo.application.container.handlers.ThrowingInWriteRequestHandler;
import com.yahoo.application.container.handlers.WriteException;
import com.yahoo.text.Utf8;
import org.junit.jupiter.api.Test;

import java.nio.charset.CharacterCodingException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author Einar M R Rosenvinge
 */
public class ContainerRequestTest {

    private static String getXML(String className, String binding) {
        return "<container version=\"1.0\">\n" +
               "  <handler id=\"test-handler\" class=\"" +
               className +
               "\">\n" +
               "    <binding>" +
               binding +
               "</binding>\n" +
               "  </handler>\n" +
               "  <accesslog type=\"disabled\" />" +
               "</container>";
    }

    @Test
    void requireThatRequestBodyWorks() throws CharacterCodingException {
        String DATA = "we have no bananas today";
        Request req = new Request("http://banana/echo", DATA.getBytes(Utf8.getCharset()));

        try (JDisc container = JDisc.fromServicesXml(getXML(EchoRequestHandler.class.getCanonicalName(), "http://*/echo"), Networking.disable)) {
            Response response = container.handleRequest(req);
            assertEquals(DATA, response.getBodyAsString());
            req.toString();
            response.toString();
        }
    }

    @Test
    void requireThatCustomRequestHeadersWork() {
        Request req = new Request("http://banana/echo");
        req.getHeaders().add("X-Foo", "Bar");

        try (JDisc container = JDisc.fromServicesXml(getXML(HeaderEchoRequestHandler.class.getCanonicalName(), "http://*/echo"), Networking.disable)) {
            Response response = container.handleRequest(req);
            assertTrue(response.getHeaders().contains("X-Foo", "Bar"));
            req.toString();
            response.toString();
        }
    }

    @Test
    void requireThatRequestHandlerThatThrowsInWriteWorks() {
        assertThrows(WriteException.class, () -> {
            String DATA = "we have no bananas today";
            Request req = new Request("http://banana/throwwrite", DATA.getBytes(Utf8.getCharset()));

            try (JDisc container = JDisc.fromServicesXml(getXML(ThrowingInWriteRequestHandler.class.getCanonicalName(), "http://*/throwwrite"), Networking.disable)) {
                Response response = container.handleRequest(req);
                req.toString();
            }
        });
    }

    @Test
    void requireThatRequestHandlerThatThrowsDelayedInWriteWorks() {
        assertThrows(DelayedWriteException.class, () -> {
            String DATA = "we have no bananas today";
            Request req = new Request("http://banana/delayedthrowwrite", DATA.getBytes(Utf8.getCharset()));

            try (JDisc container = JDisc.fromServicesXml(getXML(DelayedThrowingInWriteRequestHandler.class.getCanonicalName(), "http://*/delayedthrowwrite"), Networking.disable)) {
                Response response = container.handleRequest(req);
                req.toString();
            }

        });

    }

}