Test spring rest controller secured with https

Pratap A.K picture Pratap A.K · Dec 22, 2015 · Viewed 8.3k times · Source

Can anybody provide me with a code sample to write integration test for a controller which is secured with HTTPS?? With HTTP I am able to write, but with HTTPS I am getting certification error.

Controller

@RestController
@RequestMapping("/rest/otm/v1")
public class LoginController {  

    @Autowired
    UserAuthenticationService userAuthService;

    @ExceptionHandler({ AuthenticationFailedException.class})
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public void login(HttpServletRequest request,HttpServletResponse response) throws AuthenticationDeniedException {

        //some code
    }

}

Test Class

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0") 
public class IdentityControllerTest {

    @Value("${local.server.port}")
    int port;

    @Test
    public void test_Login_Controller() {

        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("some-proxy", 8080));
        clientHttpRequestFactory.setProxy(proxy);

        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("credential", "pratap");
        requestHeaders.set("deviceid", "xyz123");

        HttpHeaders response = restTemplate.postForObject("https://localhost:"+port+"/rest/otm/v1/login", requestHeaders, HttpHeaders.class);               

    }

}

Error

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://localhost:51184/rest/otm/v1/login":sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:607)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:357)
    at com.bosch.inl.otm.controller.IdentityControllerTest.test_Login_Controller(IdentityControllerTest.java:45)

Thanks in advance

Answer

Guy Bouallet picture Guy Bouallet · Dec 22, 2015

This error is generally reported in case of using a self signed certificate. By default, Java does not trust it so you have to either disable the certificate validation or add the certificate to the trust store. Both solution have been described here.