Created
December 6, 2011 10:03
-
-
Save vglebov/1437631 to your computer and use it in GitHub Desktop.
MockHttpServer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.smartjobboard.jira.plugins; | |
| import org.mortbay.jetty.HttpConnection; | |
| import org.mortbay.jetty.Request; | |
| import org.mortbay.jetty.Server; | |
| import org.mortbay.jetty.handler.AbstractHandler; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.io.IOException; | |
| import java.util.Hashtable; | |
| import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; | |
| import static javax.servlet.http.HttpServletResponse.SC_OK; | |
| import static org.apache.commons.io.IOUtils.write; | |
| /** | |
| * Created by IntelliJ IDEA. | |
| * User: vglebov | |
| * Date: 12/6/11 | |
| * Time: 2:41 PM | |
| */ | |
| public class MockHttpServer { | |
| private Server server; | |
| private Hashtable<String, String> map = new Hashtable<String, String>(); | |
| public MockHttpServer(int port) { | |
| server = new Server(port); | |
| server.addHandler(new AbstractHandler() { | |
| @Override | |
| public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { | |
| Request baseRequest = request instanceof Request ? (Request) request : HttpConnection.getCurrentConnection().getRequest(); | |
| //TODO использовать requestString = IOUtils.toString(baseRequest.getInputStream()); для отладки | |
| String uri = baseRequest.getUri().toString(); | |
| //TODO использовать baseRequest.getParameters() для уточнения запроса | |
| if (map.containsKey(uri)) { | |
| response.setStatus(SC_OK); | |
| response.setContentType("text/xml;charset=utf-8"); | |
| String responseString = map.get(uri); | |
| write(responseString, response.getOutputStream()); | |
| } else { | |
| response.setStatus(SC_NOT_FOUND); | |
| write("", response.getOutputStream()); | |
| } | |
| baseRequest.setHandled(true); | |
| } | |
| }); | |
| try { | |
| server.start(); | |
| } catch (Exception e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| public void setHandler(String uri, final String responseString) { | |
| map.put(uri, responseString); | |
| } | |
| public void stop() { | |
| try { | |
| server.stop(); | |
| } catch (Exception e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.smartjobboard.jira.plugins; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import java.util.List; | |
| import static org.junit.Assert.assertEquals; | |
| /** | |
| * Created by IntelliJ IDEA. | |
| * User: vglebov | |
| * Date: 11/24/11 | |
| * Time: 1:39 PM | |
| */ | |
| public class TestWhmcs { | |
| private WhmcsImpl api; | |
| private MockHttpServer mockHttpServer; | |
| @Before | |
| public void setup() throws Exception { | |
| mockHttpServer = new MockHttpServer(55555); | |
| mockHttpServer.setHandler("/whmcs/api.php", "<?xml version=\"1.0\" encoding=\"utf-8\"?><whmcsapi version=\"4.5.1\"><action>getclients</action><result>success</result><totalresults>512</totalresults><startnumber>0</startnumber><numreturned>5</numreturned><clients>...</clients></whmcsapi>"); | |
| api = new WhmcsImpl("http://localhost:55555/whmcs/api.php", "user", "password"); | |
| } | |
| @After | |
| public void teardown() throws Exception { | |
| mockHttpServer.stop(); | |
| } | |
| @Test | |
| public void testGetClients() throws Exception { | |
| List<Client> clients = api.GetClients(5); | |
| assertEquals(5, clients.size()); | |
| } | |
| @Test | |
| public void testGetClientsCount() throws Exception { | |
| Integer count = api.GetClientsCount(); | |
| assertEquals(512, count.intValue()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment