aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/jdisc/HttpRequestTestCase.java
blob: 4a71b28d5786664cb099fef73eb039333e7631bf (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
95
96
97
98
99
100
101
102
103
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc;

import static org.junit.jupiter.api.Assertions.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.yahoo.jdisc.http.HttpRequest.Method;
import com.yahoo.text.Utf8;

/**
 * API control of HttpRequest.
 *
 * @author Steinar Knutsen
 */
public class HttpRequestTestCase {
    private static final String X_RAY_YANKEE_ZULU = "x-ray yankee zulu";
    private static final String HTTP_MAILHOST_25_ALPHA_BRAVO_CHARLIE_DELTA = "http://mailhost:25/alpha?bravo=charlie&falseboolean=false&trueboolean=true";
    HttpRequest r;
    InputStream requestData;

    @BeforeEach
    public void setUp() throws Exception {
        requestData = new ByteArrayInputStream(Utf8.toBytes(X_RAY_YANKEE_ZULU));
        r = HttpRequest.createTestRequest(HTTP_MAILHOST_25_ALPHA_BRAVO_CHARLIE_DELTA, Method.GET, requestData, Collections.singletonMap("foxtrot", "golf"));
    }

    @AfterEach
    public void tearDown() throws Exception {
        r = null;
    }

    @Test
    final void testGetMethod() {
        assertSame(Method.GET, r.getMethod());
    }

    @Test
    final void testGetUri() throws URISyntaxException {
        assertEquals(new URI(HTTP_MAILHOST_25_ALPHA_BRAVO_CHARLIE_DELTA), r.getUri());
    }

    @Test
    final void testGetJDiscRequest() throws URISyntaxException {
        assertEquals(new URI(HTTP_MAILHOST_25_ALPHA_BRAVO_CHARLIE_DELTA), r.getJDiscRequest().getUri());
    }

    @Test
    final void testGetProperty() {
        assertEquals("charlie", r.getProperty("bravo"));
        assertEquals("golf", r.getProperty("foxtrot"));
        assertNull(r.getProperty("zulu"));
    }

    @Test
    final void testPropertyMap() {
        assertEquals(4, r.propertyMap().size());
    }

    @Test
    final void testGetBooleanProperty() {
        assertTrue(r.getBooleanProperty("trueboolean"));
        assertFalse(r.getBooleanProperty("falseboolean"));
        assertFalse(r.getBooleanProperty("bravo"));
    }

    @Test
    final void testHasProperty() {
        assertFalse(r.hasProperty("alpha"));
        assertTrue(r.hasProperty("bravo"));
    }

    @Test
    final void testGetHeader() {
        assertNull(r.getHeader("SyntheticHeaderFor-com.yahoo.container.jdisc.HttpRequestTestCase"));
    }

    @Test
    final void testGetHost() {
        assertEquals("mailhost", r.getHost());
    }

    @Test
    final void testGetPort() {
        assertEquals(25, r.getPort());
    }

    @Test
    final void testGetData() throws IOException {
        byte[] b = new byte[X_RAY_YANKEE_ZULU.length()];
        r.getData().read(b);
        assertEquals(X_RAY_YANKEE_ZULU, Utf8.toString(b));
    }

}