View Javadoc
1   package com.github.davidmoten.aws.lw.client;
2   
3   import java.io.IOException;
4   import java.net.URL;
5   import java.nio.charset.StandardCharsets;
6   import java.util.Collections;
7   import java.util.Map;
8   
9   import com.github.davidmoten.aws.lw.client.internal.util.Util;
10  
11  public final class HttpClientTesting implements HttpClient {
12  
13      public static final HttpClientTesting INSTANCE = new HttpClientTesting(false);
14      public static final HttpClientTesting THROWING = new HttpClientTesting(true);
15  
16      private final boolean throwing;
17      public URL endpointUrl;
18      public String httpMethod;
19      public Map<String, String> headers;
20      public byte[] requestBody;
21      public int connectTimeoutMs;
22      public int readTimeoutMs;
23  
24      private HttpClientTesting(boolean throwing) {
25          this.throwing = throwing;
26      }
27      
28      @Override
29      public ResponseInputStream request(URL endpointUrl, String httpMethod,
30              Map<String, String> headers, byte[] requestBody, int connectTimeoutMs,
31              int readTimeoutMs) throws IOException {
32          this.endpointUrl = endpointUrl;
33          this.httpMethod = httpMethod;
34          this.headers = headers;
35          this.requestBody = requestBody;
36          this.connectTimeoutMs = connectTimeoutMs;
37          this.readTimeoutMs = readTimeoutMs;
38          if (throwing) {
39              throw new IOException("bingo");
40          } else {
41              return new ResponseInputStream(() -> {}, 200, Collections.emptyMap(),
42                      Util.emptyInputStream());
43          }
44      }
45  
46      @Override
47      public String toString() {
48          return "HttpClientTesting [\n  endpointUrl=" + endpointUrl + "\n  httpMethod=" + httpMethod
49                  + "\n  headers=" + headers + "\n  requestBody="
50                  + new String(requestBody, StandardCharsets.UTF_8) + "\n  connectTimeoutMs="
51                  + connectTimeoutMs + "\n  readTimeoutMs=" + readTimeoutMs + "\n]";
52      }
53  
54      public String requestBodyString() {
55          return new String(requestBody, StandardCharsets.UTF_8);
56      }
57  
58  }