PageRenderTime 276ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/jaxrs/src/test/java/io/airlift/jaxrs/TestOverrideMethodFilterInHttpServer.java

https://gitlab.com/vectorci/airlift
Java | 290 lines | 230 code | 45 blank | 15 comment | 2 complexity | 04fb3a5d8e0632448a637d587b7f5042 MD5 | raw file
  1. /*
  2. * Copyright 2010 Proofpoint, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.airlift.jaxrs;
  17. import com.google.inject.Binder;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Module;
  20. import io.airlift.http.client.HttpClient;
  21. import io.airlift.http.client.Request;
  22. import io.airlift.http.client.StatusResponseHandler.StatusResponse;
  23. import io.airlift.http.client.jetty.JettyHttpClient;
  24. import io.airlift.http.server.testing.TestingHttpServer;
  25. import io.airlift.http.server.testing.TestingHttpServerModule;
  26. import io.airlift.json.JsonModule;
  27. import io.airlift.node.testing.TestingNodeModule;
  28. import io.airlift.testing.Closeables;
  29. import org.testng.annotations.AfterMethod;
  30. import org.testng.annotations.BeforeMethod;
  31. import org.testng.annotations.Test;
  32. import javax.ws.rs.DELETE;
  33. import javax.ws.rs.GET;
  34. import javax.ws.rs.POST;
  35. import javax.ws.rs.PUT;
  36. import javax.ws.rs.Path;
  37. import javax.ws.rs.core.Response.Status;
  38. import java.io.IOException;
  39. import java.util.concurrent.ExecutionException;
  40. import static io.airlift.http.client.StatusResponseHandler.createStatusResponseHandler;
  41. import static io.airlift.jaxrs.JaxrsBinder.jaxrsBinder;
  42. import static java.lang.String.format;
  43. import static org.testng.Assert.assertEquals;
  44. import static org.testng.Assert.assertFalse;
  45. import static org.testng.Assert.assertTrue;
  46. @Test(singleThreaded = true)
  47. public class TestOverrideMethodFilterInHttpServer
  48. {
  49. private static final String GET = "GET";
  50. private static final String POST = "POST";
  51. private static final String PUT = "PUT";
  52. private static final String DELETE = "DELETE";
  53. private TestingHttpServer server;
  54. private TestResource resource;
  55. private HttpClient client;
  56. @BeforeMethod
  57. public void setup()
  58. throws Exception
  59. {
  60. resource = new TestResource();
  61. server = createServer(resource);
  62. client = new JettyHttpClient();
  63. server.start();
  64. }
  65. @AfterMethod
  66. public void teardown()
  67. throws Exception
  68. {
  69. try {
  70. if (server != null) {
  71. server.stop();
  72. }
  73. }
  74. catch (Throwable ignored) {
  75. }
  76. Closeables.closeQuietly(client);
  77. }
  78. @Test
  79. public void testDeleteViaQueryParam()
  80. throws Exception
  81. {
  82. client.execute(buildRequestWithQueryParam(POST, DELETE), createStatusResponseHandler());
  83. assertFalse(resource.postCalled(), "POST");
  84. assertTrue(resource.deleteCalled(), "DELETE");
  85. assertFalse(resource.putCalled(), "PUT");
  86. assertFalse(resource.getCalled(), "GET");
  87. }
  88. @Test
  89. public void testPutViaQueryParam()
  90. throws Exception
  91. {
  92. client.execute(buildRequestWithQueryParam(POST, PUT), createStatusResponseHandler());
  93. assertFalse(resource.postCalled(), "POST");
  94. assertFalse(resource.deleteCalled(), "DELETE");
  95. assertTrue(resource.putCalled(), "PUT");
  96. assertFalse(resource.getCalled(), "GET");
  97. }
  98. @Test
  99. public void testPostViaQueryParam()
  100. throws Exception
  101. {
  102. client.execute(buildRequestWithQueryParam(POST, POST), createStatusResponseHandler());
  103. assertTrue(resource.postCalled(), "POST");
  104. assertFalse(resource.deleteCalled(), "DELETE");
  105. assertFalse(resource.putCalled(), "PUT");
  106. assertFalse(resource.getCalled(), "GET");
  107. }
  108. @Test
  109. public void testDeleteViaHeader()
  110. throws Exception
  111. {
  112. client.execute(buildRequestWithHeader(POST, DELETE), createStatusResponseHandler());
  113. assertFalse(resource.postCalled(), "POST");
  114. assertTrue(resource.deleteCalled(), "DELETE");
  115. assertFalse(resource.putCalled(), "PUT");
  116. assertFalse(resource.getCalled(), "GET");
  117. }
  118. @Test
  119. public void testPutViaHeader()
  120. throws Exception
  121. {
  122. client.execute(buildRequestWithHeader(POST, PUT), createStatusResponseHandler());
  123. assertFalse(resource.postCalled(), "POST");
  124. assertFalse(resource.deleteCalled(), "DELETE");
  125. assertTrue(resource.putCalled(), "PUT");
  126. assertFalse(resource.getCalled(), "GET");
  127. }
  128. @Test
  129. public void testPostViaHeader()
  130. throws Exception
  131. {
  132. client.execute(buildRequestWithHeader(POST, POST), createStatusResponseHandler());
  133. assertTrue(resource.postCalled(), "POST");
  134. assertFalse(resource.deleteCalled(), "DELETE");
  135. assertFalse(resource.putCalled(), "PUT");
  136. assertFalse(resource.getCalled(), "GET");
  137. }
  138. private void assertNonOverridableMethod(Request request)
  139. throws IOException, ExecutionException, InterruptedException
  140. {
  141. StatusResponse response = client.execute(request, createStatusResponseHandler());
  142. assertEquals(response.getStatusCode(), Status.BAD_REQUEST.getStatusCode());
  143. assertFalse(resource.postCalled(), "POST");
  144. assertFalse(resource.deleteCalled(), "DELETE");
  145. assertFalse(resource.putCalled(), "PUT");
  146. assertFalse(resource.getCalled(), "GET");
  147. }
  148. private Request buildRequestWithHeader(String type, String override)
  149. {
  150. return Request.builder().setUri(server.getBaseUrl()).setMethod(type).addHeader("X-HTTP-Method-Override", override).build();
  151. }
  152. private Request buildRequestWithQueryParam(String type, String override)
  153. {
  154. return Request.builder().setUri(server.getBaseUrl().resolve(format("/?_method=%s", override))).setMethod(type).build();
  155. }
  156. @Test
  157. public void testNonOverridableMethodsWithHeader()
  158. throws IOException, ExecutionException, InterruptedException
  159. {
  160. assertNonOverridableMethod(buildRequestWithHeader(GET, POST));
  161. assertNonOverridableMethod(buildRequestWithHeader(GET, DELETE));
  162. assertNonOverridableMethod(buildRequestWithHeader(GET, PUT));
  163. assertNonOverridableMethod(buildRequestWithHeader(DELETE, POST));
  164. assertNonOverridableMethod(buildRequestWithHeader(DELETE, GET));
  165. assertNonOverridableMethod(buildRequestWithHeader(DELETE, PUT));
  166. assertNonOverridableMethod(buildRequestWithHeader(PUT, POST));
  167. assertNonOverridableMethod(buildRequestWithHeader(PUT, DELETE));
  168. assertNonOverridableMethod(buildRequestWithHeader(PUT, GET));
  169. }
  170. @Test
  171. public void testNonOverridableMethodsWithQueryParam()
  172. throws IOException, ExecutionException, InterruptedException
  173. {
  174. assertNonOverridableMethod(buildRequestWithQueryParam(GET, POST));
  175. assertNonOverridableMethod(buildRequestWithQueryParam(GET, DELETE));
  176. assertNonOverridableMethod(buildRequestWithQueryParam(GET, PUT));
  177. assertNonOverridableMethod(buildRequestWithQueryParam(DELETE, POST));
  178. assertNonOverridableMethod(buildRequestWithQueryParam(DELETE, GET));
  179. assertNonOverridableMethod(buildRequestWithQueryParam(DELETE, PUT));
  180. assertNonOverridableMethod(buildRequestWithQueryParam(PUT, POST));
  181. assertNonOverridableMethod(buildRequestWithQueryParam(PUT, DELETE));
  182. assertNonOverridableMethod(buildRequestWithQueryParam(PUT, GET));
  183. }
  184. @Path("/")
  185. public static class TestResource
  186. {
  187. private volatile boolean post;
  188. private volatile boolean put;
  189. private volatile boolean get;
  190. private volatile boolean delete;
  191. @POST
  192. public void post()
  193. {
  194. post = true;
  195. }
  196. @GET
  197. public boolean get()
  198. {
  199. get = true;
  200. return true;
  201. }
  202. @DELETE
  203. public void delete()
  204. {
  205. delete = true;
  206. }
  207. @PUT
  208. public void put()
  209. {
  210. put = true;
  211. }
  212. public boolean postCalled()
  213. {
  214. return post;
  215. }
  216. public boolean putCalled()
  217. {
  218. return put;
  219. }
  220. public boolean getCalled()
  221. {
  222. return get;
  223. }
  224. public boolean deleteCalled()
  225. {
  226. return delete;
  227. }
  228. }
  229. private static TestingHttpServer createServer(final TestResource resource)
  230. {
  231. return Guice.createInjector(
  232. new TestingNodeModule(),
  233. new JaxrsModule(true),
  234. new JsonModule(),
  235. new TestingHttpServerModule(),
  236. new Module()
  237. {
  238. @Override
  239. public void configure(Binder binder)
  240. {
  241. jaxrsBinder(binder).bindInstance(resource);
  242. }
  243. }).getInstance(TestingHttpServer.class);
  244. }
  245. }