微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Wiremock模拟框架HTML?

如何解决如何使用Wiremock模拟框架HTML?

我正在尝试使用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);
  }

以及我得到的是什么

enter image description here

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

解决方法

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

/([a-z]*)将匹配matchdetailhtml,但不匹配matchdetail.htmlframe1.html。取而代之的是,您可以根据自己的精确度来进行([a-z0-9.]*)(.*)的匹配组。如果您无法获得预期的匹配结果,建议您使用正则表达式工具。我的个人关注对象是https://regex101.com/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。