aboutsummaryrefslogtreecommitdiffstats
path: root/routing-generator/src/test/java/com/yahoo/vespa/hosted/routing/mock/HttpClientMock.java
blob: 025eac90b8d9f49761fdb9f81c7094dd4fb3fdd2 (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.vespa.hosted.routing.mock;

import com.yahoo.yolean.Exceptions;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author mpolden
 */
@SuppressWarnings("deprecation") // Deprecations in third-party interface
public class HttpClientMock extends CloseableHttpClient {

    private final Map<String, CloseableHttpResponse> responses = new HashMap<>();

    public HttpClientMock setResponse(String method, String url, CloseableHttpResponse response) {
        responses.put(requestKey(method, url), response);
        return this;
    }

    @Override
    protected CloseableHttpResponse doExecute(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext) {
        String key = requestKey(httpRequest.getRequestLine().getMethod(), httpRequest.getRequestLine().getUri());
        CloseableHttpResponse response = responses.get(key);
        if (response == null) {
            throw new IllegalArgumentException("No response defined for " + key);
        }
        return response;
    }

    @Override
    public void close() {}

    @Override
    public HttpParams getParams() {
        throw new UnsupportedOperationException();
    }

    @Override
    public ClientConnectionManager getConnectionManager() {
        throw new UnsupportedOperationException();
    }

    private static String requestKey(String method, String url) {
        return method.toUpperCase(Locale.ENGLISH) + " " + url;
    }

    public static class JsonResponse extends BasicHttpResponse implements CloseableHttpResponse {

        public JsonResponse(Path jsonFile, int code) {
            this(Exceptions.uncheck(() -> Files.readString(jsonFile)), code);
        }

        public JsonResponse(String json, int code) {
            super(HttpVersion.HTTP_1_1, code, null);
            setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
        }

        @Override
        public void close() {}

    }

}