root/trunk/test/java/org/jcoderz/commons/connector/http/transport/HttpClientConnectionTest.java

Revision 1380, 28.8 kB (checked in by amandel, 3 years ago)

Use same port for client & server.
Don't catch unexpected exceptions in test case.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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 */
33package org.jcoderz.commons.connector.http.transport;
34
35import java.io.ByteArrayInputStream;
36
37import junit.framework.Test;
38import junit.framework.TestSuite;
39
40import org.jcoderz.commons.ArgumentMalformedException;
41import org.jcoderz.commons.TestCase;
42import org.jcoderz.commons.util.Constants;
43import org.jcoderz.commons.util.StringUtil;
44
45
46
47/**
48 * This class test the implementation of the HttpConnectionInterface
49 * provided via the Jakarta commons-httpclient project.
50 * @author anonymous
51 */
52public class HttpClientConnectionTest
53      extends TestCase
54{
55   private static final int CONNECT_TIMEOUT = 5000;
56   private static final int READ_TIMEOUT = 5000;
57   private static final String FILE_SEPARATOR
58         = System.getProperty("file.separator");
59   private static final String
60         DEFAULT_URL = "http://localhost:" 
61             + HttpClientConnectionTestSetup.DEFAULT_SERVER_PORT;
62   private static final byte[]
63         SIMPLE_BODY = "This is a simple POST request".getBytes();
64   private static final String
65         ECHO_SIMPLE_BODY = "Echo:This is a simple POST request";
66   private static final String
67         CONTENT_TYPE_PARAMETER = "Content-Type";
68   private static final String
69         CONTENT_TYPE_PARAMETER_VALUE = "text/xml; charset=ISO-8859-1";
70   private static final String
71         CONNECTION_PARAMETER = "Connection";
72   private static final String
73         CONNECTION_PARAMETER_VALUE_CLOSE = "Close";
74   private static final String
75         CONNECTION_PARAMETER_VALUE_KEEPALIVE = "Keep-Alive";
76   private static final String
77         HTTPCONNECTIONEXCEPTION = "HttpConnectionException: ";
78   private static final String
79         ILLEGALSTATEEXCEPTION = "IllegalStateException: ";
80   private static final String
81         NO_MESSAGE_BODY_IN_RESPONSE = "No message body in response";
82   private static final String
83         CONNECTION_CLOSE_EXPECTED = "Connection close expected";
84   private static final String
85         RESPONSE_BODY_NOT_LIKE_EXPECTED
86            = "Response body not like expected";
87   private static final String
88         ATTRIBUTE_CONNECTION_MISSING
89            = "No attribute \'Connection\'";
90   private static final String
91         CONNECTION_MUST_BE_ESTABLISHED_OR_RELEASED
92            = "Connection must be established before or released";
93   private static final String
94         ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED
95            = "Illegal State message not like expected";
96   private static final String
97         ILLEGALSTATEEXCEPTION_EXPECTED
98            = "IllegalStateException expected";
99
100   /** The interface implementation in use. */
101   private HttpClientConnection mHttpConnection;
102
103
104   /**
105    * Main.
106    *
107    * @param args main arguments
108    */
109   public static void main (
110         String[] args)
111   {
112      junit.textui.TestRunner.run(suite());
113   }
114
115   /**
116    * Creates the testsuite.
117    * @return Test
118    *          the testsuite
119    */
120   public static Test suite ()
121   {
122      TestSuite suite = null;
123      if (hasTestCases())
124      {
125         suite = getSuite(HttpClientConnectionTest.class);
126      }
127      else
128      {
129         suite = new TestSuite(HttpClientConnectionTest.class);
130      }
131
132      final Test setup = new HttpClientConnectionTestSetup(suite);
133      return setup;
134   }
135
136   /**
137    * Setup creates a new interface implementation object.
138    * @see TestCase#setUp()
139    */
140   protected void setUp ()
141         throws Exception
142   {
143      super.setUp();
144      mHttpConnection = new HttpClientConnectionImpl();
145   }
146
147   /**
148    * TearDown of the TestCase.
149    * @see TestCase#tearDown()
150    */
151   protected void tearDown ()
152         throws Exception
153   {
154      super.tearDown();
155   }
156
157   /**
158    * Performs establishing a connection, creating a request, sending that
159    * request, receiving the response and closing the connection.
160    * @throws Exception for any unexpected error
161    */
162   public void testSendSimplePostRequest ()
163         throws Exception
164   {
165      // connect
166      mHttpConnection.establishConnection(
167            DEFAULT_URL,
168            CONNECT_TIMEOUT, READ_TIMEOUT);
169      mHttpConnection.setRequestBody(
170            new ByteArrayInputStream(SIMPLE_BODY));
171      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
172      header.addRequestHeader(
173            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
174      header.addRequestHeader(
175            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
176      mHttpConnection.setRequestResponseHeader(header);
177
178      // execute
179      mHttpConnection.execute();
180      final String response = getResponseBodyAsString();
181      final String connectionValue
182            = mHttpConnection.getResponseHeader(CONNECTION_PARAMETER);
183
184      // close
185      mHttpConnection.closeConnection();
186
187      assertNotNull(NO_MESSAGE_BODY_IN_RESPONSE, response);
188      assertEquals(RESPONSE_BODY_NOT_LIKE_EXPECTED, ECHO_SIMPLE_BODY, response);
189      assertNotNull(ATTRIBUTE_CONNECTION_MISSING, connectionValue);
190      assertEquals(CONNECTION_CLOSE_EXPECTED,
191            "close",
192            connectionValue.toLowerCase(Constants.SYSTEM_LOCALE));
193   }
194
195   /**
196    * Sends post request without a waiting server.
197    * @throws Exception if the test case fails with an exception.
198    */
199   public void testSendSimplePostRequestToNotExistingTarget ()
200       throws Exception
201   {
202      //    connect
203      mHttpConnection.establishConnection(
204            "http://localhost:23",
205            CONNECT_TIMEOUT, READ_TIMEOUT);
206      mHttpConnection.setRequestBody(
207            new ByteArrayInputStream(SIMPLE_BODY));
208      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
209      header.addRequestHeader(
210            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
211      header.addRequestHeader(
212            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
213      mHttpConnection.setRequestResponseHeader(header);
214
215      // execute
216      try
217      {
218         mHttpConnection.execute();
219         fail("IOException expected");
220      }
221      catch (HttpConnectConnectionException expected)
222      {
223        // expected
224      }
225      // close
226      mHttpConnection.closeConnection();
227   }
228
229   /**
230    * Tests if a response header key value pair set for the connection
231    * has been evaluated correctly.
232    *
233    * @throws Exception in case of any unexpected error
234    */
235   public void testSendSimplePostRequestWithExpectedResponseHeaderSuccess ()
236         throws Exception
237   {
238      // connect
239      mHttpConnection.establishConnection(
240            DEFAULT_URL,
241            CONNECT_TIMEOUT, READ_TIMEOUT);
242      mHttpConnection.setRequestBody(
243            new ByteArrayInputStream(SIMPLE_BODY));
244      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
245      header.addRequestHeader(
246            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
247      header.addRequestHeader(
248            "ECHO_ResponseHeader1", "expected1");
249      header.addResponseHeader(
250            "ResponseHeader1", "expected1");
251      mHttpConnection.setRequestResponseHeader(header);
252
253      // execute
254      mHttpConnection.execute();
255      final String response = getResponseBodyAsString();
256      final String connectionValue
257            = mHttpConnection.getResponseHeader(CONNECTION_PARAMETER);
258      final String expectedValue
259            = mHttpConnection.getResponseHeader("ResponseHeader1");
260
261      // close
262      mHttpConnection.closeConnection();
263
264      assertNotNull(NO_MESSAGE_BODY_IN_RESPONSE, response);
265      assertEquals(RESPONSE_BODY_NOT_LIKE_EXPECTED, ECHO_SIMPLE_BODY, response);
266      assertNotNull(ATTRIBUTE_CONNECTION_MISSING, connectionValue);
267      assertNotNull("Expected response header missing", expectedValue);
268      assertEquals("Value of expected response header not like expected - ",
269            "expected1",
270            expectedValue.toLowerCase(Constants.SYSTEM_LOCALE));
271   }
272
273   /**
274    * Tests if the correct exception is thrown if evaluating a response header
275    * key value pair set for the connection has been failed.
276    *
277    * @throws Exception in case of any unexpected error
278    */
279   public void testSendSimplePostRequestWithExpectedResponseHeaderFailed ()
280         throws Exception
281   {
282      // connect
283      mHttpConnection.establishConnection(
284            DEFAULT_URL,
285            CONNECT_TIMEOUT, READ_TIMEOUT);
286      mHttpConnection.setRequestBody(
287            new ByteArrayInputStream(SIMPLE_BODY));
288      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
289      header.addRequestHeader(
290            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
291      header.addRequestHeader(
292            "ECHO_ResponseHeader2", "expected2");
293      header.addResponseHeader(
294            "ResponseHeader1", "expected2");
295      header.addResponseHeader(
296            "ResponseHeader2", "wrong value");
297      mHttpConnection.setRequestResponseHeader(header);
298
299      // execute
300      try
301      {
302         mHttpConnection.execute();
303         fail("Exception expected regarding response header");
304      }
305      catch (HttpInvalidResponseHeaderException expected)
306      {
307         // expected
308      }
309   }
310
311   /**
312    * Tests if the correct exception is thrown if the http response
313    * body received is empty.
314    *
315    * @throws Exception in case of any unexpected error
316    */
317   public void testSendSimplePostRequestWithEmptyResponseBody ()
318         throws Exception
319   {
320      // connect
321      mHttpConnection.establishConnection(
322            DEFAULT_URL,
323            CONNECT_TIMEOUT, READ_TIMEOUT);
324      mHttpConnection.setRequestBody(
325            new ByteArrayInputStream(SIMPLE_BODY));
326      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
327      header.addRequestHeader(
328            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
329      header.addRequestHeader(
330            "UseEmptyResponse", "true");
331      mHttpConnection.setRequestResponseHeader(header);
332
333      // execute
334      try
335      {
336         mHttpConnection.execute();
337         getResponseBodyAsString();
338         fail("'HttpEmptyResponseException' expected");
339      }
340      catch (HttpEmptyResponseException expected)
341      {
342         // expected
343      }
344
345      // close
346      mHttpConnection.closeConnection();
347   }
348
349   /**
350    * Sends a request and expects no response.
351    * @throws Exception for any unexpected error
352    */
353   public void testSendSimplePostRequestWithoutResponse ()
354         throws Exception
355   {
356      // connect
357      mHttpConnection.establishConnection(
358            DEFAULT_URL,
359            CONNECT_TIMEOUT, READ_TIMEOUT);
360      mHttpConnection.setRequestBody(new ByteArrayInputStream(SIMPLE_BODY));
361      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
362      header.addRequestHeader(
363            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
364      header.addRequestHeader(
365            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_KEEPALIVE);
366      header.addRequestHeader(
367            "DoNotRespond", "True");
368      mHttpConnection.setRequestResponseHeader(header);
369
370      // execute
371      byte[] response = null;
372      try
373      {
374         mHttpConnection.execute();
375         response = mHttpConnection.getResponseBody();
376         fail("'HttpTimeoutException' expected.");
377      }
378      catch (HttpTimeoutConnectionException he)
379      {
380         assertNull("No response message expected", response);
381      }
382      // close
383      mHttpConnection.closeConnection();
384   }
385
386   /**
387    * Sends a request and expects a immediate close on server side.
388    * @throws Exception for any unexpected errors
389    */
390   public void testSendSimplePostRequestWithImmediateClose ()
391         throws Exception
392   {
393      // connect
394      mHttpConnection.establishConnection(
395            DEFAULT_URL,
396            CONNECT_TIMEOUT, READ_TIMEOUT);
397      mHttpConnection.setRequestBody(new ByteArrayInputStream(SIMPLE_BODY));
398      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
399      header.addRequestHeader(
400            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
401      header.addRequestHeader(
402            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_KEEPALIVE);
403      header.addRequestHeader(
404            "DoImmediateClose", "True");
405      mHttpConnection.setRequestResponseHeader(header);
406
407      // execute
408      byte[] response = null;
409      try
410      {
411         mHttpConnection.execute();
412         response = mHttpConnection.getResponseBody();
413         fail("HttpConnectionException expected");
414      }
415      catch (HttpConnectionException he)
416      {
417         assertNull("No response message expected", response);
418      }
419
420      // close
421      mHttpConnection.closeConnection();
422   }
423
424   /**
425    * Sends a request with Connection=close and receives the
426    * response.
427    * @throws Exception for any unexpected error
428    */
429   public void testSendMultiplePostRequestAfterServerClose ()
430         throws Exception
431   {
432      // connect
433      mHttpConnection.establishConnection(
434            DEFAULT_URL,
435            CONNECT_TIMEOUT, READ_TIMEOUT);
436
437      // set message 1
438      mHttpConnection.setRequestBody(new ByteArrayInputStream(SIMPLE_BODY));
439      HttpRequestResponseHeader header = new HttpRequestResponseHeader();
440      header.addRequestHeader(
441            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
442      header.addRequestHeader(
443            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
444      mHttpConnection.setRequestResponseHeader(header);
445
446      String response = null;
447      String connectionValue = null;
448
449      // execute message 1
450      mHttpConnection.execute();
451      response = getResponseBodyAsString();
452      connectionValue = mHttpConnection.getResponseHeader(CONNECTION_PARAMETER);
453
454      // check reponse 1
455      assertNotNull(NO_MESSAGE_BODY_IN_RESPONSE, response);
456      assertEquals(RESPONSE_BODY_NOT_LIKE_EXPECTED, ECHO_SIMPLE_BODY, response);
457      assertNotNull(ATTRIBUTE_CONNECTION_MISSING, connectionValue);
458      assertEquals(CONNECTION_CLOSE_EXPECTED,
459            "close",
460            connectionValue.toLowerCase(Constants.SYSTEM_LOCALE));
461
462      // release connection
463      mHttpConnection.releaseConnection();
464
465      // set message 2
466      mHttpConnection.setRequestBody(
467            new ByteArrayInputStream(
468                  "This is the second POST request".getBytes()));
469      header = new HttpRequestResponseHeader();
470      header.addRequestHeader(
471            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
472      header.addRequestHeader(
473            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_KEEPALIVE);
474      mHttpConnection.setRequestResponseHeader(header);
475
476      // execute message 2
477      mHttpConnection.execute();
478      response = getResponseBodyAsString();
479
480      // release connection
481      mHttpConnection.releaseConnection();
482
483      // set message 3
484      mHttpConnection.setRequestBody(
485            new ByteArrayInputStream(
486               "This is the third POST request".getBytes()));
487      header = new HttpRequestResponseHeader();
488      header.addRequestHeader(
489            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
490      header.addRequestHeader(
491            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_KEEPALIVE);
492      mHttpConnection.setRequestResponseHeader(header);
493
494      // execute message 3
495      mHttpConnection.execute();
496      response = getResponseBodyAsString();
497      mHttpConnection.closeConnection();
498   }
499
500   /**
501    * Tests establishing a connection. Checks one simple good case and several
502    * cases that leads to an ArgumentMalformedException caused by an
503    * unsufficient URL parameter.
504    */
505   public void testEstablishConnection ()
506   {
507      // simple connect success
508      mHttpConnection.establishConnection(
509            "http://subwayca/php3/index.php3",
510            CONNECT_TIMEOUT, READ_TIMEOUT);
511      mHttpConnection.closeConnection();
512      //
513
514      // "invalid" argument leads to ArgumentMalformedException
515      final String invalidUrl = "malformed url";
516      try
517      {
518         mHttpConnection.establishConnection(
519               invalidUrl, CONNECT_TIMEOUT, READ_TIMEOUT);
520         fail("ArgumentMalformedException expected");
521      }
522      catch (ArgumentMalformedException expected)
523      {
524         //expected
525      }
526      //
527
528      // "null" argument leads to ArgumentMalformedException
529      try
530      {
531         mHttpConnection.establishConnection(
532               null, CONNECT_TIMEOUT, READ_TIMEOUT);
533         fail("ArgumentMalformedException expected");
534      }
535      catch (ArgumentMalformedException expected)
536      {
537         // expected
538      }
539      //
540
541      // useless timeout values
542      mHttpConnection.establishConnection(
543            "http://www.heise.de", -1, -1);
544
545      // close
546      mHttpConnection.closeConnection();
547   }
548
549   /**
550    * Releases a connection multiple times.
551    */
552   public void testReleaseConnectionSuccessMultipleUse ()
553   {
554      // connect
555      mHttpConnection.establishConnection(
556            DEFAULT_URL,
557            CONNECT_TIMEOUT, READ_TIMEOUT);
558       // multiple release
559      mHttpConnection.releaseConnection();
560      mHttpConnection.releaseConnection();
561
562      // close
563      mHttpConnection.closeConnection();
564   }
565
566   /**
567    * Tests the release of connection after sending a message and a sending anew
568    * after releasing.
569    * @throws Exception if the test case fails with an exception.
570    */
571   public void testReleaseConnectionSuccessWithTwoSendMessages ()
572       throws Exception
573   {
574      // connect
575      mHttpConnection.establishConnection(
576            DEFAULT_URL,
577            CONNECT_TIMEOUT, READ_TIMEOUT);
578
579      // set message 1
580      HttpRequestResponseHeader header = new HttpRequestResponseHeader();
581      header.addRequestHeader(
582            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
583      header.addRequestHeader(
584            "Message1", "Nachricht 1");
585      mHttpConnection.setRequestResponseHeader(header);
586
587      mHttpConnection.setRequestBody(
588            new ByteArrayInputStream("This is message one".getBytes()));
589
590      // send message 1
591      mHttpConnection.execute();
592
593      // release connection
594      mHttpConnection.releaseConnection();
595
596      // set message 2
597      header = new HttpRequestResponseHeader();
598      header.addRequestHeader(
599            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
600      header.addRequestHeader(
601            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
602      header.addRequestHeader(
603            "Message2", "Nachricht 2");
604      mHttpConnection.setRequestResponseHeader(header);
605
606      // send message 2
607      mHttpConnection.execute();
608
609      // close
610      mHttpConnection.closeConnection();
611   }
612
613   /**
614    * Tests releasing the connection and sending a second message without
615    * sending the first message.
616    * @throws Exception if the test case fails with an exception.
617    */
618   public void testReleaseConnectionSuccessWithOneSendMessage ()
619       throws Exception
620   {
621      //    connect
622      mHttpConnection.establishConnection(
623            DEFAULT_URL,
624            CONNECT_TIMEOUT, READ_TIMEOUT);
625
626      // set message 1
627      HttpRequestResponseHeader header = new HttpRequestResponseHeader();
628      header.addRequestHeader(
629            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
630      header.addRequestHeader(
631            "Message1", "Nachricht 1");
632      mHttpConnection.setRequestResponseHeader(header);
633
634      mHttpConnection.setRequestBody(
635            new ByteArrayInputStream("This is message one".getBytes()));
636
637      // release connection
638      mHttpConnection.releaseConnection();
639
640      // set message 2
641      header = new HttpRequestResponseHeader();
642      header.addRequestHeader(
643            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
644      header.addRequestHeader(
645            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
646      header.addRequestHeader(
647            "Message2", "Nachricht 2");
648      mHttpConnection.setRequestResponseHeader(header);
649
650      // send message 2
651      mHttpConnection.execute();
652   }
653
654   /**
655    * Tests error case with a missing "releaseConnection" call before reusing
656    * an already opened connection.
657    * @throws Exception if the test case fails with an exception.
658    */
659   public void testReleaseConnectionFailed ()
660       throws Exception
661   {
662      // connect
663      mHttpConnection.establishConnection(
664            DEFAULT_URL,
665            CONNECT_TIMEOUT, READ_TIMEOUT);
666
667      // set message 1
668      HttpRequestResponseHeader header = new HttpRequestResponseHeader();
669      header.addRequestHeader(
670            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
671      header.addRequestHeader(
672            "Message1", "Nachricht 1");
673      mHttpConnection.setRequestResponseHeader(header);
674
675      mHttpConnection.setRequestBody(
676            new ByteArrayInputStream("This is message one".getBytes()));
677
678      // send message 1
679      mHttpConnection.execute();
680
681      // set message 2
682      header = new HttpRequestResponseHeader();
683      header.addRequestHeader(
684            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
685      header.addRequestHeader(
686            "Message2", "Nachricht 2");
687      mHttpConnection.setRequestResponseHeader(header);
688
689      // send message 2
690      try
691      {
692         mHttpConnection.execute();
693         fail(ILLEGALSTATEEXCEPTION_EXPECTED);
694      }
695      catch (IllegalStateException ise)
696      {
697         assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
698               CONNECTION_MUST_BE_ESTABLISHED_OR_RELEASED,
699               ise.getMessage());
700      }
701      // close
702      mHttpConnection.closeConnection();
703   }
704
705   /**
706    * Release a not established connection.
707    */
708   public void testReleaseConnectionWithInvalidState ()
709   {
710      // invalid state
711      try
712      {
713         mHttpConnection.releaseConnection();
714         fail("IllegalStatException expected");
715      }
716      catch (IllegalStateException ise)
717      {
718         assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
719               "Connection must be established before",
720               ise.getMessage());
721      }
722   }
723    /**
724    * Closes the connection after successful sending.
725    * @throws Exception if the test case fails with an exception.
726    */
727   public void testCloseConnectionSuccessAfterSending ()
728       throws Exception
729   {
730      mHttpConnection.establishConnection(
731            DEFAULT_URL,
732            CONNECT_TIMEOUT, READ_TIMEOUT);
733
734      //    set message
735      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
736      header.addRequestHeader(
737            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
738      header.addRequestHeader(
739            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
740      mHttpConnection.setRequestResponseHeader(header);
741
742      mHttpConnection.setRequestBody(
743            new ByteArrayInputStream("This is the message".getBytes()));
744
745      mHttpConnection.execute();
746      mHttpConnection.closeConnection();
747   }
748
749   /**
750    * Multiple closing fails.
751    */
752   public void testCloseConnectionFailed ()
753   {
754      // connection not established
755      try
756      {
757         mHttpConnection.closeConnection();
758         fail(ILLEGALSTATEEXCEPTION_EXPECTED);
759      }
760      catch (IllegalStateException ise)
761      {
762         assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
763               "Connection must be established before",
764               ise.getMessage());
765      }
766
767      // closed twice
768      mHttpConnection.establishConnection(
769            "http://subwayca:80",
770            CONNECT_TIMEOUT, READ_TIMEOUT);
771      try
772      {
773         mHttpConnection.closeConnection();
774         mHttpConnection.closeConnection();
775         fail(ILLEGALSTATEEXCEPTION_EXPECTED);
776      }
777      catch (IllegalStateException ise)
778      {
779         assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
780               "Connection must be established before",
781               ise.getMessage());
782      }
783   }
784
785   /**
786    * Executes connection without establishing before.
787    * @throws Exception if the test case fails with an exception.
788    */
789   public void testExecuteFailed ()
790       throws Exception
791   {
792      // not connected
793      try
794      {
795         mHttpConnection.execute();
796         fail(ILLEGALSTATEEXCEPTION_EXPECTED);
797      }
798      catch (IllegalStateException ise)
799      {
800         assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
801               CONNECTION_MUST_BE_ESTABLISHED_OR_RELEASED,
802               ise.getMessage());
803      }
804
805      // executed twice
806      mHttpConnection.establishConnection(
807            DEFAULT_URL,
808            CONNECT_TIMEOUT, READ_TIMEOUT);
809
810      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
811      header.addRequestHeader(
812            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
813      mHttpConnection.setRequestResponseHeader(header);
814
815      mHttpConnection.setRequestBody(
816            new ByteArrayInputStream("This is the message".getBytes()));
817
818      try
819      {
820         mHttpConnection.execute();
821         mHttpConnection.execute();
822         fail(ILLEGALSTATEEXCEPTION_EXPECTED);
823      }
824      catch (IllegalStateException ise)
825      {
826         assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
827               CONNECTION_MUST_BE_ESTABLISHED_OR_RELEASED,
828               ise.getMessage());
829      }
830      mHttpConnection.closeConnection();
831   }
832
833   /**
834    * Performs establishing a connection (to SubwayCA) , creating a request,
835    * sending that request, receiving the response and closing the connection
836    * USING SSL.
837    * @throws Exception
838    *          in case of missing keystore
839    */
840   public void testSendSimplePostRequestWithSSL ()
841         throws Exception
842   {
843      final String keyStoreFilename = getBaseDir()
844            + FILE_SEPARATOR + "test" + FILE_SEPARATOR + "data"
845            + FILE_SEPARATOR + "ssl_store.jks";
846      final String trustStoreFilename = getBaseDir()
847            + FILE_SEPARATOR + "test" + FILE_SEPARATOR + "data"
848            + FILE_SEPARATOR + "fwk_trusted.jks";
849      System.setProperty("javax.net.ssl.keyStore", keyStoreFilename);
850      System.setProperty("javax.net.ssl.trustStore", trustStoreFilename);
851      System.setProperty("javax.net.ssl.trustStorePassword", "fawkez42");
852      System.setProperty("javax.net.ssl.keyStorePassword", "fawkez42");
853      mHttpConnection.initSsl("ssl", "sslssl");
854
855      // connect
856      mHttpConnection.establishConnection(DEFAULT_URL,
857            CONNECT_TIMEOUT, READ_TIMEOUT);
858      mHttpConnection.setRequestBody(new ByteArrayInputStream(SIMPLE_BODY));
859
860      final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
861      header.addRequestHeader(
862            CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
863      header.addRequestHeader(
864            CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
865      mHttpConnection.setRequestResponseHeader(header);
866
867      // execute
868      mHttpConnection.execute();
869      final String response = getResponseBodyAsString();
870      final String connectionValue
871            = mHttpConnection.getResponseHeader(CONNECTION_PARAMETER);
872
873      // close
874      mHttpConnection.closeConnection();
875
876      assertNotNull(NO_MESSAGE_BODY_IN_RESPONSE, response);
877      assertNotNull(ATTRIBUTE_CONNECTION_MISSING, connectionValue);
878      assertEquals(CONNECTION_CLOSE_EXPECTED,
879            "close",
880            connectionValue.toLowerCase(Constants.SYSTEM_LOCALE));
881   }
882
883   /**
884    * Gets the response body as String.
885    * @return String
886    *          the response body as string
887    */
888   private String getResponseBodyAsString ()
889         throws HttpEmptyResponseException
890   {
891      return StringUtil.toString(mHttpConnection.getResponseBody());
892   }
893}
Note: See TracBrowser for help on using the browser.