如何使用WireMock模拟一个包含框架HTML的页面?

huangapple go评论81阅读模式
英文:

How to mock a frame html using wiremock?

问题

我正在尝试使用 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模拟一个包含框架HTML的页面?

如此,我的问题是,我如何在 WireMock 中添加这些框架?

英文:

I am trying to mock a html which includes frames using wiremock. These html files on resources/html folder. The html class is below,

    &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Frameset//EN&quot;
        &quot;http://www.w3.org/TR/html4/frameset.dtd&quot;&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
    &lt;TITLE&gt;A simple frameset document&lt;/TITLE&gt;
&lt;/HEAD&gt;

&lt;frameset cols=&quot;20%, 80%&quot; rows=&quot;100&quot;&gt;
    &lt;frame id=&quot;leftFrame&quot; src=&quot;/frame1.html&quot;&gt;
    &lt;frame id=&quot;mainFrame&quot; src=&quot;/frame2.html&quot;&gt;
&lt;/frameset&gt;
&lt;/HTML&gt;

What I was trying to do is:

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

    wireMockServer.start();
  }

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

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

and what I get it:

如何使用WireMock模拟一个包含框架HTML的页面?

So my question is, how can I add these frames on Wiremock?

答案1

得分: 1

你的问题出在错误的正则表达式匹配。

/([a-z]*) 会匹配到 matchdetailhtml,但不会匹配 matchdetail.html 或者 frame1.html。你可以改为使用匹配组 ([a-z0-9.]*) 或者 (.*),具体取决于你需要多精确的匹配。如果你得不到预期的匹配结果,我建议你可以查看一个正则表达式工具。我个人常用的是 https://regex101.com/

英文:

Your issue comes from an incorrect regex match.

/([a-z]*) will match matchdetail and html, but not matchdetail.html or frame1.html. You can instead do a matching group of ([a-z0-9.]*) or (.*), depending on how precise you want to be. I'd advise you to check out a regex tool if you aren't getting the matches you'd expect. My personal go-to is https://regex101.com/.

huangapple
  • 本文由 发表于 2020年9月22日 17:21:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64006743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定