* java http request

* httpclient code

* small fixes

* remove try catch
This commit is contained in:
lor6
2017-04-25 05:24:41 +03:00
committed by KevinGilmore
parent 4eb3f44b14
commit c10f709e17
6 changed files with 317 additions and 0 deletions
@@ -0,0 +1,40 @@
package com.baeldung.http;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import java.io.IOException;
public class HttpRequestBuilderTest {
private HttpRequestBuilder requestPerformer;
@Before
public void setup() {
requestPerformer = new HttpRequestBuilder();
}
@Test
public void whenGetRequest_thenOk() throws IOException {
HttpResponseWrapper response = requestPerformer.sendRequest("http://www.example.com", "GET", null, null);
assertEquals("status code incorrect", response.getStatus(), 200);
assertTrue("content incorrect", response.getContent()
.contains("Example Domain"));
}
@Test
public void whenPostRequest_thenOk() throws IOException {
Map<String, String> parameters = new HashMap<>();
parameters.put("param1", "val");
Map<String, String> properties = new HashMap<>();
properties.put("Content-Type", "application/json");
HttpResponseWrapper response = requestPerformer.sendRequest("http://www.example.com", "POST", parameters, properties);
assertEquals("status code incorrect", response.getStatus(), 200);
}
}
@@ -0,0 +1,46 @@
package com.baeldung.httpclient;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.http.HttpResponseWrapper;
public class HttpClientRequestBuilderTest {
private HttpClientRequestBuilder requestBuilder;
@Before
public void setup() {
requestBuilder = new HttpClientRequestBuilder();
}
@Test
public void whenGetRequest_thenOk() {
Map<String, String> parameters = new HashMap<>();
parameters.put("param1", "val");
HttpResponseWrapper response = requestBuilder.sendGetRequest("http://www.example.com",parameters);
assertEquals("status code incorrect", response.getStatus(), 200);
assertTrue("content incorrect", response.getContent()
.contains("Example Domain"));
}
@Test
public void whenPostRequestWithParameters_thenOk() {
Map<String, String> parameters = new HashMap<>();
parameters.put("param1", "val");
HttpResponseWrapper response = requestBuilder.sendPostRequestWithParameters("http://www.example.com", parameters);
assertEquals("status code incorrect", response.getStatus(), 200);
}
@Test
public void whenPostRequestWithJson_thenOk() {
String json = "{\"id\":\"1\"}";
HttpResponseWrapper response = requestBuilder.sendPostRequestWithJson("http://www.example.com",json);
assertEquals("status code incorrect", response.getStatus(), 200);
}
}