Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitutable HTTP client #2455

Merged
merged 5 commits into from
Nov 2, 2023
Merged

Substitutable HTTP client #2455

merged 5 commits into from
Nov 2, 2023

Conversation

tomakehurst
Copy link
Member

@tomakehurst tomakehurst commented Oct 19, 2023

This change creates an HttpClient abstraction within WireMock with a default Apache backed implementation. This is used in the proxy renderer and webhooks extension in place of the original directly referenced Apache clients.

The HTTP client factory and a default implementation are made available as services to extensions.

The factory for the client can be substituted via the Options object or via an extension, following the same pattern as for the HTTP server.

Doing this enables a few useful things:

  • Allows full customisation of the client if you want to e.g. add custom security logic and wrap calls in common behaviour.
  • Paves the way for a "dependency light" version of WireMock that uses Java's HTTP client and server. This could also make a better Android build.
  • Means you can supply network rules programmatically if you need to express a rule that's not possible with the current declarative rules code.
  • The gRPC extension can start to support record and playback (by wrapping the client and translating to/from proto).

throws IOException {
final List<HttpHeader> headers =
Arrays.stream(apacheResponse.getHeaders())
.collect(groupingBy(NameValuePair::getName))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this is case sensitive - perhaps lower case the name?

}

protected ImmutableRequest(
String absouteUrl,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo, should be absoluteUrl

byte[] body,
boolean multipart,
boolean browserProxyRequest) {
this.absouteUrl = absouteUrl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use Objects.requireNonNull for all the fields that can't be null?


HttpUriRequest httpRequest = getHttpRequestFor(responseDefinition);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think HttpClientFactory.getHttpRequestFor is now unused

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna leave this in case anybody depends on it I think

@Mahoney
Copy link
Collaborator

Mahoney commented Oct 24, 2023

Minor comments, looks great otherwise

…and made ProxyResponseRenderer use this instead of the Apache client directly. Still to do: make this configurable via Options/extension.
…bject and via an extension in the same manner has HttpServerFactory, allowing the client factory to be set via options or via an extension.
…this instead of its own hard-coded http client.
…, fixed a typo, wrote a test to demonstrate correct header case handling
@tomakehurst tomakehurst marked this pull request as ready for review November 2, 2023 13:51
@tomakehurst tomakehurst requested a review from a team as a code owner November 2, 2023 13:51
@tomakehurst tomakehurst merged commit 4719dd7 into master Nov 2, 2023
7 checks passed
@tomakehurst tomakehurst deleted the substitutable-http-client branch November 2, 2023 13:54
henrik242 pushed a commit to henrik242/wiremock that referenced this pull request Nov 10, 2023
* Created an HttpClient interface with an Apache client implementation and made ProxyResponseRenderer use this instead of the Apache client directly.

* Added an HttpClientFactory interface that's returned by the options object and via an extension in the same manner has HttpServerFactory, allowing the client factory to be set via options or via an extension.

* Made HttpClient a service passed to extensions and made webhooks use this instead of its own hard-coded http client.
Marvin9 pushed a commit to Marvin9/wiremock that referenced this pull request Nov 20, 2023
* Created an HttpClient interface with an Apache client implementation and made ProxyResponseRenderer use this instead of the Apache client directly.

* Added an HttpClientFactory interface that's returned by the options object and via an extension in the same manner has HttpServerFactory, allowing the client factory to be set via options or via an extension.

* Made HttpClient a service passed to extensions and made webhooks use this instead of its own hard-coded http client.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants