Java – simulation server for spring OAuth endpoint
I'm trying integration testing to see how my registered endpoint behaves when it fails My registered endpoint is an API provided by an external source (protected by spring OAuth) The client website uses spring OAuth to communicate with the API
What I'm trying to do is laugh at the API. However, the problem I encounter is that the request will not be directed to the simulated endpoint; org. springframework. web. client. Resourceaccessexception: exception in post request“ http://localhost:8081/oauth/token ”I / O error: connection rejected; The nested exception is Java net. Connectexception: connection rejected This is my test as follows:
@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(); } } }
Please note that the reason I set two expectations for the impersonation web server is that spring OAuth obtains the access token before making a request to the public / registered endpoint If I miss anything, please let me know
thank you.
Solution
One thing you can do is simulate oauth2accesstoken or oauth2accesstoken