summaryrefslogtreecommitdiffstats
path: root/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/ProxyAsyncHttpClientTest.java
blob: 062fd4aaa32362cebe5db181f571034fb070787b (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.communication.http;

import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ProxyAsyncHttpClientTest {

    @Test
    public void testSimple() throws Exception {
        // Can't really test much here, but verifies that the code runs.
        DummyAsyncHttpClient dummy = new DummyAsyncHttpClient(
                new HttpResult().setContent(new JSONObject().put("bar", 42)));
        ProxyAsyncHttpClient client = new ProxyAsyncHttpClient<>(dummy, "myproxyhost", 1234);

        HttpRequest r = new HttpRequest();
        r.setPath("/foo");
        r.setHost("myhost");
        r.setPort(4567);

        r.setPostContent(new JSONObject().put("foo", 34));

        client.execute(r);

        assertEquals(new HttpRequest().setPath("/myhost:4567/foo")
                                      .setHost("myproxyhost")
                                      .setPort(1234)
                                      .setPostContent(new JSONObject().put("foo", 34)),
                     dummy.lastRequest);
    }

    @Test
    public void testNoAndEmptyPath() throws Exception {
        DummyAsyncHttpClient dummy = new DummyAsyncHttpClient(
                new HttpResult().setContent(new JSONObject().put("bar", 42)));
        ProxyAsyncHttpClient client = new ProxyAsyncHttpClient<>(dummy, "myproxyhost", 1234);
        try{
            client.execute(new HttpRequest());
            assertTrue(false);
        } catch (IllegalStateException e) {
            assertTrue(e.getMessage().contains("Host and path must be set prior"));
        }
        client.execute(new HttpRequest().setHost("local").setPath(""));
    }

}