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

ConsoleNotifyingWiremockNetworkTrafficListener accepts custom encoding #2139

Conversation

gsmith85
Copy link
Contributor

Allows specification of a custom Charset for on
ConsoleNotifyingWiremockNetworkTrafficListener allowing flexibility for service mocks that include non UTF-8 characters in their responses.

Additionally:

  • Adds test coverage for ConsoleNotifyingWiremockNetworkTrafficListener.
  • Changes behavior of incoming and outgoing so that if a ByteBuffer with a Charset other than the one specified by
    ConsoleNotifyingWiremockNetworkTrafficListener is provided, a succinct message is logged. This replaces the previous behavior that dumped the entire CharacterCodingException. This was extremely noisy and did not provide useful additional diagnostics.

Fixes #1541

Allows specification of a custom `Charset` for on
`ConsoleNotifyingWiremockNetworkTrafficListener` allowing flexibility
for service mocks that include non UTF-8 characters in their responses.

Additionally:
- Adds test coverage for `ConsoleNotifyingWiremockNetworkTrafficListener`.
- Changes behavior of `incoming` and `outgoing` so that if a ByteBuffer
with a Charset other than the one specified by
`ConsoleNotifyingWiremockNetworkTrafficListener` is provided, a succinct
message is logged. This replaces the previous behavior that dumped the
entire CharacterCodingException. This was extremely noisy and did not
provide useful additional diagnostics.

Fixes wiremock#1541
Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

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

Looks good to me. Will delve into the merge conflict

private final Charset charset;
private final CharsetDecoder charsetDecoder;

ConsoleNotifyingWiremockNetworkTrafficListener(Notifier notifier, Charset charset) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe public too? Can be done later if needed

Copy link
Contributor Author

@gsmith85 gsmith85 Jul 18, 2023

Choose a reason for hiding this comment

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

This choice was made around the fact that this type is named ConsoleNotifying*. It seemed off to allow for instantiating it with a custom Notifier that could notify to places other than the console. So per that consideration and Effective Java Item 13, I'm intentionally minimizing accessibility.

I do think there's potentially an opportunity here to create a simple or skeletal type that would expose the ability to create a WiremockNetworkTrafficListener with an arbitrary Notifier/ Charset combo.

I'd probably have that be structured as:

public final class WiremockNetworkTrafficListeners {
  private WiremockNetworkTrafficListeners() {}

  public static create(Notifier notifier, CharSet charset) {
    return new SimpleWiremockNetworkTrafficListener(notifier, charset);
  }

  public static createConsoleNotifying() {
    return new SimpleWiremockNetworkTrafficListener(DEFAULT_CONSOLE_NOTIFIER, DEFAULT_CHARSET)
  }
  
  public static createConsoleNotifying(CharSet charset) {
    return new SimpleWiremockNetworkTrafficListener(DEFAULT_CONSOLE_NOTIFIER, charset)
  }
}

class SimpleWiremockNetworkTrafficListener implements WiremockNetworkTrafficListener {
  // Basically all the implementation that currently exists in this pull request ConsoleNotifying...
}

In Wiremock 2 you'd probably keep a shim ConsoleNotifyingNetworkTrafficListener type for backwards compatibility that would simply call through to WiremockNetworkTrafficListeners, but in 3 I'd drop the ConsoleNotifyingNetworkTrafficListener type altogether and exclusively rely on instantiation through the WiremockNetworkTrafficListeners factory pattern per EJ1.

Let me know what you think. But in any case, as long as it's okay with you let's do it as part of a separate PR?

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be a good improvement for WireMock's API extensibility!
If you are open to submitting a separate PR for that, indeed it would be the best approach

@oleg-nenashev oleg-nenashev requested a review from a team as a code owner July 17, 2023 11:25
@gsmith85
Copy link
Contributor Author

Fixed the Spotless issues @oleg-nenashev so hopefully with your approval and once CI runs we can get this in 🙌 .

@oleg-nenashev oleg-nenashev self-requested a review July 18, 2023 07:38
@gsmith85
Copy link
Contributor Author

Not sure why, but I unless I'm going crazy, I had to run Spotless a second time to completely address fix identified issues. So apologies for not clearing them all on last commit I think we should be good now.

@oleg-nenashev
Copy link
Member

Not sure why, but I unless I'm going crazy, I had to run Spotless a second time to completely address fix identified issues. So apologies for not clearing them all on last commit I think we should be good now.

No problems at all. Commits are free, we can always squash-merge if needed 😄
I am also a frequent victim of some of the rules we have enforced, so I totally understand

Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

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

LGTM, and thanks!

private final Charset charset;
private final CharsetDecoder charsetDecoder;

ConsoleNotifyingWiremockNetworkTrafficListener(Notifier notifier, Charset charset) {
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be a good improvement for WireMock's API extensibility!
If you are open to submitting a separate PR for that, indeed it would be the best approach

@oleg-nenashev oleg-nenashev merged commit 65e1768 into wiremock:master Jul 19, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants