根据RFC 3986的无效URI示例

时间:2012-02-27 04:42:47

标签: java spring-mvc rfc3986

对于我的单元测试,我想找到一个无效URI的例子,它无法匹配来自Spring MVC 3.1.1的UriComponentsBuilder#URI_PATTERN正则表达式:

private static final String SCHEME_PATTERN = "([^:/?#]+):";

private static final String HTTP_PATTERN = "(http|https):";

private static final String USERINFO_PATTERN = "([^@/]*)";

private static final String HOST_PATTERN = "([^/?#:]*)";

private static final String PORT_PATTERN = "(\\d*)";

private static final String PATH_PATTERN = "([^?#]*)";

private static final String QUERY_PATTERN = "([^#]*)";

private static final String LAST_PATTERN = "(.*)";

// Regex patterns that matches URIs. See RFC 3986, appendix B
private static final Pattern URI_PATTERN = Pattern.compile(
        "^(" + SCHEME_PATTERN + ")?" + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN +
                ")?" + ")?" + PATH_PATTERN + "(\\?" + QUERY_PATTERN + ")?" + "(#" + LAST_PATTERN + ")?");

我很难找到一个字符串,导致UriComponentsBuilder.fromUriString()失败IllegalArgumentException。我尝试的任何东西都将正则表达式检查作为有效的URI。

1 个答案:

答案 0 :(得分:3)

我认为任何东西都会匹配正则表达式。问题是正则表达式没有严格执行RFC。相反,它被设计为宽容的;即接受任何旧垃圾并尽最大努力将其解析为URL。

(但是,我注意到它不能很好地解析非HTTP URI。看看SCHEME_PATTERN ......然后哭。)

如果您想对URI进行严格的解析或验证,最好使用java.net.URI。正则表达式不是执行此任务的正确工具。