-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHttpUtilityTests.java
More file actions
194 lines (178 loc) · 7.91 KB
/
HttpUtilityTests.java
File metadata and controls
194 lines (178 loc) · 7.91 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
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(PowerMockRunner.class)
@PrepareForTest({URL.class, HttpURLConnection.class})
public class HttpUtilityTests {
private static String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";
@InjectMocks
HttpUtility httpUtility;
@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
@PrepareForTest({URL.class, HttpURLConnection.class})
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
@PrepareForTest({URL.class, HttpURLConnection.class})
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
@PrepareForTest({URL.class, HttpURLConnection.class})
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
@PrepareForTest({URL.class, HttpURLConnection.class})
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);
}
}
// New test cases for content-type handling changes
@Test
@PrepareForTest({URL.class, HttpURLConnection.class})
public void testSendRequestWithRawBody() {
try {
given(mockConnection.getRequestProperty("content-type")).willReturn("application/xml");
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "application/xml");
JsonObject params = new JsonObject();
params.addProperty("__raw_body__", "<xml>test</xml>");
String response = httpUtility.sendRequest("POST", url, params, headers);
Assert.assertEquals(expected, response);
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}
@Test
@PrepareForTest({URL.class, HttpURLConnection.class})
public void testSendRequestWithoutContentTypeHeader() {
try {
given(mockConnection.getRequestProperty("content-type")).willReturn("application/json");
JsonObject params = new JsonObject();
params.addProperty("key", "value");
String response = httpUtility.sendRequest("POST", url, params, null);
Assert.assertEquals(expected, response);
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}
@Test
@PrepareForTest({URL.class, HttpURLConnection.class})
public void testSendRequestWithNullRequestId() {
try {
given(mockConnection.getHeaderField(anyString())).willReturn(null);
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);
Assert.assertNotNull(HttpUtility.getRequestID());
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}
@Test
@PrepareForTest({URL.class, HttpURLConnection.class})
public void testSendRequestFormURLEncodedWithSpecialCharacters() {
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 with spaces");
params.addProperty("special", "test@email.com");
String response = httpUtility.sendRequest("POST", url, params, headers);
Assert.assertEquals(expected, response);
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}
}