root / extensions / gae / src / test / java / org / jclouds / gae / GaeHttpCommandExecutorServiceTest.java @ 35e7942d
History | View | Annotate | Download (7.6 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
|
4 |
*
|
5 |
* ====================================================================
|
6 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
* you may not use this file except in compliance with the License.
|
8 |
* You may obtain a copy of the License at
|
9 |
*
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
11 |
*
|
12 |
* Unless required by applicable law or agreed to in writing, software
|
13 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
* See the License for the specific language governing permissions and
|
16 |
* limitations under the License.
|
17 |
* ====================================================================
|
18 |
*/
|
19 |
package org.jclouds.gae; |
20 |
|
21 |
import static org.easymock.EasyMock.expect; |
22 |
import static org.easymock.classextension.EasyMock.createMock; |
23 |
import static org.easymock.classextension.EasyMock.createNiceMock; |
24 |
import static org.easymock.classextension.EasyMock.replay; |
25 |
import static org.testng.Assert.assertEquals; |
26 |
|
27 |
import java.io.File; |
28 |
import java.io.IOException; |
29 |
import java.net.MalformedURLException; |
30 |
import java.net.URI; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Date; |
33 |
import java.util.List; |
34 |
import java.util.concurrent.ExecutorService; |
35 |
|
36 |
import javax.ws.rs.HttpMethod; |
37 |
import javax.ws.rs.core.HttpHeaders; |
38 |
|
39 |
import org.jclouds.http.HttpRequest; |
40 |
import org.jclouds.http.HttpResponse; |
41 |
import org.jclouds.http.Payloads; |
42 |
import org.jclouds.http.handlers.DelegatingErrorHandler; |
43 |
import org.jclouds.http.handlers.DelegatingRetryHandler; |
44 |
import org.jclouds.http.internal.HttpWire; |
45 |
import org.jclouds.util.Utils; |
46 |
import org.testng.annotations.BeforeTest; |
47 |
import org.testng.annotations.Parameters; |
48 |
import org.testng.annotations.Test; |
49 |
|
50 |
import com.google.appengine.api.urlfetch.HTTPHeader; |
51 |
import com.google.appengine.api.urlfetch.HTTPRequest; |
52 |
import com.google.appengine.api.urlfetch.HTTPResponse; |
53 |
import com.google.appengine.api.urlfetch.URLFetchService; |
54 |
import com.google.appengine.repackaged.com.google.common.base.Charsets; |
55 |
import com.google.common.io.Files; |
56 |
|
57 |
/**
|
58 |
*
|
59 |
* @author Adrian Cole
|
60 |
*/
|
61 |
@Test
|
62 |
public class GaeHttpCommandExecutorServiceTest { |
63 |
GaeHttpCommandExecutorService client; |
64 |
URI endPoint;
|
65 |
|
66 |
@BeforeTest
|
67 |
void setupClient() throws MalformedURLException { |
68 |
endPoint = URI.create("http://localhost:80/foo"); |
69 |
client = new GaeHttpCommandExecutorService(createNiceMock(URLFetchService.class),
|
70 |
createNiceMock(ExecutorService.class), createNiceMock(DelegatingRetryHandler.class),
|
71 |
createNiceMock(DelegatingErrorHandler.class), createNiceMock(HttpWire.class)); |
72 |
} |
73 |
|
74 |
@Test
|
75 |
void testConvertHostHeaderToEndPoint() {
|
76 |
// TODO
|
77 |
} |
78 |
|
79 |
@Test
|
80 |
void testConvertWithHeaders() throws IOException { |
81 |
HTTPResponse gaeResponse = createMock(HTTPResponse.class); |
82 |
expect(gaeResponse.getResponseCode()).andReturn(200);
|
83 |
List<HTTPHeader> headers = new ArrayList<HTTPHeader>(); |
84 |
headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); |
85 |
expect(gaeResponse.getHeaders()).andReturn(headers); |
86 |
expect(gaeResponse.getContent()).andReturn(null).atLeastOnce();
|
87 |
replay(gaeResponse); |
88 |
HttpResponse response = client.convert(gaeResponse); |
89 |
assertEquals(response.getStatusCode(), 200);
|
90 |
assertEquals(response.getContent(), null);
|
91 |
assertEquals(response.getHeaders().size(), 1);
|
92 |
assertEquals(response.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE), "text/xml");
|
93 |
} |
94 |
|
95 |
@Test
|
96 |
void testConvertWithContent() throws IOException { |
97 |
HTTPResponse gaeResponse = createMock(HTTPResponse.class); |
98 |
expect(gaeResponse.getResponseCode()).andReturn(200);
|
99 |
List<HTTPHeader> headers = new ArrayList<HTTPHeader>(); |
100 |
headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); |
101 |
expect(gaeResponse.getHeaders()).andReturn(headers); |
102 |
expect(gaeResponse.getContent()).andReturn("hello".getBytes()).atLeastOnce();
|
103 |
replay(gaeResponse); |
104 |
HttpResponse response = client.convert(gaeResponse); |
105 |
assertEquals(response.getStatusCode(), 200);
|
106 |
assertEquals(Utils.toStringAndClose(response.getContent()), "hello");
|
107 |
assertEquals(response.getHeaders().size(), 1);
|
108 |
assertEquals(response.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE), "text/xml");
|
109 |
} |
110 |
|
111 |
@Test
|
112 |
void testConvertRequestGetsTargetAndUri() throws IOException { |
113 |
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
|
114 |
HTTPRequest gaeRequest = client.convert(request); |
115 |
assertEquals(gaeRequest.getURL().getPath(), "/foo");
|
116 |
} |
117 |
|
118 |
@Test
|
119 |
void testConvertRequestSetsFetchOptions() throws IOException { |
120 |
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
|
121 |
HTTPRequest gaeRequest = client.convert(request); |
122 |
assert gaeRequest.getFetchOptions() != null; |
123 |
} |
124 |
|
125 |
@Test
|
126 |
void testConvertRequestSetsHeaders() throws IOException { |
127 |
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
|
128 |
request.getHeaders().put("foo", "bar"); |
129 |
HTTPRequest gaeRequest = client.convert(request); |
130 |
assertEquals(gaeRequest.getHeaders().get(0).getName(), "foo"); |
131 |
assertEquals(gaeRequest.getHeaders().get(0).getValue(), "bar"); |
132 |
} |
133 |
|
134 |
@Test
|
135 |
void testConvertRequestNoContent() throws IOException { |
136 |
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
|
137 |
HTTPRequest gaeRequest = client.convert(request); |
138 |
assert gaeRequest.getPayload() == null; |
139 |
assertEquals(gaeRequest.getHeaders().size(), 2);// content length, user agent |
140 |
assertEquals(gaeRequest.getHeaders().get(0).getName(), HttpHeaders.USER_AGENT);
|
141 |
assertEquals(gaeRequest.getHeaders().get(0).getValue(), "jclouds/1.0 urlfetch/1.3.0"); |
142 |
} |
143 |
|
144 |
@Test
|
145 |
void testConvertRequestStringContent() throws IOException { |
146 |
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
|
147 |
request.setPayload("hoot!");
|
148 |
testHoot(request); |
149 |
} |
150 |
|
151 |
@Test
|
152 |
void testConvertRequestInputStreamContent() throws IOException { |
153 |
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
|
154 |
request.setPayload(Utils.toInputStream("hoot!"));
|
155 |
testHoot(request); |
156 |
} |
157 |
|
158 |
@Test
|
159 |
void testConvertRequestBytesContent() throws IOException { |
160 |
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
|
161 |
request.setPayload("hoot!".getBytes());
|
162 |
testHoot(request); |
163 |
} |
164 |
|
165 |
@Test(expectedExceptions = UnsupportedOperationException.class) |
166 |
void testConvertRequestBadContent() throws IOException { |
167 |
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
|
168 |
request.setPayload(Payloads.newPayload(new Date())); |
169 |
client.convert(request); |
170 |
} |
171 |
|
172 |
@Test
|
173 |
@Parameters("basedir") |
174 |
void testConvertRequestFileContent(String basedir) throws IOException { |
175 |
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
|
176 |
File file = new File(basedir, "target/testfiles/hoot"); |
177 |
file.getParentFile().mkdirs(); |
178 |
Files.write("hoot!".getBytes(Charsets.UTF_8), file);
|
179 |
request.setPayload(file); |
180 |
testHoot(request); |
181 |
} |
182 |
|
183 |
private void testHoot(HttpRequest request) throws IOException { |
184 |
request.getHeaders().put(HttpHeaders.CONTENT_TYPE, "text/plain");
|
185 |
HTTPRequest gaeRequest = client.convert(request); |
186 |
try {
|
187 |
assertEquals(gaeRequest.getHeaders().get(0).getName(), HttpHeaders.CONTENT_TYPE);
|
188 |
assertEquals(gaeRequest.getHeaders().get(0).getValue(), "text/plain"); |
189 |
} catch (AssertionError e) { |
190 |
assertEquals(gaeRequest.getHeaders().get(1).getName(), HttpHeaders.CONTENT_TYPE);
|
191 |
assertEquals(gaeRequest.getHeaders().get(1).getValue(), "text/plain"); |
192 |
} |
193 |
assertEquals(new String(gaeRequest.getPayload()), "hoot!"); |
194 |
} |
195 |
} |