aboutsummaryrefslogtreecommitdiffstats
path: root/application/src/test/java/com/yahoo/application/container/JDiscContainerRequestTest.java
blob: 9f5555069cd16f438b18a022cd66bea728407ef5 (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
// Copyright 2017 Yahoo Holdings. 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.Test;

import java.nio.charset.CharacterCodingException;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

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

    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" +
               "</container>";
    }

    @Test
    public void requireThatRequestBodyWorks() throws InterruptedException, 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);
            assertThat(response.getBodyAsString(), equalTo(DATA));
            req.toString();
            response.toString();
        }
    }

    @Test
    public void requireThatCustomRequestHeadersWork() throws InterruptedException {
        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);
            assertThat(response.getHeaders().contains("X-Foo", "Bar"), is(true));
            req.toString();
            response.toString();
        }
    }

    @Test(expected = WriteException.class)
    public void requireThatRequestHandlerThatThrowsInWriteWorks() throws InterruptedException {
        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(expected = DelayedWriteException.class)
    public void requireThatRequestHandlerThatThrowsDelayedInWriteWorks() throws InterruptedException {
        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();
        }
    }

}