Fix compile issue of post order test example with latest http client version

This commit is contained in:
Johannes Geppert
2019-02-09 15:32:02 +01:00
parent 95866c347e
commit bbe46e92f0
@@ -18,15 +18,16 @@
*/
package it.org.apache.struts2.rest.example;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import net.sourceforge.jwebunit.junit.WebTestCase;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
public class PostOrderTest extends WebTestCase {
public void setUp() throws Exception {
@@ -72,68 +73,50 @@ public class PostOrderTest extends WebTestCase {
}
public void testPostOrderInXml() throws IOException {
HttpClient client = new HttpClient();
PostMethod method = null;
try {
method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.xml");
method.setRequestEntity(new StringRequestEntity("<org.apache.struts2.rest.example.Order>\n" +
"<clientName>Test3</clientName>\n" +
"<amount>3342</amount>\n" +
"</org.apache.struts2.rest.example.Order>"));
client.executeMethod(method);
assertEquals(201, method.getStatusCode());
assertTrue(method.getResponseHeader("Location").getValue().startsWith(ParameterUtils.getBaseUrl()+"/orders/"));
} finally {
method.releaseConnection();
}
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.xml");
httpPost.setEntity(new StringEntity("<org.apache.struts2.rest.example.Order>\n" +
"<clientName>Test3</clientName>\n" +
"<amount>3342</amount>\n" +
"</org.apache.struts2.rest.example.Order>"));
CloseableHttpResponse response = client.execute(httpPost);
assertEquals(201, response.getStatusLine().getStatusCode());
assertTrue(response.getHeaders("Location")[0].getValue().startsWith(ParameterUtils.getBaseUrl() + "/orders/"));
client.close();
}
public void testPostOrderInXmlWithBadData() throws IOException {
HttpClient client = new HttpClient();
PostMethod method = null;
try {
method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.xml");
method.setRequestEntity(new StringRequestEntity("<org.apache.struts2.rest.example.Order>\n" +
"<amount>3342</amount>\n" +
"</org.apache.struts2.rest.example.Order>"));
client.executeMethod(method);
assertEquals(400, method.getStatusCode());
String response = method.getResponseBodyAsString();
assertTrue(response.contains("<string>The client name is empty"));
assertNull(method.getResponseHeader("Location"));
} finally {
method.releaseConnection();
}
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.xml");
httpPost.setEntity(new StringEntity("<org.apache.struts2.rest.example.Order>\n" +
"<amount>3342</amount>\n" +
"</org.apache.struts2.rest.example.Order>"));
CloseableHttpResponse response = client.execute(httpPost);
assertEquals(400, response.getStatusLine().getStatusCode());
assertTrue(response.toString().contains("<string>The client name is empty"));
assertNull(response.getHeaders("Location"));
client.close();
}
public void testPostOrderInJson() throws IOException {
HttpClient client = new HttpClient();
PostMethod method = null;
try {
method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.json");
method.setRequestEntity(new StringRequestEntity("{\"amount\":33,\"clientName\":\"Test4\"}"));
client.executeMethod(method);
assertEquals(201, method.getStatusCode());
assertTrue(method.getResponseHeader("Location").getValue().startsWith(ParameterUtils.getBaseUrl()+"/orders/"));
} finally {
method.releaseConnection();
}
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.json");
httpPost.setEntity(new StringEntity("{\"amount\":33,\"clientName\":\"Test4\"}"));
CloseableHttpResponse response = client.execute(httpPost);
assertEquals(201, response.getStatusLine().getStatusCode());
assertTrue(response.getHeaders("Location")[0].getValue().startsWith(ParameterUtils.getBaseUrl() + "/orders/"));
client.close();
}
public void testPostOrderInJsonWithBadData() throws IOException {
HttpClient client = new HttpClient();
PostMethod method = null;
try {
method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.json");
method.setRequestEntity(new StringRequestEntity("{\"amount\":33}"));
client.executeMethod(method);
String response = method.getResponseBodyAsString();
assertEquals(400, method.getStatusCode());
assertEquals("{\"actionErrors\":[],\"fieldErrors\":{\"clientName\":[\"The client name is empty\"]}}", response);
assertNull(method.getResponseHeader("Location"));
} finally {
method.releaseConnection();
}
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.json");
httpPost.setEntity(new StringEntity("{\"amount\":33}"));
CloseableHttpResponse response = client.execute(httpPost);
assertEquals(400, response.getStatusLine().getStatusCode());
assertTrue(response.toString()
.contains("{\"actionErrors\":[],\"fieldErrors\":{\"clientName\":[\"The client name is empty\"]}}"));
assertNull(response.getHeaders("Location"));
client.close();
}
}