| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | (1) | 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 | | | |
| 49 | | | |
| 50 | | | |
| 51 | 0 | (2) | public class HttpConnectorBeanTest |
| 52 | | | extends TestCase |
| 53 | | | { |
| 54 | | | private static final String URL_SUBWAYCA |
| 55 | | | = "http://subwayca/php3/index.php3"; |
| 56 | 0 | | private static final String URL_SIMPLE_SERVER |
| 57 | | | |
| 58 | | | = "http://" + TestCase.getHostName() + ":55555"; |
| 59 | | | |
| 60 | 0 | | private HttpConnectorSessionInterface mBean = null; |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | @return |
| 65 | | | |
| 66 | | | |
| 67 | | | public static Test suite () |
| 68 | | | { |
| 69 | 0 | | TestSuite suite = null; |
| 70 | 0 | | if (hasTestCases()) |
| 71 | | | { |
| 72 | 0 | | suite = getSuite(HttpConnectorBeanTest.class); |
| 73 | | | } |
| 74 | | | else |
| 75 | | | { |
| 76 | 0 | | suite = new TestSuite(HttpConnectorBeanTest.class); |
| 77 | | | } |
| 78 | | | |
| 79 | 0 | | final Test setup = new HttpConnectorBeanTestSetup(suite); |
| 80 | 0 | (3)(4) | return setup; |
| 81 | | | } |
| 82 | | | |
| 83 | | | {@inheritDoc} |
| 84 | | | protected void setUp () |
| 85 | | | throws Exception |
| 86 | | | { |
| 87 | 0 | | super.setUp(); |
| 88 | 0 | | mBean = HttpConnectorSessionJNDIUtil.getHome().create(); |
| 89 | 0 | | } |
| 90 | | | |
| 91 | | | protected void tearDown () |
| 92 | | | throws Exception |
| 93 | | | { |
| 94 | 0 | | super.tearDown(); |
| 95 | 0 | | mBean = null; |
| 96 | 0 | | } |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | @throws |
| 101 | | | |
| 102 | | | public void testConnectingSubwayCA () |
| 103 | | | throws Exception |
| 104 | | | { |
| 105 | 0 | | final Url url = Url.fromString(URL_SUBWAYCA); |
| 106 | 0 | | final ConnectorResponse response = mBean.sendAndReceive( |
| 107 | | | getName().getBytes(), url, null); |
| 108 | 0 | | assertNotNull("Response object is null", response); |
| 109 | 0 | | assertTrue("Response message is empty", |
| 110 | | | response.getResponse().length > 0); |
| 111 | 0 | | assertTrue("Request time not calculated", response.getRequestTime() > 0); |
| 112 | 0 | (5) | assertTrue("Retries not expected", response.getTries() == 1); |
| 113 | 0 | | } |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | @throws |
| 119 | | | |
| 120 | | | public void testConnectingSimpleServer () |
| 121 | | | throws Exception |
| 122 | | | { |
| 123 | 0 | | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
| 124 | 0 | | final ConnectorResponse response = mBean.sendAndReceive( |
| 125 | | | getName().getBytes(), url, null); |
| 126 | 0 | | assertNotNull("Response object is null", response); |
| 127 | 0 | | assertTrue("Response message is empty", |
| 128 | | | response.getResponse().length > 0); |
| 129 | 0 | | assertTrue("Request time not calculated", response.getRequestTime() > 0); |
| 130 | 0 | | } |
| 131 | | | |
| 132 | | | |
| 133 | | | |
| 134 | | | |
| 135 | | | @throws |
| 136 | | | |
| 137 | | | public void testConnectingSimpleServerWithExpectedResponseHeaderSuccess () |
| 138 | | | throws Exception |
| 139 | | | { |
| 140 | 0 | | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
| 141 | 0 | | final HttpRequestResponseHeader header = new HttpRequestResponseHeader(); |
| 142 | 0 | | header.addRequestHeader("ECHO_ExpectedHeader", "value"); |
| 143 | 0 | | header.addResponseHeader("ExpectedHeader", "value"); |
| 144 | 0 | | final ConnectorResponse response = mBean.sendAndReceive( |
| 145 | | | getName().getBytes(), url, header); |
| 146 | 0 | | assertNotNull("Response object is null", response); |
| 147 | 0 | | assertTrue("Response message is empty", |
| 148 | | | response.getResponse().length > 0); |
| 149 | 0 | | assertTrue("Request time not calculated", response.getRequestTime() > 0); |
| 150 | 0 | | } |
| 151 | | | |
| 152 | | | |
| 153 | | | |
| 154 | | | |
| 155 | | | |
| 156 | | | @throws |
| 157 | | | |
| 158 | | | public void testConnectingSimpleServerWithExpectedResponseHeaderFailed () |
| 159 | | | throws Exception |
| 160 | | | { |
| 161 | 0 | | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
| 162 | 0 | | final HttpRequestResponseHeader header = new HttpRequestResponseHeader(); |
| 163 | 0 | | header.addResponseHeader("Content-Type", "text/plain"); |
| 164 | | | ConnectorResponse response; |
| 165 | | | try |
| 166 | | | { |
| 167 | 0 | (6) | response = mBean.sendAndReceive( |
| 168 | | | getName().getBytes(), url, header); |
| 169 | 0 | | fail("Expected 'HttpInvalidResponseHeaderException' " |
| 170 | | | + " with invalid value for 'Content-Type' in response"); |
| 171 | | | } |
| 172 | 0 | | catch (ConnectorException ce) |
| 173 | | | { |
| 174 | 0 | | if (ce.getCause() instanceof HttpInvalidResponseHeaderException) |
| 175 | | | { |
| 176 | | | |
| 177 | 0 | | final HttpInvalidResponseHeaderException expected |
| 178 | | | = (HttpInvalidResponseHeaderException) ce.getCause(); |
| 179 | 0 | | assertTrue("Content-Type not invalid in response", |
| 180 | | | expected.getInvalidParameter().containsKey("Content-Type")); |
| 181 | 0 | | } |
| 182 | | | else |
| 183 | | | { |
| 184 | 0 | | fail("Not the expected exception: " + ce.getMessage()); |
| 185 | | | } |
| 186 | 0 | | } |
| 187 | 0 | | } |
| 188 | | | |
| 189 | | | |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | @throws |
| 194 | | | |
| 195 | | | public void testConnectingSimpleServerWithEmptyResponseBody () |
| 196 | | | throws Exception |
| 197 | | | { |
| 198 | 0 | | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
| 199 | 0 | | final HttpRequestResponseHeader header = new HttpRequestResponseHeader(); |
| 200 | 0 | | header.addRequestHeader("UseEmptyResponse", "true"); |
| 201 | | | ConnectorResponse response; |
| 202 | | | try |
| 203 | | | { |
| 204 | 0 | (7) | response = mBean.sendAndReceive( |
| 205 | | | getName().getBytes(), url, header); |
| 206 | 0 | | fail("Expected 'HttpEmptyResponseException'"); |
| 207 | | | } |
| 208 | 0 | | catch (ConnectorException ce) |
| 209 | | | { |
| 210 | 0 | | if (!(ce.getCause() instanceof HttpEmptyResponseException)) |
| 211 | | | { |
| 212 | 0 | | fail("Not the expected exception: " + ce.getCause().getMessage()); |
| 213 | | | } |
| 214 | 0 | | } |
| 215 | 0 | | } |
| 216 | | | |
| 217 | | | |
| 218 | | | |
| 219 | | | @throws |
| 220 | | | |
| 221 | | | public void testConnectingSimpleServerWithTimeout () |
| 222 | | | throws Exception |
| 223 | | | { |
| 224 | 0 | | final Url url = Url.fromString(URL_SIMPLE_SERVER); |
| 225 | 0 | | final HttpRequestResponseHeader header = new HttpRequestResponseHeader(); |
| 226 | 0 | | header.addRequestHeader("DoNotRespond", "True"); |
| 227 | | | ConnectorResponse response; |
| 228 | | | try |
| 229 | | | { |
| 230 | 0 | (8) | response = mBean.sendAndReceive( |
| 231 | | | getName().getBytes(), url, header); |
| 232 | 0 | | fail("Expected 'TimeoutException'"); |
| 233 | | | } |
| 234 | 0 | | catch (ConnectionTimeoutErrorException expected) |
| 235 | | | { |
| 236 | | | |
| 237 | | | } |
| 238 | 0 | | catch (ConnectorException ce) |
| 239 | | | { |
| 240 | 0 | | fail("Not the expected exception: " + ce.getMessage()); |
| 241 | 0 | | } |
| 242 | 0 | | } |
| 243 | | | |
| 244 | | | } |
| 245 | | | |