1
私たちのプロジェクトでは、非常に珍しいREST APIを備えた外部システムを使用しています。 これは、URL内の角括弧が含まれていますURLのjava MockRestServiceServerエスケープ文字
api/v1/series?match[]=up
は、今、私たちは私たち自身のREST APIをテストするために、ちょうどこの外部システムの応答をモックとしたいです。 ユニットテストでMockRestServiceServerオブジェクトを使用しています。私たちのサービスインサイド
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));
我々だけで
restTemplate.getForObject(prometheusURL + "series?match[]=up", ResponseObjectForSeries.class);
として、この外部APIを呼び出すしかし、結果として、我々は次のエラーを持っている:
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の明白性を使用しようとしました。しかし、それは助けにはならない。
に
を使用して解決?コードを私たちと共有してもよろしいですか? –
私はmockServer.expect(ExpectedCount.manyTimes()、requestTo(UriComponentsBuilder.fromHttpUrl(prometheusURL + "series?match%5B%5D = up"))を書いたことを意味します。 2回エンコードされました –
toUri()を削除すると機能しますか? – user7294900