代码之家  ›  专栏  ›  技术社区  ›  dcp

java中的模拟https请求

  •  5
  • dcp  · 技术社区  · 14 年前

    假设我正在写一个应用程序,我需要能够做这样的事情:

    String url = "https://someurl/";
    GetMethod method = new GetMethod(URLEncoder.encode(url));
    String content = method.getResponseBodyAsString();
    

    有没有办法提供一个模拟服务器,让我处理https请求?我要寻找的是一种编写单元测试的方法,但我需要能够模拟实际用于测试的部分 https://someurl 所以我可以得到一个已知的回应。

    9 回复  |  直到 14 年前
        1
  •  3
  •   Pablojim    14 年前

    基本上有两种选择:

    1抽象对框架的调用并测试它。

    2作为测试的一部分启动应用程序服务器,然后对其运行方法。(这将更像是一个集成测试)

    这可以通过多种方式实现。这可以是测试外部的,例如maven jetty插件。或者测试可以通过编程方式启动服务器。请参见: http://docs.codehaus.org/display/JETTY/Embedding+Jetty

    就我个人而言,我会选择选项1—您正在尝试测试外部库的功能。这通常是不必要的。另外,将依赖项抽象出来到外部库也是一种很好的做法。

    希望这有帮助。

        2
  •  5
  •   Jan Dudek    11 年前

    http://jadler.net ),一个http存根/模拟库,我已经工作了一段时间。1.0.0稳定版刚刚发布,它应该提供您要求的功能:

    @Test
    public void getAccount() {
    
        onRequest()
            .havingMethodEqualTo("GET")
            .havingURIEqualTo("/accounts/1")
            .havingBody(isEmptyOrNullString())
            .havingHeaderEqualTo("Accept", "application/json")
        .respond()
            .withTimeout(2, SECONDS)
            .withStatus(200)
            .withBody("{\"account\":{\"id\" : 1}}")
            .withEncoding(Charset.forName("UTF-8"))
            .withContentType("application/json; charset=UTF-8");
    
        final AccountService service = new AccountServiceRestImpl("http", "localhost", port());
        final Account account = service.getAccount(1);
    
        assertThat(account, is(notNullValue()));
        assertThat(account.getId(), is(1));
    }
    
    
    @Test
    public void deleteAccount() {
    
        onRequest()
            .havingMethodEqualTo("DELETE")
            .havingPathEqualTo("/accounts/1")
        .respond()
            .withStatus(204);
    
        final AccountService service = new AccountServiceRestImpl("http", "localhost", port());
        service.deleteAccount(1);
    
        verifyThatRequest()
            .havingMethodEqualTo("DELETE")
            .havingPathEqualTo("/accounts/1")
        .receivedOnce();
    }
    
        3
  •  2
  •   hvgotcodes    14 年前

    GetMethod 
    

    延伸

    HttpMethod
    

    所以你可以很容易地用你最喜欢的模拟库来模拟它。你的

    method.getResponseBodyAsString()
    

        4
  •  1
  •   Codemwnci    14 年前

    看看JWebUnit http://jwebunit.sourceforge.net/

    public class ExampleWebTestCase extends WebTestCase {
        public void setUp() {
            super.setUp();
            setBaseUrl("http://localhost:8080/test");
        }
    
        public void test1() {
            beginAt("/home");
            clickLink("login");
            assertTitleEquals("Login");
            setTextField("username", "test");
            setTextField("password", "test123");
            submit();
            assertTitleEquals("Welcome, test!");
        }
    }
    
        5
  •  1
  •   zigdon    14 年前

    thttpd GetMethod ,然后您就可以对其进行模拟,而不必为所有测试实际配备远程服务器。

        6
  •  1
  •   micdah    14 年前

    jMock .

    class GetMethodTest {
    @Rule public JUnitRuleMockery context = new JunitRuleMockery();
    
    @Test
    public void testGetMethod() throws Exception {
        // Setup mocked object with expectations
        final GetMethod method = context.mock(GetMethod.class);
        context.checking(new Expectations() {{
            oneOf (method).getResponseBodyAsString();
            will(returnValue("Response text goes here"));
        }});
    
        // Now do the checking against mocked object
        String content = method.getResponseBodyAsString();
    }
    }
    
        7
  •  1
  •   naumcho    14 年前

    您可以将代码包装到某个类中WebClient.getUrl()然后模仿(例如。 jmock

    expectation {
       oneOf("https://someurl/"), will(returnValue(someHTML));
    }
    
        8
  •  0
  •   user3406366    11 年前

    使用xml模拟存根服务器,它可以根据请求参数、头等模拟静态http响应。配置和使用它非常简单。

    http://xmlmimic.sourceforge.net/ http://sourceforge.net/projects/xmlmimic/