/test/src/main/java/org/switchyard/test/mixins/HTTPMixIn.java

https://github.com/lincolnthree/switchyard-core · Java · 174 lines · 91 code · 16 blank · 67 comment · 5 complexity · 0347ce9553e6f14e0d29760a10c85676 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
  4. * as indicated by the @authors tag. All rights reserved.
  5. * See the copyright.txt in the distribution for a
  6. * full listing of individual contributors.
  7. *
  8. * This copyrighted material is made available to anyone wishing to use,
  9. * modify, copy, or redistribute it subject to the terms and conditions
  10. * of the GNU Lesser General Public License, v. 2.1.
  11. * This program is distributed in the hope that it will be useful, but WITHOUT A
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  14. * You should have received a copy of the GNU Lesser General Public License,
  15. * v.2.1 along with this distribution; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. package org.switchyard.test.mixins;
  20. import org.apache.commons.httpclient.HttpClient;
  21. import org.apache.commons.httpclient.HttpConnectionManager;
  22. import org.apache.commons.httpclient.HttpMethod;
  23. import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
  24. import org.apache.commons.httpclient.URIException;
  25. import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
  26. import org.apache.commons.httpclient.methods.PostMethod;
  27. import org.apache.commons.httpclient.methods.StringRequestEntity;
  28. import org.junit.Assert;
  29. import org.switchyard.test.SwitchYardTestKit;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.io.UnsupportedEncodingException;
  33. /**
  34. * HTTP Test Mix In.
  35. *
  36. * @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
  37. */
  38. public class HTTPMixIn extends AbstractTestMixIn {
  39. private HttpClient _httpClient;
  40. private String _contentType = "text/xml";
  41. /**
  42. * Set the content type.
  43. * <p/>
  44. * Default content type is "text/xml".
  45. *
  46. * @param contentType The content type.
  47. * @return This HTTPMixIn instance.
  48. */
  49. public HTTPMixIn setContentType(String contentType) {
  50. this._contentType = contentType;
  51. return this;
  52. }
  53. @Override
  54. public void initialize() {
  55. _httpClient = new HttpClient();
  56. }
  57. /**
  58. * POST the specified request payload to the specified HTTP endpoint.
  59. * @param endpointURL The HTTP endpoint URL.
  60. * @param request The request payload.
  61. * @return The HTTP response payload.
  62. */
  63. public String postString(String endpointURL, String request) {
  64. PostMethod postMethod = new PostMethod(endpointURL);
  65. try {
  66. postMethod.setRequestEntity(new StringRequestEntity(request, _contentType, "UTF-8"));
  67. } catch (UnsupportedEncodingException e) {
  68. e.printStackTrace();
  69. }
  70. try {
  71. return execute(postMethod);
  72. } finally {
  73. postMethod.releaseConnection();
  74. }
  75. }
  76. /**
  77. * POST the specified String request to the specified HTTP endpoint and perform an XML compare
  78. * between the HTTP response and the specified expected response String.
  79. * @param endpointURL The HTTP endpoint URL.
  80. * @param request The classpath resource to be posted to the endpoint.
  81. * @param expectedResponse The String to use to perform the XML test on the response.
  82. * @return The HTTP response payload.
  83. */
  84. public String postStringAndTestXML(String endpointURL, String request, String expectedResponse) {
  85. String response = postString(endpointURL, request);
  86. SwitchYardTestKit.compareXMLToString(response, expectedResponse);
  87. return response;
  88. }
  89. /**
  90. * POST the specified classpath resource to the specified HTTP endpoint.
  91. * @param endpointURL The HTTP endpoint URL.
  92. * @param requestResource The classpath resource to be posted to the endpoint.
  93. * @return The HTTP response payload.
  94. */
  95. public String postResource(String endpointURL, String requestResource) {
  96. PostMethod postMethod = new PostMethod(endpointURL);
  97. InputStream requestStream = getTestKit().getResourceAsStream(requestResource);
  98. try {
  99. postMethod.setRequestEntity(new InputStreamRequestEntity(requestStream, _contentType + "; charset=utf-8"));
  100. return execute(postMethod);
  101. } finally {
  102. try {
  103. requestStream.close();
  104. } catch (IOException e) {
  105. Assert.fail("Unexpected exception closing HTTP request resource stream.");
  106. } finally {
  107. postMethod.releaseConnection();
  108. }
  109. }
  110. }
  111. /**
  112. * POST the specified classpath resource to the specified HTTP endpoint and perform an XML compare
  113. * between the HTTP response and the specified expected classpath response resource.
  114. * @param endpointURL The HTTP endpoint URL.
  115. * @param requestResource The classpath resource to be posted to the endpoint.
  116. * @param expectedResponseResource The classpath resource to use to perform the XML test on the response.
  117. * @return The HTTP response payload.
  118. */
  119. public String postResourceAndTestXML(String endpointURL, String requestResource, String expectedResponseResource) {
  120. String response = postResource(endpointURL, requestResource);
  121. getTestKit().compareXMLToResource(response, expectedResponseResource);
  122. return response;
  123. }
  124. /**
  125. * Execute the supplied HTTP Method.
  126. * <p/>
  127. * Does not release the {@link org.apache.commons.httpclient.HttpMethod#releaseConnection() HttpMethod connection}.
  128. *
  129. * @param method The HTTP Method.
  130. * @return The HTTP Response.
  131. */
  132. public String execute(HttpMethod method) {
  133. if (_httpClient == null) {
  134. Assert.fail("HTTPMixIn not initialized. You must call the initialize() method before using this MixIn");
  135. }
  136. try {
  137. _httpClient.executeMethod(method);
  138. return method.getResponseBodyAsString();
  139. } catch (Exception e) {
  140. try {
  141. Assert.fail("Exception invoking HTTP endpoint '" + method.getURI() + "': " + e.getMessage());
  142. } catch (URIException e1) {
  143. e1.printStackTrace();
  144. }
  145. }
  146. return null;
  147. }
  148. @Override
  149. public void uninitialize() {
  150. if (_httpClient != null) {
  151. final HttpConnectionManager connectionManager = _httpClient.getHttpConnectionManager();
  152. if (connectionManager instanceof MultiThreadedHttpConnectionManager) {
  153. final MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = (MultiThreadedHttpConnectionManager)connectionManager;
  154. multiThreadedHttpConnectionManager.shutdown();
  155. }
  156. }
  157. }
  158. }