So I'm using the following code in my own HtmlUnitDriver and thought it might be useful for other people as well.
My main use cases are all debugging related like:
Thought I share this here ... feel free to close if the consent is that it's not needed (since everyone can already do what I'm doing here with a bit more effort)
public class CustomHtmlUnitDriver extends HtmlUnitDriver {
private List<IHtmlUnitRequestInterceptor> requestInterceptors = new ArrayList<>();
public CustomHtmlUnitDriver() {
super(true);
try (WebConnectionWrapper conWrapper = new WebConnectionWrapper(getWebClient()) {
@Override
public WebResponse getResponse(WebRequest request) throws IOException {
requestInterceptors.forEach(interceptor -> interceptor.onBeforeRequest(request));
WebResponse response = super.getResponse(request);
requestInterceptors.forEach(interceptor -> interceptor.onAfterRequest(request, response));
return response;
}
}) {
} catch (IOException e) {
throw new RuntimeException("unable to close connectionWrapper", e);
}
}
public void registerRequestInterceptor(IHtmlUnitRequestInterceptor requestInterceptor) {
requestInterceptors.add(requestInterceptor);
}
public static interface IHtmlUnitRequestInterceptor {
/**
* Will be called for every request. Response has not being asked for yet.
*/
void onBeforeRequest(WebRequest webRequest);
/**
* Callback after an request.
*/
void onAfterRequest(WebRequest webRequest, WebResponse response);
}
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too