首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >url中的java MockRestServiceServer转义字符

url中的java MockRestServiceServer转义字符
EN

Stack Overflow用户
提问于 2017-07-27 00:10:23
回答 1查看 996关注 0票数 1

在我们的项目中,我们使用了外部系统和非常不寻常的REST API。它在url中包含方括号:

代码语言:javascript
复制
api/v1/series?match[]=up

现在我们要测试我们自己的REST api,并模拟这个外部系统的响应。所以我们在单元测试中使用了MockRestServiceServer对象。

代码语言:javascript
复制
    mockServer = MockRestServiceServer.createServer(restTemplate);
    mockServer.expect(ExpectedCount.manyTimes(), requestTo(UriComponentsBuilder.fromHttpUrl(prometheusURL + "series?match[]=up")
            .build().toUri()))
            .andExpect(method(HttpMethod.GET))
            .andRespond(withSuccess(promResponseMetricUpInfo, MediaType.APPLICATION_JSON));

在我们的服务中,我们只是将这个外部API调用为

代码语言:javascript
复制
restTemplate.getForObject(prometheusURL + "series?match[]=up", ResponseObjectForSeries.class);

但结果是我们出现了以下错误:

代码语言:javascript
复制
java.lang.AssertionError: Unexpected request expected:<http://localhost:9090/api/v1/series?match[]=up> but was:<http://localhost:9090/api/v1/series?match%5B%5D=up>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.match.MockRestRequestMatchers$5.match(MockRestRequestMatchers.java:121) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.DefaultRequestExpectation.match(DefaultRequestExpectation.java:84) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.SimpleRequestExpectationManager.validateRequestInternal(SimpleRequestExpectationManager.java:55) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.AbstractRequestExpectationManager.validateRequest(AbstractRequestExpectationManager.java:75) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.MockRestServiceServer$MockClientHttpRequestFactory$1.executeInternal(MockRestServiceServer.java:289) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.mock.http.client.MockClientHttpRequest.execute(MockClientHttpRequest.java:94) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:652) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]

我们怎样才能摆脱这些方括号呢?我尝试在测试中使用反斜杠或%5B%5D显式。但这并不管用。

EN

回答 1

Stack Overflow用户

发布于 2017-08-10 18:47:23

此问题通过以下方式解决

代码语言:javascript
复制
URLEncoder.encode()

在测试中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45332277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档