问题描述
我正在使用 wiremock 来模拟端点,如果我将正确的属性提供给 oauth2resttemplate 以获取实际的身份验证令牌,则它可以成功运行。如果我提供一些模拟细节,那么我的休息模板不会像模拟调用那样深入。
我知道我可能需要模拟身份验证网址,但不确定要返回什么..
my.service.clientId=<real-ID>
my.service.clientSecret=<real-secret>
my.service.audience=<real-audience>
my.service.BaseUrl=http://<real token url>/as/token.oauth2
my.service.url=http://localhost:8080/something/ #this stubs the actual endpoint
my.service.grantType=client_credentials
my.service.scopes=something.read
oauth2resttemplate oauth2resttemplate = OauthTemplateUtil.createOauthRestTemplate(oauthProperties);
在我的测试中,我像这样配置存根
private void configureStubs() {
configureFor("localhost",8080);
stubFor(get(urlEqualTo("/<path>"))
.willReturn(aResponse().withBody("<my-json-response>");
}
这工作正常,但我显然不想使用真正的客户端和秘密并为我的测试进行实际调用,我认为我需要存根 my.service.BaseUrl 但不确定我应该返回什么从它回来,只有200?我需要返回一个假令牌吗?它应该是什么样子的?
谢谢
解决方法
这是如何配置以使用休息模板模拟 OauthProperties
my.service.clientId=mock-id
my.service.clientSecret=mock-secret
my.service.audience=mock-audience
my.service.BaseUrl=http://http://localhost:8080/as/token.oauth2
my.service.url=http://localhost:8080/something/
my.service.grantType=client_credentials
my.service.scopes=some.scope
stubFor(post(urlEqualTo("/as/token.oauth2?aud=https://mock-audience"))
.willReturn(okJson("{\"token_type\": \"Bearer\",\"access_token\":\"{{randomValue length=20 type='ALPHANUMERIC'}}\"}")));