PageRenderTime 297ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/twilio/http/RequestTest.java

http://github.com/twilio/twilio-java
Java | 320 lines | 271 code | 49 blank | 0 comment | 0 complexity | de7900d639cd8137538c6b58ee8155f2 MD5 | raw file
Possible License(s): MIT
  1. package com.twilio.http;
  2. import com.twilio.exception.ApiException;
  3. import com.twilio.rest.Domains;
  4. import java.time.ZonedDateTime;
  5. import java.time.ZoneId;
  6. import java.time.ZoneOffset;
  7. import java.time.LocalDate;
  8. import org.junit.Test;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import static com.twilio.Assert.assertQueryStringsEqual;
  12. import static com.twilio.Assert.assertUrlsEqual;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertNotEquals;
  15. import static org.junit.Assert.assertFalse;
  16. import static org.junit.Assert.assertNotNull;
  17. import static org.junit.Assert.assertTrue;
  18. import static org.junit.Assert.fail;
  19. import static org.junit.Assert.assertThrows;
  20. import static org.junit.Assert.assertThat;
  21. public class RequestTest {
  22. @Test
  23. public void testConstructorWithDomain() {
  24. Request request = new Request(HttpMethod.GET, Domains.FLEXAPI.toString(), "/v1/uri");
  25. assertNotNull(request);
  26. assertEquals(HttpMethod.GET, request.getMethod());
  27. assertEquals("https://flex-api.twilio.com/v1/uri", request.getUrl());
  28. }
  29. @Test
  30. public void testConstructURL() throws MalformedURLException {
  31. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  32. URL url = r.constructURL();
  33. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar");
  34. assertUrlsEqual(expected, url);
  35. }
  36. @Test(expected = ApiException.class)
  37. public void testConstructURLURISyntaxException() {
  38. Request request = new Request(HttpMethod.DELETE, "http://{");
  39. request.constructURL();
  40. fail("ApiException was expected");
  41. }
  42. @Test
  43. public void testConstructURLURISyntaxExceptionContent() {
  44. Request request = new Request(HttpMethod.DELETE, "http://{");
  45. ApiException e = assertThrows(ApiException.class, request::constructURL);
  46. assertEquals("Bad URI: Illegal character in authority at index 7: http://{", e.getMessage());
  47. }
  48. @Test
  49. public void testConstructURLMalformedExceptionContent(){
  50. Request request = new Request(HttpMethod.DELETE, "/2010-04-01/foo<>");
  51. ApiException e = assertThrows(ApiException.class, request::constructURL);
  52. assertEquals("Bad URL: no protocol: /2010-04-01/foo<>", e.getMessage());
  53. }
  54. @Test
  55. public void testConstructURLWithPipe() throws MalformedURLException {
  56. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foo|bar");
  57. URL url = r.constructURL();
  58. URL expected = new URL("https://api.twilio.com/2010-04-01/foo%7Cbar");
  59. assertUrlsEqual(expected, url);
  60. }
  61. @Test
  62. public void testConstructURLWithMultipleSlashes() throws MalformedURLException {
  63. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foo|bar/bar|foo");
  64. URL url = r.constructURL();
  65. URL expected = new URL("https://api.twilio.com/2010-04-01/foo%7Cbar/bar%7Cfoo");
  66. assertUrlsEqual(expected, url);
  67. }
  68. @Test
  69. public void testConstructURLWithCredentials() throws MalformedURLException {
  70. Request r = new Request(HttpMethod.GET, "user:pass@" + Domains.API.toString(), "/2010-04-01/foobar");
  71. URL url = r.constructURL();
  72. URL expected = new URL("https://user:pass@api.twilio.com/2010-04-01/foobar");
  73. assertUrlsEqual(expected, url);
  74. }
  75. @Test
  76. public void testConstructURLWithParam() throws MalformedURLException {
  77. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  78. r.addQueryParam("baz", "quux");
  79. URL url = r.constructURL();
  80. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz=quux");
  81. assertUrlsEqual(expected, url);
  82. }
  83. @Test
  84. public void testConstructURLWithParams() throws MalformedURLException {
  85. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  86. r.addQueryParam("baz", "quux");
  87. r.addQueryParam("garply", "xyzzy");
  88. URL url = r.constructURL();
  89. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz=quux&garply=xyzzy");
  90. assertUrlsEqual(expected, url);
  91. }
  92. @Test
  93. public void testConstructURLWithPlusPrefix() {
  94. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  95. r.addQueryParam("To", "+18888888888");
  96. URL url = r.constructURL();
  97. String expected = "https://api.twilio.com/2010-04-01/foobar?To=%2B18888888888";
  98. assertEquals(expected, url.toString());
  99. }
  100. @Test
  101. public void testConstructURLWithMultivaluedParam() throws MalformedURLException {
  102. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  103. r.addQueryParam("baz", "quux");
  104. r.addQueryParam("baz", "xyzzy");
  105. URL url = r.constructURL();
  106. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz=quux&baz=xyzzy");
  107. assertUrlsEqual(expected, url);
  108. }
  109. @Test
  110. public void testConstructURLWithInequalityParam() throws MalformedURLException {
  111. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  112. r.addQueryParam("baz>", "3");
  113. URL url = r.constructURL();
  114. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz>=3");
  115. assertUrlsEqual(expected, url);
  116. }
  117. @Test
  118. public void testAddQueryDateRangeLowerBound() throws MalformedURLException {
  119. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  120. r.addQueryDateRange("baz", LocalDate.of(2014, 1, 1), null);
  121. URL url = r.constructURL();
  122. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz>=2014-01-01");
  123. assertUrlsEqual(expected, url);
  124. }
  125. @Test
  126. public void testAddQueryDateRangeUpperBound() throws MalformedURLException {
  127. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  128. r.addQueryDateRange("baz", null, LocalDate.of(2014, 1, 1));
  129. URL url = r.constructURL();
  130. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz<=2014-01-01");
  131. assertUrlsEqual(expected, url);
  132. }
  133. @Test
  134. public void testAddQueryDateRangeClosed() throws MalformedURLException {
  135. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  136. r.addQueryDateRange("baz", LocalDate.of(2014, 1, 10), LocalDate.of(2014, 6, 1));
  137. URL url = r.constructURL();
  138. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz>=2014-01-10&baz<=2014-06-01");
  139. assertUrlsEqual(expected, url);
  140. }
  141. @Test
  142. public void testAddQueryDateRangeMismatchedBounds() throws MalformedURLException {
  143. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  144. LocalDate wrongLowerBound = LocalDate.of(2020, 6, 1);
  145. LocalDate wrongUpperBound = LocalDate.of(2014, 1, 10);
  146. r.addQueryDateRange("baz", wrongLowerBound, wrongUpperBound);
  147. URL url = r.constructURL();
  148. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz>=2014-01-10&baz<=2020-06-01");
  149. assertNotEquals(expected, url);
  150. }
  151. @Test
  152. public void testAddQueryDateTimeRangeLowerBound() throws MalformedURLException {
  153. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  154. r.addQueryDateTimeRange("baz", ZonedDateTime.of(2014, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC), null);
  155. URL url = r.constructURL();
  156. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz>=2014-01-01T00:00:00");
  157. assertUrlsEqual(expected, url);
  158. }
  159. @Test
  160. public void testAddQueryDateTimeRangeUpperBound() throws MalformedURLException {
  161. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  162. r.addQueryDateTimeRange("baz", null, ZonedDateTime.of(2014, 1, 1, 22, 0, 0, 0, ZoneOffset.UTC));
  163. URL url = r.constructURL();
  164. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz<=2014-01-01T22:00:00");
  165. assertUrlsEqual(expected, url);
  166. }
  167. @Test
  168. public void testAddQueryDateTimeRangeClosed() throws MalformedURLException {
  169. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  170. r.addQueryDateTimeRange("baz", ZonedDateTime.of(2014, 1, 10, 14, 0, 0, 0, ZoneOffset.UTC),
  171. ZonedDateTime.of(2014, 6, 1, 16, 0, 0, 0, ZoneOffset.UTC));
  172. URL url = r.constructURL();
  173. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz>=2014-01-10T14:00:00&baz<=2014-06-01T16:00:00");
  174. assertUrlsEqual(expected, url);
  175. }
  176. @Test
  177. public void testAddQueryDateTimeRangeClosedNotUTC() throws MalformedURLException {
  178. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  179. ZoneId z = ZoneId.of("America/Chicago");
  180. ZonedDateTime begin = ZonedDateTime.of(2014, 1, 10, 14, 0, 0, 0, z);
  181. ZonedDateTime end = ZonedDateTime.of(2014, 6, 1, 16, 0, 0, 0, z);
  182. r.addQueryDateTimeRange("baz", begin, end);
  183. URL url = r.constructURL();
  184. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz>=2014-01-10T20:00:00&baz<=2014-06-01T21:00:00");
  185. assertUrlsEqual(expected, url);
  186. }
  187. @Test
  188. public void testAddQueryDateTimeRangeMismatchedBounds() throws MalformedURLException {
  189. Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
  190. ZonedDateTime wrongLowerBound = ZonedDateTime.of(2020, 6, 1, 16, 0, 0, 0, ZoneOffset.UTC);
  191. ZonedDateTime wrongUpperBound = ZonedDateTime.of(2014, 1, 10, 14, 0, 0, 0, ZoneOffset.UTC);
  192. r.addQueryDateTimeRange("baz", wrongLowerBound, wrongUpperBound);
  193. URL url = r.constructURL();
  194. URL expected = new URL("https://api.twilio.com/2010-04-01/foobar?baz>=2014-01-10T14:00:00&baz<=2020-06-01T16:00:00");
  195. assertNotEquals(expected, url);
  196. }
  197. @Test
  198. public void testNoEdgeOrRegionInUrl() throws MalformedURLException {
  199. final Request request = new Request(HttpMethod.GET, "https://api.twilio.com");
  200. assertUrlsEqual(new URL("https://api.twilio.com"), request.constructURL());
  201. request.setRegion("region");
  202. assertUrlsEqual(new URL("https://api.region.twilio.com"), request.constructURL());
  203. request.setEdge("edge");
  204. assertUrlsEqual(new URL("https://api.edge.region.twilio.com"), request.constructURL());
  205. request.setRegion(null);
  206. assertUrlsEqual(new URL("https://api.edge.us1.twilio.com"), request.constructURL());
  207. }
  208. @Test
  209. public void testRegionInUrl() throws MalformedURLException {
  210. final Request request = new Request(HttpMethod.GET, "https://api.urlRegion.twilio.com");
  211. assertUrlsEqual(new URL("https://api.urlRegion.twilio.com"), request.constructURL());
  212. request.setRegion("region");
  213. assertUrlsEqual(new URL("https://api.region.twilio.com"), request.constructURL());
  214. request.setEdge("edge");
  215. assertUrlsEqual(new URL("https://api.edge.region.twilio.com"), request.constructURL());
  216. request.setRegion(null);
  217. assertUrlsEqual(new URL("https://api.edge.urlRegion.twilio.com"), request.constructURL());
  218. }
  219. @Test
  220. public void testRegionAndEdgeInUrl() throws MalformedURLException {
  221. final Request request = new Request(HttpMethod.GET, "https://api.urlEdge.urlRegion.twilio.com");
  222. assertUrlsEqual(new URL("https://api.urlEdge.urlRegion.twilio.com"), request.constructURL());
  223. request.setRegion("region");
  224. assertUrlsEqual(new URL("https://api.urlEdge.region.twilio.com"), request.constructURL());
  225. request.setEdge("edge");
  226. assertUrlsEqual(new URL("https://api.edge.region.twilio.com"), request.constructURL());
  227. request.setRegion(null);
  228. assertUrlsEqual(new URL("https://api.edge.urlRegion.twilio.com"), request.constructURL());
  229. }
  230. @Test
  231. public void testRegionInConstructor() {
  232. final Request request = new Request(HttpMethod.GET, Domains.ACCOUNTS.toString(), "/path/to/something.json?foo=12.34#bar", "region");
  233. assertUrlsEqual("https://accounts.region.twilio.com/path/to/something.json?foo=12.34#bar", request.constructURL());
  234. request.setEdge("edge");
  235. assertUrlsEqual("https://accounts.edge.region.twilio.com/path/to/something.json?foo=12.34#bar", request.constructURL());
  236. }
  237. @Test
  238. public void testEncodeFormBody() {
  239. Request r = new Request(HttpMethod.POST, "http://example.com/foobar");
  240. r.addPostParam("baz", "quux");
  241. r.addPostParam("garply", "xyzzy");
  242. String encoded = r.encodeFormBody();
  243. assertQueryStringsEqual("baz=quux&garply=xyzzy", encoded);
  244. }
  245. @Test
  246. public void testGetPassword() {
  247. Request request = new Request(HttpMethod.DELETE, "/uri");
  248. request.setAuth("username", "password");
  249. assertEquals("password", request.getPassword());
  250. }
  251. @Test
  252. public void testGetUsername() {
  253. Request request = new Request(HttpMethod.DELETE, "/uri");
  254. request.setAuth("username", "password");
  255. assertEquals("username", request.getUsername());
  256. }
  257. @Test
  258. public void testRequiresAuthentication() {
  259. Request request = new Request(HttpMethod.DELETE, "/uri");
  260. assertFalse(request.requiresAuthentication());
  261. request.setAuth("username", "password");
  262. assertTrue(request.requiresAuthentication());
  263. }
  264. @Test
  265. public void testEquals() {
  266. Request request = new Request(HttpMethod.DELETE, "/uri");
  267. request.setAuth("username", "password");
  268. assertNotEquals(request, new Object());
  269. assertNotEquals(null, request);
  270. }
  271. }