Author: hiranya
Date: Mon Jul 22 22:05:35 2013
New Revision: 1505831

URL: http://svn.apache.org/r1505831
Log:
Refactored sample157 to use the BasicHttpClient

Modified:
    
synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/clients/BasicHttpClient.java
    
synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/tests/proxy/Sample157.java

Modified: 
synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/clients/BasicHttpClient.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/clients/BasicHttpClient.java?rev=1505831&r1=1505830&r2=1505831&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/clients/BasicHttpClient.java
 (original)
+++ 
synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/clients/BasicHttpClient.java
 Mon Jul 22 22:05:35 2013
@@ -26,6 +26,7 @@ import org.apache.http.entity.BasicHttpE
 import org.apache.http.impl.client.DefaultHttpClient;
 
 import java.io.ByteArrayInputStream;
+import java.util.Map;
 
 /**
  * A simple HTTP client that enables making HTTP requests. Useful for testing
@@ -60,9 +61,29 @@ public class BasicHttpClient {
      * @throws Exception If an error occurs while making the HTTP call
      */
     public HttpResponse doPost(String url, byte[] payload, String contentType) 
throws Exception {
+        return doPost(url, payload, contentType, null);
+    }
+
+    /**
+     * Make a HTTP POST request on the specified URL.
+     *
+     * @param url A valid HTTP URL
+     * @param payload An array of bytes to be posted to the URL (message body)
+     * @param contentType Content type of the message body
+     * @param headers A map of HTTP headers to be set on the outgoing request
+     * @return A HttpResponse object
+     * @throws Exception If an error occurs while making the HTTP call
+     */
+    public HttpResponse doPost(String url, byte[] payload,
+                               String contentType, Map<String,String> headers) 
throws Exception {
         HttpClient client = new DefaultHttpClient();
         try {
             HttpPost post = new HttpPost(url);
+            if (headers != null) {
+                for (Map.Entry<String,String> entry : headers.entrySet()) {
+                    post.setHeader(entry.getKey(), entry.getValue());
+                }
+            }
             BasicHttpEntity entity = new BasicHttpEntity();
             entity.setContentType(contentType);
             entity.setContent(new ByteArrayInputStream(payload));

Modified: 
synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/tests/proxy/Sample157.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/tests/proxy/Sample157.java?rev=1505831&r1=1505830&r2=1505831&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/tests/proxy/Sample157.java
 (original)
+++ 
synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/tests/proxy/Sample157.java
 Mon Jul 22 22:05:35 2013
@@ -16,29 +16,24 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
+
 package org.apache.synapse.samples.framework.tests.proxy;
 
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpHeaders;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.protocol.HTTP;
 import org.apache.synapse.samples.framework.SynapseTestCase;
+import org.apache.synapse.samples.framework.clients.BasicHttpClient;
+import org.apache.synapse.samples.framework.clients.HttpResponse;
 
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
 
 public class Sample157 extends SynapseTestCase {
 
     private String requestXml;
-    private HttpClient httpclient;
+    private BasicHttpClient httpClient;
 
     public Sample157() {
         super(157);
-        httpclient = new DefaultHttpClient();
+        httpClient = new BasicHttpClient();
 
         requestXml = "<soap:Envelope 
xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"; 
xmlns:ser=\"http://services.samples\"; 
xmlns:xsd=\"http://services.samples/xsd\";>\n" +
                 "       <soap:Header/>\n" +
@@ -53,95 +48,41 @@ public class Sample157 extends SynapseTe
     }
 
 
-    public void testRoutingOnHttpHeader() {
+    public void testRoutingOnHttpHeader() throws Exception {
         String url = "http://localhost:8280/services/StockQuoteProxy";;
 
-        log.info("Running test: Routing Messages based on HTTP URL, HTTP 
Headers and Query " +
-                "Parameters");
+        log.info("Running test: Routing Messages based on HTTP URL, HTTP 
Headers and " +
+                "Query Parameters");
 
-        // Create a new HttpClient and Post Header
-        HttpPost httpPost = new HttpPost(url);//new HttpPost(availabilityUrl + 
VERSION_TEXT);
+        HttpResponse response = httpClient.doPost(url, requestXml.getBytes(),
+                "application/soap+xml;charset=UTF-8");
+        assertEquals(202, response.getStatus());
+
+
+        Map<String,String> headers = new HashMap<String, String>();
+        headers.put("foo", "bar");
+        response = httpClient.doPost(url, requestXml.getBytes(),
+                "application/soap+xml;charset=UTF-8", headers);
+        assertEquals(200, response.getStatus());
+        assertTrue("Response is empty", !"".equals(new 
String(response.getBody())));
+
+
+        headers = new HashMap<String, String>();
+        headers.put("my_custom_header1", "foo1");
+        response = httpClient.doPost(url, requestXml.getBytes(),
+                "application/soap+xml;charset=UTF-8", headers);
+        assertEquals(200, response.getStatus());
+        assertTrue("Response is empty", !"".equals(new 
String(response.getBody())));
 
-        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, 
"application/soap+xml;charset=UTF-8");
-        httpPost.setHeader("foo", "bar");
-        HttpResponse response;
-        HttpEntity entity;
-        try {
-            entity = new StringEntity(requestXml, "application/xml", 
HTTP.UTF_8);
-            httpPost.setEntity(entity);
-            response = httpclient.execute(httpPost);
-            assertNotNull("Did not get a response ", response);
-            HttpEntity resEntity = response.getEntity();
-            assertNotNull("Response is empty", resEntity);
-            BufferedReader rd = new BufferedReader(new 
InputStreamReader(resEntity.getContent()));
-            String result = "";
-            String line;
-            while ((line = rd.readLine()) != null) {
-                result += line;
-            }
-            System.out.println(result);
-            assertTrue("Response is empty", !"".equals(result));
-        } catch (Exception e) {
-            fail("Errors occurred while sending POST request: " + 
e.getMessage());
-        }
-
-        url = "http://localhost:8280/services/StockQuoteProxy";;
-
-        log.info("Running test: Routing Messages based on HTTP URL, HTTP 
Headers and Query Parameters");
-
-        // Create a new HttpClient and Post Header
-        httpPost = new HttpPost(url);//new HttpPost(availabilityUrl + 
VERSION_TEXT);
-
-        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, 
"application/soap+xml;charset=UTF-8");
-        httpPost.setHeader("my_custom_header1", "foo1");
-
-        try {
-            entity = new StringEntity(requestXml, "application/xml", 
HTTP.UTF_8);
-            httpPost.setEntity(entity);
-            response = httpclient.execute(httpPost);
-            assertNotNull("Did not get a response ", response);
-            HttpEntity resEntity = response.getEntity();
-            assertNotNull("Response is empty", resEntity);
-            BufferedReader rd = new BufferedReader(new 
InputStreamReader(resEntity.getContent()));
-            String result = "";
-            String line;
-            while ((line = rd.readLine()) != null) {
-                result += line;
-            }
-            System.out.println(result);
-            assertTrue("Response is empty", !"".equals(result));
-        } catch (Exception e) {
-            fail("Errors occurred while sending POST request: " + 
e.getMessage());
-        }
 
         url = 
"http://localhost:8280/services/StockQuoteProxy?qparam1=qpv_foo&qparam2=qpv_foo2";;
-        log.info("Running test: Routing Messages based on HTTP URL, HTTP 
Headers and Query Parameters");
-
-        // Create a new HttpClient and Post Header
-        httpPost = new HttpPost(url);//new HttpPost(availabilityUrl + 
VERSION_TEXT);
-
-        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, 
"application/soap+xml;charset=UTF-8");
-        httpPost.setHeader("my_custom_header2", "bar");
-        httpPost.setHeader("my_custom_header3", "foo");
-
-        try {
-            entity = new StringEntity(requestXml, "application/xml", 
HTTP.UTF_8);
-            httpPost.setEntity(entity);
-            response = httpclient.execute(httpPost);
-            assertNotNull("Did not get a response ", response);
-            HttpEntity resEntity = response.getEntity();
-            assertNotNull("Response is empty", resEntity);
-            BufferedReader rd = new BufferedReader(new 
InputStreamReader(resEntity.getContent()));
-            String result = "";
-            String line;
-            while ((line = rd.readLine()) != null) {
-                result += line;
-            }
-            System.out.println(result);
-            assertTrue("Response is empty", !"".equals(result));
-        } catch (Exception e) {
-            fail("Errors occurred while sending POST request: " + 
e.getMessage());
-        }
+        headers = new HashMap<String, String>();
+        headers.put("my_custom_header2", "bar");
+        headers.put("my_custom_header3", "foo");
+        response = httpClient.doPost(url, requestXml.getBytes(),
+                "application/soap+xml;charset=UTF-8", headers);
+        assertEquals(200, response.getStatus());
+        assertTrue("Response is empty", !"".equals(new 
String(response.getBody())));
     }
 
 }


Reply via email to