英文:
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 中添加这些框架?
英文:
I am trying to mock a html which includes frames using wiremock. These html files on resources/html folder. The html class is below,
<!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>
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("/([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);
}
and what I get it:
So my question is, how can I add these frames on Wiremock?
答案1
得分: 1
你的问题出在错误的正则表达式匹配。
/([a-z]*)
会匹配到 matchdetail
和 html
,但不会匹配 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/.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论