Skip to content

Instantly share code, notes, and snippets.

@melekes melekes/HttpClient.java Secret
Last active Aug 29, 2015

Embed
What would you like to do?
Testing HTTP client in Java
package com.company.http_client;
import java.io.*;
import java.lang.String;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.HttpURLConnection;
public final class HttpClient {
private String baseUrl;
public HttpClient(String baseUrl) {
this.baseUrl = baseUrl;
}
public JSONObject search(String term) throws UnsupportedEncodingException {
String charset = java.nio.charset.StandardCharsets.UTF_8.name();
String query = String.format("term=%s", URLEncoder.encode(term, charset));
try {
URL url = new URL(baseUrl + "/companies/search?" + query);
System.out.println("Sending GET " + url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
int responseCode = conn.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
System.out.println("Received response with code " + responseCode);
return null;
}
BufferedReader streamReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr);
}
conn.disconnect();
String responseBody = responseStrBuilder.toString();
System.out.println("Received body " + responseBody);
return new JSONObject(responseBody);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
package com.company.http_client;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.json.JSONObject;
@RunWith(PowerMockRunner.class)
@PrepareForTest({HttpClient.class, URL.class})
public class HttpClientTest {
@Test
public void testSearchReturnsJSONObject() throws Exception {
URL url = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withArguments(Mockito.anyString()).thenReturn(url);
HttpURLConnection connection = PowerMockito.mock(HttpURLConnection.class);
PowerMockito.when(url.openConnection()).thenReturn(connection);
PowerMockito.when(connection.getInputStream()).thenReturn(getClass().getResourceAsStream("/http_client/search.json"));
PowerMockito.when(connection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
HttpClient client = new HttpClient("http://localhost:8000");
JSONObject company = client.search("Google");
Assert.assertNotNull(company);
Assert.assertEquals("Google Inc.", company.getString("name"));
}
@Test
public void testSearchReturnsNullIfNothingWasFound() throws Exception {
URL url = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withArguments(Mockito.anyString()).thenReturn(url);
HttpURLConnection connection = PowerMockito.mock(HttpURLConnection.class);
PowerMockito.when(url.openConnection()).thenReturn(connection);
PowerMockito.when(connection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_NOT_FOUND);
HttpClient client = new HttpClient("http://localhost:8000");
JSONObject company = client.search("Ggggle");
Assert.assertNull(company);
}
}
{
"name": "Google Inc.",
"founded": "September 4, 1998, Menlo Park, California, United States",
"ceo": "Larry Page",
"founders": "Larry Page, Sergey Brin"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.