问题描述
我正在尝试使用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);
}
以及我得到的是什么
解决方法
您的问题来自不正确的正则表达式匹配。
/([a-z]*)
将匹配matchdetail
和html
,但不匹配matchdetail.html
或frame1.html
。取而代之的是,您可以根据自己的精确度来进行([a-z0-9.]*)
或(.*)
的匹配组。如果您无法获得预期的匹配结果,建议您使用正则表达式工具。我的个人关注对象是https://regex101.com/。