【问题标题】:How to mock a frame html using wiremock?如何使用wiremock模拟框架html?
【发布时间】:2020-09-22 09:21:35
【问题描述】:

我正在尝试使用wiremock 模拟一个包含框架的html。这些 html 文件位于 resources/html 文件夹中。 html类如下,

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
        "http://www.w3.org/TR/html4/frameset.dtd">
<HTML>
<HEAD>
    <TITLE>A simple frameset document</TITLE>
</HEAD>

<frameset cols="20%, 80%" rows="100">
    <frame id="leftFrame" src="/frame1.html">
    <frame id="mainFrame" src="/frame2.html">
</frameset>
</HTML>

我想做的是:

 public MockUtils(String folder, String fileName) throws IOException {
    wireMockServer = new WireMockServer(options().port(8081));
    wireMockServer.stubFor(
        get(urlPathMatching("/([a-z]*)"))
            .willReturn(
                aResponse()
                    .withBodyFile("framesample.html")
                    .withBody(prepareResponse(fileName, folder))));

    wireMockServer.start();
  }

  private static String prepareResponse(String path, String folder) throws IOException {
    return getResourceAsString(folder + "/" + path);
  }

  private static String getResourceAsString(String name) throws IOException {
    return Resources.toString(Resources.getResource(name), Charsets.UTF_8);
  }

我得到了什么:

所以我的问题是,如何在 Wiremock 上添加这些框架?

【问题讨论】:

    标签: java html testing frame wiremock


    【解决方案1】:

    您的问题来自不正确的正则表达式匹配。

    /([a-z]*) 将匹配 matchdetailhtml,但不匹配 matchdetail.htmlframe1.html。您可以改为使用([a-z0-9.]*)(.*) 的匹配组,具体取决于您想要的精确度。如果您没有得到您期望的匹配项,我建议您查看正则表达式工具。我个人的首选是https://regex101.com/

    【讨论】:

    • 谢谢,我的正则表达式不好。我已经更正了正则表达式,但现在它仍然不能正确显示帧。这些框架文件位于 test/resources/http 文件夹中。
    • 嗯...现在看,您正在响应 bodyFile 和 body。我会切换到只使用bodyFile。我认为您还必须更新文件路径。您需要更新默认的__files 目录,或者只是将文件移动到__files 下,默认情况下位于src/test/resources 下,因此如果您保留http 文件夹并将其移动到__files 下,我想应该是.withBodyFile("http/framesample.html")
    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多