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 package org.apache.http.client.fluent;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.nio.charset.Charset;
32
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpEntityEnclosingRequest;
35 import org.apache.http.HttpException;
36 import org.apache.http.HttpHost;
37 import org.apache.http.HttpRequest;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.client.ClientProtocolException;
40 import org.apache.http.client.ResponseHandler;
41 import org.apache.http.entity.ContentType;
42 import org.apache.http.entity.StringEntity;
43 import org.apache.http.localserver.LocalServerTestBase;
44 import org.apache.http.protocol.HttpContext;
45 import org.apache.http.protocol.HttpRequestHandler;
46 import org.apache.http.util.EntityUtils;
47 import org.junit.After;
48 import org.junit.Assert;
49 import org.junit.Before;
50 import org.junit.Test;
51
52 public class TestFluent extends LocalServerTestBase {
53
54 @Before @Override
55 public void setUp() throws Exception {
56 super.setUp();
57 this.serverBootstrap.registerHandler("/", new HttpRequestHandler() {
58
59 @Override
60 public void handle(
61 final HttpRequest request,
62 final HttpResponse response,
63 final HttpContext context) throws HttpException, IOException {
64 response.setEntity(new StringEntity("All is well", ContentType.TEXT_PLAIN));
65 }
66
67 });
68 this.serverBootstrap.registerHandler("/echo", new HttpRequestHandler() {
69
70 @Override
71 public void handle(
72 final HttpRequest request,
73 final HttpResponse response,
74 final HttpContext context) throws HttpException, IOException {
75 HttpEntity responseEntity = null;
76 if (request instanceof HttpEntityEnclosingRequest) {
77 final HttpEntity requestEntity = ((HttpEntityEnclosingRequest) request).getEntity();
78 if (requestEntity != null) {
79 final ContentType contentType = ContentType.getOrDefault(requestEntity);
80 if (ContentType.TEXT_PLAIN.getMimeType().equals(contentType.getMimeType())) {
81 responseEntity = new StringEntity(
82 EntityUtils.toString(requestEntity), ContentType.TEXT_PLAIN);
83 }
84 }
85 }
86 if (responseEntity == null) {
87 responseEntity = new StringEntity("echo", ContentType.TEXT_PLAIN);
88 }
89 response.setEntity(responseEntity);
90 }
91
92 });
93 }
94
95 @After @Override
96 public void shutDown() throws Exception {
97 Executor.closeIdleConnections();
98 super.shutDown();
99 }
100
101 @Test
102 public void testGetRequest() throws Exception {
103 final HttpHost target = start();
104 final String baseURL = "http://localhost:" + target.getPort();
105 final String message = Request.Get(baseURL + "/").execute().returnContent().asString();
106 Assert.assertEquals("All is well", message);
107 }
108
109 @Test(expected = ClientProtocolException.class)
110 public void testGetRequestFailure() throws Exception {
111 final HttpHost target = start();
112 final String baseURL = "http://localhost:" + target.getPort();
113 Request.Get(baseURL + "/boom").execute().returnContent().asString();
114 }
115
116 @Test
117 public void testPostRequest() throws Exception {
118 final HttpHost target = start();
119 final String baseURL = "http://localhost:" + target.getPort();
120 final String message1 = Request.Post(baseURL + "/echo")
121 .bodyString("what is up?", ContentType.TEXT_PLAIN)
122 .execute().returnContent().asString();
123 Assert.assertEquals("what is up?", message1);
124 final String message2 = Request.Post(baseURL + "/echo")
125 .bodyByteArray(new byte[]{1, 2, 3}, ContentType.APPLICATION_OCTET_STREAM)
126 .execute().returnContent().asString();
127 Assert.assertEquals("echo", message2);
128 }
129
130 @Test
131 public void testContentAsStringWithCharset() throws Exception {
132 final HttpHost target = start();
133 final String baseURL = "http://localhost:" + target.getPort();
134 final Content content = Request.Post(baseURL + "/echo").bodyByteArray("Ü".getBytes("utf-8")).execute()
135 .returnContent();
136 Assert.assertEquals((byte)-61, content.asBytes()[0]);
137 Assert.assertEquals((byte)-100, content.asBytes()[1]);
138 Assert.assertEquals("Ü", content.asString(Charset.forName("utf-8")));
139 }
140
141 @Test
142 public void testConnectionRelease() throws Exception {
143 final HttpHost target = start();
144 final String baseURL = "http://localhost:" + target.getPort();
145 for (int i = 0; i < 20; i++) {
146 Request.Get(baseURL + "/").execute().returnContent();
147 Request.Get(baseURL + "/").execute().returnResponse();
148 Request.Get(baseURL + "/").execute().discardContent();
149 Request.Get(baseURL + "/").execute().handleResponse(new ResponseHandler<Object>() {
150
151 @Override
152 public Object handleResponse(
153 final HttpResponse response) throws IOException {
154 return null;
155 }
156
157 });
158 final File tmpFile = File.createTempFile("test", ".bin");
159 try {
160 Request.Get(baseURL + "/").execute().saveContent(tmpFile);
161 } finally {
162 tmpFile.delete();
163 }
164 }
165 }
166
167 }