java – Spring Oauth端点的模拟服务器
发布时间:2020-12-15 02:10:28 所属栏目:Java 来源:网络整理
导读:我正在尝试进行集成测试,以查看我的注册端点失败时的行为.我的注册端点是由外部源(由 Spring OAuth保护)提供的API.客户端网站使用客户端Spring Oauth与API进行通信. 我正在尝试做的是嘲笑API然而,我遇到的问题是请求不会针对模拟的端点; org.springframework
我正在尝试进行集成测试,以查看我的注册端点失败时的行为.我的注册端点是由外部源(由
Spring OAuth保护)提供的API.客户端网站使用客户端Spring Oauth与API进行通信.
我正在尝试做的是嘲笑API然而,我遇到的问题是请求不会针对模拟的端点; org.springframework.web.client.ResourceAccessException:POST请求中的“http://localhost:8081/oauth/token”的I / O错误:连接被拒绝:连接;嵌套异常是java.net.ConnectException:连接被拒绝:连接.这是我的测试如下: @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath*:RegistrationControllerIntegrationTest-context.xml"}) public class RegistrationControllerIntegrationTest { @Resource RegistrationController registrationController; @Resource private WebApplicationContext webApplicationContext; MockMvc mockMvc; @Value("${oauth.accessTokenUri}") private String oauthUri; private MockRestServiceServer mockRestServiceServer; private OAuth2RestTemplate clientCredRest; @Resource(name = "clientCredentialRest") public void setClientCredRest(OAuth2RestTemplate clientCredRest) { this.clientCredRest = clientCredRest; } @Before public void setUp() { this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); this.mockRestServiceServer = MockRestServiceServer.createServer(this.clientCredRest); } @Test public void testRegistrationThatReturnsBadRequestWhenUserAlreadyExist() { this.mockRestServiceServer.expect(MockRestRequestMatchers.requestTo("localhost:8081/oauth/token")).andExpect(MockRestRequestMatchers.method(HttpMethod.POST)) .andRespond(MockRestResponseCreators.withSuccess().contentType(MediaType.APPLICATION_JSON).body("{n" + ""access_token": "8ecd93d4-2484-46de-922a-652fa79d027d",n" + ""token_type": "bearer",n" + ""expires_in": 1265n" + ""scope": "read write"n" + "}")); Gson gson = Converters.registerDateTime(new GsonBuilder()).create(); PodamFactory factory = new PodamFactoryImpl(); RegistrationDTO dto = factory.manufacturePojo(RegistrationDTO.class); dto.setUserName("test"); String json = gson.toJson(dto); this.mockRestServiceServer.expect(MockRestRequestMatchers.requestTo("localhost:8081/public/registration")).andExpect(MockRestRequestMatchers.method(HttpMethod.POST)) .andRespond(MockRestResponseCreators.withBadRequest().contentType(MediaType.APPLICATION_JSON).body("{n" + "resource: nulln" + "field: "user_name"n" + "code: "0"n" + "message: "Username already exist"n" + "}")); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/frontend/register").content(json).header("activate","true").header("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/45.0.2454.101 Safari/537.36").header("Origin","chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo").contentType(MediaType.APPLICATION_JSON); try { this.mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isBadRequest()); } catch (Exception e) { e.printStackTrace(); } } } 请注意,我为模拟Web服务器设置两个期望的原因是Spring Oauth在向公共/注册端点发出请求之前获取访问令牌的原因.如果我遗漏任何东西,请告诉我. 谢谢. 解决方法
您可以做的一件事是模拟OAuth2AccessToken或OAuth2AccessToken
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |