我正在尝试进行集成测试,以查看我的注册端点失败时的行为。我的注册端点是由外部源(由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\": 1265\n" +
"\"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: null\n" +
"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在向public/registration
端点发出请求之前获取访问令牌的原因。如果我遗漏任何东西,请告诉我。
感谢。
答案 0 :(得分:0)
您可以做的一件事是模拟OAuth2AccessToken
或OAuth2AccessToken
答案 1 :(得分:0)
我通过覆盖UserInfoRestTemplateFactory
并使OAuth2RestTemplate
成为一个bean来解决它。从Spring Boot 1.5.0开始提供。
示例:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class OAuth2Test {
@TestConfiguration
static class OAuth2TestConfiguration {
@Bean
UserInfoRestTemplateFactory userInfoRestTemplateFactory(
ObjectProvider<List<UserInfoRestTemplateCustomizer>> customizers,
ObjectProvider<OAuth2ProtectedResourceDetails> details,
ObjectProvider<OAuth2ClientContext> context) {
return new DefaultUserInfoRestTemplateFactory(customizers, details, context);
}
@Bean
OAuth2RestTemplate oAuth2RestTemplate(UserInfoRestTemplateFactory factory) {
return factory.getUserInfoRestTemplate();
}
}
@Autowired
private ResourceServerProperties resourceServerProperties;
@Autowired
private MockMvc mockMvc;
@Autowired
private OAuth2RestTemplate oAuth2RestTemplate;
@Before
public void setUp() throws Exception {
createServer(oAuth2RestTemplate)
.expect(requestTo(resourceServerProperties.getUserInfoUri()))
.andExpect(header(HttpHeaders.AUTHORIZATION, OAuth2AccessToken.BEARER_TYPE + " " + ACCESS_TOKEN))
.andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
}
@Test
public void testProtectedResource() throws Exception {
mockMvc.perform(get("/protected-resource").with(oauth2Token()))
.andExpect(status().isNotFound());
}
private RequestPostProcessor oauth2Token() {
return request -> {
request.addHeader(HttpHeaders.AUTHORIZATION, OAuth2AccessToken.BEARER_TYPE + " " + ACCESS_TOKEN);
return request;
};
}
}
答案 2 :(得分:0)
设置自己的访问令牌对我有用,这阻止了OAuth2RestTemplate本身获取访问令牌:
@Before
public void before() {
mockServer = MockRestServiceServer.createServer(oAuth2RestTemplate);
oAuth2RestTemplate.getOAuth2ClientContext().setAccessToken(
new DefaultOAuth2AccessToken("accesstoken")
);
}