integration-testingjunit5payarapayara-micro

How to add HTTP header to MicroShedTest and SharedContainerConfig JUnit tests


I have integration tests that have been running smoothly until I added security to my application. The security uses a custom generated api key and validation is done in a custom HttpAuthenticationMechanism from header 'X-API-Key'.

I need to find out if it is possible to add headers to calls made by the test suite. I have checked the internet and all I found was @BasicAuthConfig and @JwtConfig which was not of any use.

I need to added a header 'X-API-Key' to http calls made to the containers.


Solution

  • I did not find anything useful so I created my own solution. Instead of relying on @RESTClient to give me the resource proxy I created my own like so:

    public static <T> T getResourceProxy(Class<T> t) {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("X-API-Key", "abcdefg.abcdefg1234567hij890");
        headerMap.put("Content-Type", "application/json");
        headerMap.put("Accept", "application/json, text/plain");
    
        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
        bean.setHeaders(headerMap);
        bean.setResourceClass(t);
        bean.setAddress("http://localhost:8080/myApp");
    
        List<Object> providers = new ArrayList<>();
    
        providers.add(new JacksonJaxbJsonProvider());
        providers.add(new JacksonJsonProvider());
        bean.setProviders(providers);
    
        return bean.create(t);
    }