View Javadoc

1   package com.atlassian.marketplace.client.http;
2   
3   import java.net.URI;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.HashMap;
7   import java.util.Map;
8   import java.util.concurrent.atomic.AtomicInteger;
9   
10  import com.atlassian.marketplace.client.impl.CommonsHttpTransport;
11  import com.atlassian.utt.http.TestHttpServer;
12  import com.atlassian.utt.http.TestHttpServer.RequestProperties;
13  import com.atlassian.utt.matchers.NamedFunction;
14  
15  import com.google.common.base.Function;
16  import com.google.common.base.Supplier;
17  import com.google.common.collect.ImmutableMap;
18  import com.google.common.collect.Multimap;
19  
20  import org.apache.commons.io.IOUtils;
21  import org.hamcrest.Matcher;
22  import org.junit.After;
23  import org.junit.Before;
24  
25  import static com.atlassian.fugue.Option.some;
26  import static com.atlassian.utt.http.TestHttpServer.RequestProperties.header;
27  import static com.atlassian.utt.http.TestHttpServer.RequestProperties.requestBody;
28  import static com.atlassian.utt.matchers.NamedFunction.namedFunction;
29  import static com.google.common.collect.Multimaps.newListMultimap;
30  import static org.hamcrest.Matchers.allOf;
31  import static org.hamcrest.Matchers.contains;
32  import static org.hamcrest.Matchers.equalTo;
33  
34  public class CommonsHttpTransportTestBase
35  {
36      private static AtomicInteger nextServerPort = new AtomicInteger(8173);
37      
38      protected static final URI TEST_URI = URI.create("/test/request");
39      
40      protected static final String CONTENT_STRING = "a thing";
41      
42      protected int serverPort;
43      protected URI serverUri;
44      protected TestHttpServer server;
45      protected CommonsHttpTransport http;
46      protected SimpleHttpResponse resp;
47      
48      @Before
49      public void setup() throws Exception
50      {
51          serverPort = nextServerPort.getAndIncrement();
52          serverUri = URI.create("http://localhost:" + serverPort);
53          
54          server = TestHttpServer.create(serverPort);
55          server.start();
56      }
57      
58      @After
59      public void teardown()
60      {
61          server.stop();
62          if (http != null)
63          {
64              http.close();
65          }
66          if (resp != null)
67          {
68              resp.close();
69          }
70      }
71      
72      protected byte[] makeContent() throws Exception
73      {
74          return CONTENT_STRING.getBytes("UTF-8");
75      }
76      
77      protected Multimap<String, String> makeParams() throws Exception
78      {
79          Multimap<String, String> params = newListMultimap(new HashMap<String, Collection<String>>(),
80              new Supplier<ArrayList<String>>()
81              {
82                  @Override
83                  public ArrayList<String> get()
84                  {
85                      return new ArrayList<String>();
86                  }
87              });
88          params.put("param", "value");
89          params.put("foo", "bar");
90          params.put("foo", "baz");
91          return params;
92      }
93      
94      protected HttpConfiguration.Builder baseConfig()
95      {
96          return HttpConfiguration.builder();
97      }
98      
99      protected HttpConfiguration.Builder withSimpleRequestDecorator(HttpConfiguration.Builder builder)
100     {
101         RequestDecorator rd = new RequestDecorator()
102             {
103                 @Override
104                 public Map<String, String> getRequestHeaders()
105                 {
106                     return ImmutableMap.of("foo", "bar");
107                 }
108             };
109         return builder.requestDecorator(some(rd));
110     }
111     
112     protected RequestDecorator simpleRequestDecorator(final Map<String, String> headers)
113     {
114         return new RequestDecorator()
115         {
116             @Override
117             public Map<String, String> getRequestHeaders()
118             {
119                 return headers;
120             }
121         };
122     }
123     
124     protected Matcher<RequestProperties> hasPostParams()
125     {
126         return allOf(
127             header("Content-Type").is(contains("application/x-www-form-urlencoded")),
128             requestBody().is(equalTo("param=value&foo=bar&foo=baz"))
129         );
130     }
131     
132     protected Matcher<RequestProperties> hasRequestDecoratorHeaders()
133     {
134         return header("foo").is(contains("bar"));
135     }
136     
137     protected static NamedFunction<SimpleHttpResponse, Integer> responseStatus()
138     {
139         return namedFunction("responseStatus", new Function<SimpleHttpResponse, Integer>()
140         {
141             @Override
142             public Integer apply(SimpleHttpResponse r)
143             {
144                 return r.getStatus();
145             }
146         });
147     }
148     
149     protected static NamedFunction<SimpleHttpResponse, String> responseBody()
150     {
151         return namedFunction("responseBody", new Function<SimpleHttpResponse, String>()
152         {
153             @Override
154             public String apply(SimpleHttpResponse r)
155             {
156                 try
157                 {
158                     return new String(IOUtils.toByteArray(r.getContentStream()), "UTF-8");
159                 }
160                 catch (Exception e)
161                 {
162                     throw new RuntimeException(e);
163                 }
164             }
165         });
166     }
167 }