| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.connector.http; |
|---|
| 34 | |
|---|
| 35 | import junit.framework.Test; |
|---|
| 36 | import junit.framework.TestSuite; |
|---|
| 37 | import org.jcoderz.commons.TestCase; |
|---|
| 38 | import org.jcoderz.commons.connector.ConnectionTimeoutErrorException; |
|---|
| 39 | import org.jcoderz.commons.connector.ConnectorException; |
|---|
| 40 | import org.jcoderz.commons.connector.http.transport.HttpEmptyResponseException; |
|---|
| 41 | import org.jcoderz.commons.connector.http.transport.HttpInvalidResponseHeaderException; |
|---|
| 42 | import org.jcoderz.commons.connector.http.transport.HttpRequestResponseHeader; |
|---|
| 43 | import org.jcoderz.commons.types.Url; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * JUnit remote (bean) tests for the commons http connector. |
|---|
| 49 | * |
|---|
| 50 | */ |
|---|
| 51 | public class HttpConnectorBeanTest |
|---|
| 52 | extends TestCase |
|---|
| 53 | { |
|---|
| 54 | private static final String URL_SUBWAYCA |
|---|
| 55 | = "http://subwayca/php3/index.php3"; |
|---|
| 56 | private static final String URL_SIMPLE_SERVER |
|---|
| 57 | // = "http://localhost:55555/index.html?Gruss=Hallo&Wort2=Welt"; |
|---|
| 58 | = "http://" + TestCase.getHostName() + ":55555"; |
|---|
| 59 | |
|---|
| 60 | private HttpConnectorSessionInterface mBean = null; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Creates the testsuite. |
|---|
| 64 | * @return Test |
|---|
| 65 | * the testsuite |
|---|
| 66 | */ |
|---|
| 67 | public static Test suite () |
|---|
| 68 | { |
|---|
| 69 | TestSuite suite = null; |
|---|
| 70 | if (hasTestCases()) |
|---|
| 71 | { |
|---|
| 72 | suite = getSuite(HttpConnectorBeanTest.class); |
|---|
| 73 | } |
|---|
| 74 | else |
|---|
| 75 | { |
|---|
| 76 | suite = new TestSuite(HttpConnectorBeanTest.class); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | final Test setup = new HttpConnectorBeanTestSetup(suite); |
|---|
| 80 | return setup; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /** {@inheritDoc} */ |
|---|
| 84 | protected void setUp () |
|---|
| 85 | throws Exception |
|---|
| 86 | { |
|---|
| 87 | super.setUp(); |
|---|
| 88 | mBean = HttpConnectorSessionJNDIUtil.getHome().create(); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | protected void tearDown () |
|---|
| 92 | throws Exception |
|---|
| 93 | { |
|---|
| 94 | super.tearDown(); |
|---|
| 95 | mBean = null; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Tests a simple connection to the subwayca. |
|---|
| 100 | * @throws Exception in case of any error |
|---|
| 101 | */ |
|---|
| 102 | public void testConnectingSubwayCA () |
|---|
| 103 | throws Exception |
|---|
| 104 | { |
|---|
| 105 | final Url url = Url.fromString(URL_SUBWAYCA); |
|---|
| 106 | final ConnectorResponse response = mBean.sendAndReceive( |
|---|
| 107 | getName().getBytes(), url, null); |
|---|
| 108 | assertNotNull("Response object is null", response); |
|---|
| 109 | assertTrue("Response message is empty", |
|---|
| 110 | response.getResponse().length > 0); |
|---|
| 111 | assertTrue("Request time not calculated", response.getRequestTime() > 0); |
|---|
| 112 | assertTrue("Retries not expected", response.getTries() == 1); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Tests a simple connection to the simple server |
|---|
| 117 | * started whilst setup. |
|---|
| 118 | * @throws Exception in case of any error |
|---|
| 119 | */ |
|---|
| 120 | public void testConnectingSimpleServer () |
|---|
| 121 | throws Exception |
|---|
| 122 | { |
|---|
| 123 | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
|---|
| 124 | final ConnectorResponse response = mBean.sendAndReceive( |
|---|
| 125 | getName().getBytes(), url, null); |
|---|
| 126 | assertNotNull("Response object is null", response); |
|---|
| 127 | assertTrue("Response message is empty", |
|---|
| 128 | response.getResponse().length > 0); |
|---|
| 129 | assertTrue("Request time not calculated", response.getRequestTime() > 0); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * Tests a connection to the simple server started whilst setup |
|---|
| 134 | * including a header to set and an expected header in response. |
|---|
| 135 | * @throws Exception in case of any error |
|---|
| 136 | */ |
|---|
| 137 | public void testConnectingSimpleServerWithExpectedResponseHeaderSuccess () |
|---|
| 138 | throws Exception |
|---|
| 139 | { |
|---|
| 140 | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
|---|
| 141 | final HttpRequestResponseHeader header = new HttpRequestResponseHeader(); |
|---|
| 142 | header.addRequestHeader("ECHO_ExpectedHeader", "value"); |
|---|
| 143 | header.addResponseHeader("ExpectedHeader", "value"); |
|---|
| 144 | final ConnectorResponse response = mBean.sendAndReceive( |
|---|
| 145 | getName().getBytes(), url, header); |
|---|
| 146 | assertNotNull("Response object is null", response); |
|---|
| 147 | assertTrue("Response message is empty", |
|---|
| 148 | response.getResponse().length > 0); |
|---|
| 149 | assertTrue("Request time not calculated", response.getRequestTime() > 0); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * Tests a connection to the simple server started whilst setup |
|---|
| 154 | * including a header to validate a parameter in response. |
|---|
| 155 | * The parameter to validate failed. |
|---|
| 156 | * @throws Exception in case of any error |
|---|
| 157 | */ |
|---|
| 158 | public void testConnectingSimpleServerWithExpectedResponseHeaderFailed () |
|---|
| 159 | throws Exception |
|---|
| 160 | { |
|---|
| 161 | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
|---|
| 162 | final HttpRequestResponseHeader header = new HttpRequestResponseHeader(); |
|---|
| 163 | header.addResponseHeader("Content-Type", "text/plain"); |
|---|
| 164 | ConnectorResponse response; |
|---|
| 165 | try |
|---|
| 166 | { |
|---|
| 167 | response = mBean.sendAndReceive( |
|---|
| 168 | getName().getBytes(), url, header); |
|---|
| 169 | fail("Expected 'HttpInvalidResponseHeaderException' " |
|---|
| 170 | + " with invalid value for 'Content-Type' in response"); |
|---|
| 171 | } |
|---|
| 172 | catch (ConnectorException ce) |
|---|
| 173 | { |
|---|
| 174 | if (ce.getCause() instanceof HttpInvalidResponseHeaderException) |
|---|
| 175 | { |
|---|
| 176 | // expected |
|---|
| 177 | final HttpInvalidResponseHeaderException expected |
|---|
| 178 | = (HttpInvalidResponseHeaderException) ce.getCause(); |
|---|
| 179 | assertTrue("Content-Type not invalid in response", |
|---|
| 180 | expected.getInvalidParameter().containsKey("Content-Type")); |
|---|
| 181 | } |
|---|
| 182 | else |
|---|
| 183 | { |
|---|
| 184 | fail("Not the expected exception: " + ce.getMessage()); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| 190 | * Tests a connection to the simple server started whilst setup |
|---|
| 191 | * with an empty message body received in response. |
|---|
| 192 | * |
|---|
| 193 | * @throws Exception in case of any error |
|---|
| 194 | */ |
|---|
| 195 | public void testConnectingSimpleServerWithEmptyResponseBody () |
|---|
| 196 | throws Exception |
|---|
| 197 | { |
|---|
| 198 | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
|---|
| 199 | final HttpRequestResponseHeader header = new HttpRequestResponseHeader(); |
|---|
| 200 | header.addRequestHeader("UseEmptyResponse", "true"); |
|---|
| 201 | ConnectorResponse response; |
|---|
| 202 | try |
|---|
| 203 | { |
|---|
| 204 | response = mBean.sendAndReceive( |
|---|
| 205 | getName().getBytes(), url, header); |
|---|
| 206 | fail("Expected 'HttpEmptyResponseException'"); |
|---|
| 207 | } |
|---|
| 208 | catch (ConnectorException ce) |
|---|
| 209 | { |
|---|
| 210 | if (!(ce.getCause() instanceof HttpEmptyResponseException)) |
|---|
| 211 | { |
|---|
| 212 | fail("Not the expected exception: " + ce.getCause().getMessage()); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | /** |
|---|
| 218 | * Tests a timeout whilst waiting for a response from the simple server. |
|---|
| 219 | * @throws Exception in case of any error |
|---|
| 220 | */ |
|---|
| 221 | public void testConnectingSimpleServerWithTimeout () |
|---|
| 222 | throws Exception |
|---|
| 223 | { |
|---|
| 224 | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
|---|
| 225 | final HttpRequestResponseHeader header = new HttpRequestResponseHeader(); |
|---|
| 226 | header.addRequestHeader("DoNotRespond", "True"); |
|---|
| 227 | ConnectorResponse response; |
|---|
| 228 | try |
|---|
| 229 | { |
|---|
| 230 | response = mBean.sendAndReceive( |
|---|
| 231 | getName().getBytes(), url, header); |
|---|
| 232 | fail("Expected 'TimeoutException'"); |
|---|
| 233 | } |
|---|
| 234 | catch (ConnectionTimeoutErrorException expected) |
|---|
| 235 | { |
|---|
| 236 | // expected |
|---|
| 237 | } |
|---|
| 238 | catch (ConnectorException ce) |
|---|
| 239 | { |
|---|
| 240 | fail("Not the expected exception: " + ce.getMessage()); |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | } |
|---|