AEM cloud unit tests: how to create a SlingHttpServletRequest and set some headers?
Some of the service methods we use require passing in a request object, as they pull out various headers and parameters.
In our unit tests, we need to be able to create a request object, then set some headers on it.
There is the MockSligHttpServletRequest, but this cant be used as its not possible to set headers.
Constructor Summary
| MockSlingHttpServletRequest(String resourcePath, String selectors, String extension, String suffix, String queryString) |
This does now work:
SlingHttpServletRequest request = new SlingHttpServletRequest(); // fails because its abstract
This works, but then you cant set any headers:
import static org.mockito.Mockito.mock
:
SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);
This works, and you can set headers, but creates a HttpRequest, not a SlingHttpServletRequest, so I cant pass it into methods expecting a SlingHttpServletRequest
HttpRequest httpRequest = new BasicHttpRequest("GET", "http://dummy.html");
httpRequest.setHeader("x-token, "testtoken");
Any idea how to manually create a "dummy" request with specific headers in a unit test?