-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHttpUtilityTests.java
More file actions
118 lines (107 loc) · 4.6 KB
/
HttpUtilityTests.java
File metadata and controls
118 lines (107 loc) · 4.6 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.skyflow.utils;
import com.google.gson.JsonObject;
import com.skyflow.errors.SkyflowException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
public class HttpUtilityTests {
private static String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";
@Mock
OutputStream outputStream;
private String expected;
private String expectedError;
private URL url;
private HttpURLConnection mockConnection;
@Before
public void setup() throws IOException {
expected = "{\"status\":\"success\"}";
expectedError = "{\"error\":{\"grpc_code\":123,\"http_code\":500,\"message\":\"something went wrong\",\"http_status\":\"internal server error\",\"details\":[]}}\n";
mockConnection = Mockito.mock(HttpURLConnection.class);
given(mockConnection.getInputStream()).willReturn(new ByteArrayInputStream(expected.getBytes()));
given(mockConnection.getErrorStream()).willReturn(new ByteArrayInputStream(expectedError.getBytes()));
given(mockConnection.getOutputStream()).willReturn(outputStream);
given(mockConnection.getResponseCode()).willReturn(200);
given(mockConnection.getHeaderField(anyString())).willReturn("id");
final URLStreamHandler handler = new URLStreamHandler() {
@Override
protected URLConnection openConnection(final URL arg0) throws IOException {
return mockConnection;
}
};
url = new URL("https://google.com", "google.com", 80, "", handler);
}
@Test
public void testSendRequest() {
try {
given(mockConnection.getRequestProperty("content-type")).willReturn("application/json");
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "application/json");
JsonObject params = new JsonObject();
params.addProperty("key", "value");
String response = HttpUtility.sendRequest("GET", url, params, headers);
Assert.assertEquals(expected, response);
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}
@Test
public void testSendRequestFormData() {
try {
given(mockConnection.getRequestProperty("content-type")).willReturn("multipart/form-data");
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "multipart/form-data");
JsonObject params = new JsonObject();
params.addProperty("key", "value");
String response = HttpUtility.sendRequest("GET", url, params, headers);
Assert.assertEquals(expected, response);
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}
@Test
public void testSendRequestFormURLEncoded() {
try {
given(mockConnection.getRequestProperty("content-type")).willReturn("application/x-www-form-urlencoded");
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "application/x-www-form-urlencoded");
JsonObject params = new JsonObject();
params.addProperty("key", "value");
String response = HttpUtility.sendRequest("GET", url, params, headers);
Assert.assertEquals(expected, response);
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}
@Test
public void testSendRequestError() {
try {
given(mockConnection.getResponseCode()).willReturn(500);
String response = HttpUtility.sendRequest("GET", url, null, null);
} catch (SkyflowException e) {
Assert.assertEquals(500, e.getHttpCode());
Assert.assertEquals(new Integer(123), e.getGrpcCode());
Assert.assertEquals("internal server error", e.getHttpStatus());
Assert.assertEquals("something went wrong", e.getMessage());
Assert.assertTrue(e.getDetails().isEmpty());
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}
}