Java tutorial
/** * The MIT License (MIT) * * Copyright (c) <2013> <Cryptable> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ package org.cryptable.pki.communication.http; import org.apache.http.HttpResponse; import org.apache.http.ProtocolVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHttpResponse; import org.apache.http.message.BasicStatusLine; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; import java.io.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * Test the http communication module * * Created with IntelliJ IDEA. * User: davidtillemans * Date: 9/06/13 * Time: 14:36 * To change this template use File | Settings | File Templates. */ public class PKIHTTPCommunicationTest { private HttpResponse prepareResponse(int expectedResponseStatus, String expectedResponseBody) { HttpResponse response = new BasicHttpResponse( new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), expectedResponseStatus, "")); response.setStatusCode(expectedResponseStatus); try { response.setEntity(new StringEntity(expectedResponseBody)); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } return response; } @Test public void testHTTPSendMessage() throws IOException { byte[] testMsg = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!' }; PKIHTTPCommunication pkihttpCommunication = new PKIHTTPCommunication("http://localhost:8080/cmp"); final HttpClient httpClient = mock(HttpClient.class); HttpResponse response = prepareResponse(200, "Successful call"); when(httpClient.execute(Mockito.any(HttpPost.class))).thenReturn(response); pkihttpCommunication.setHttpClient(httpClient); pkihttpCommunication.sendReceiveMsg(testMsg); InputStream is = pkihttpCommunication.getHttpPost().getEntity().getContent(); byte[] data = new byte[(int) pkihttpCommunication.getHttpPost().getEntity().getContentLength()]; is.read(data); Assert.assertArrayEquals(testMsg, data); Assert.assertEquals(testMsg.length, pkihttpCommunication.getHttpPost().getEntity().getContentLength()); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); InputStream isResponse = response.getEntity().getContent(); byte[] dataResponse = new byte[(int) response.getEntity().getContentLength()]; isResponse.read(dataResponse); Assert.assertArrayEquals("Successful call".getBytes(), dataResponse); } }