View Javadoc
1   package com.github.davidmoten.aws.lw.client;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.IOException;
5   import java.net.URL;
6   import java.util.LinkedList;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.Queue;
10  import java.util.concurrent.CopyOnWriteArrayList;
11  
12  public class HttpClientTestingWithQueue implements HttpClient {
13  
14      // needs to be volatile to work with Multipart async operations
15      private final Queue<Object> queue = new LinkedList<>();
16      private final List<String> urls = new CopyOnWriteArrayList<>();
17      private final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
18  
19      public void add(ResponseInputStream r) {
20          queue.add(r);
21      }
22  
23      public void add(IOException e) {
24          queue.add(e);
25      }
26  
27      public List<String> urls() {
28          return urls;
29      }
30  
31      public byte[] bytes() {
32          return bytes.toByteArray();
33      }
34  
35      @Override
36      public synchronized ResponseInputStream request(URL endpointUrl, String httpMethod, Map<String, String> headers,
37              byte[] requestBody, int connectTimeoutMs, int readTimeoutMs) throws IOException {
38          urls.add(httpMethod + ":" + endpointUrl.toString());
39          Object o = queue.poll();
40          if (o instanceof ResponseInputStream) {
41              ResponseInputStream r = (ResponseInputStream) o;
42              if (r.statusCode() == 200 && requestBody != null && httpMethod == "PUT") {
43                  bytes.write(requestBody);
44              }
45              return r;
46          } else {
47              throw (IOException) o;
48          }
49      }
50  
51  }