Skip to content

Commit 1d37da0

Browse files
committed
Include more info when webhook refusal logged
Otherwise, there's no context to trace it back to a stub
1 parent 74e0bf2 commit 1d37da0

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

src/main/java/org/wiremock/webhooks/Webhooks.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.github.tomakehurst.wiremock.common.LocalNotifier.notifier;
1919
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2020
import static java.util.stream.Collectors.toList;
21+
import static org.apache.commons.lang3.ObjectUtils.firstNonNull;
2122

2223
import com.fasterxml.jackson.annotation.JsonCreator;
2324
import com.github.tomakehurst.wiremock.common.NetworkAddressRules;
@@ -48,6 +49,7 @@
4849
import org.apache.hc.core5.util.TimeValue;
4950
import org.apache.hc.core5.util.Timeout;
5051

52+
@SuppressWarnings("deprecation") // maintaining PostServeAction for backwards compatibility
5153
public class Webhooks extends PostServeAction implements ServeEventListener {
5254

5355
private final ScheduledExecutorService scheduler;
@@ -79,6 +81,7 @@ public Webhooks() {
7981
this(NetworkAddressRules.ALLOW_ALL);
8082
}
8183

84+
@SuppressWarnings("unused") // public API
8285
public Webhooks(WebhookTransformer... transformers) {
8386
this(Arrays.asList(transformers), NetworkAddressRules.ALLOW_ALL);
8487
}
@@ -149,7 +152,14 @@ private void triggerWebhook(ServeEvent serveEvent, Parameters parameters) {
149152
response.getCode(),
150153
EntityUtils.toString(response.getEntity())));
151154
} catch (ProhibitedNetworkAddressException e) {
152-
notifier.error("The target webhook address is denied in WireMock's configuration.");
155+
notifier.error(
156+
String.format(
157+
"The target webhook address %s specified by stub %s is denied in WireMock's configuration.",
158+
finalDefinition.getUrl(),
159+
firstNonNull(
160+
serveEvent.getStubMapping().getName(),
161+
serveEvent.getStubMapping().getId(),
162+
"<no name or id>")));
153163
} catch (Exception e) {
154164
notifier.error(
155165
String.format(

src/test/java/com/github/tomakehurst/wiremock/WebhooksAcceptanceViaPostServeActionTest.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.github.tomakehurst.wiremock.http.RequestMethod;
3939
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
4040
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
41+
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
4142
import com.github.tomakehurst.wiremock.testsupport.CompositeNotifier;
4243
import com.github.tomakehurst.wiremock.testsupport.TestNotifier;
4344
import com.github.tomakehurst.wiremock.testsupport.WireMockTestClient;
@@ -365,17 +366,18 @@ public void addsRandomDelayViaJSON() throws Exception {
365366

366367
@Test
367368
public void doesNotFireAWebhookWhenRequestedForDeniedTarget() throws Exception {
368-
rule.stubFor(
369-
post(urlPathEqualTo("/webhook"))
370-
.willReturn(aResponse().withStatus(200))
371-
.withPostServeAction(
372-
"webhook",
373-
webhook()
374-
.withMethod(POST)
375-
.withUrl("http://169.254.2.34/foo")
376-
.withHeader("Content-Type", "application/json")
377-
.withHeader("X-Multi", "one", "two")
378-
.withBody("{ \"result\": \"SUCCESS\" }")));
369+
StubMapping stub =
370+
rule.stubFor(
371+
post(urlPathEqualTo("/webhook"))
372+
.willReturn(aResponse().withStatus(200))
373+
.withPostServeAction(
374+
"webhook",
375+
webhook()
376+
.withMethod(POST)
377+
.withUrl("http://169.254.2.34/foo")
378+
.withHeader("Content-Type", "application/json")
379+
.withHeader("X-Multi", "one", "two")
380+
.withBody("{ \"result\": \"SUCCESS\" }")));
379381

380382
client.post("/webhook", new StringEntity("", TEXT_PLAIN));
381383

@@ -389,7 +391,10 @@ public void doesNotFireAWebhookWhenRequestedForDeniedTarget() throws Exception {
389391
await().until(() -> testNotifier.getErrorMessages(), hasSize(greaterThanOrEqualTo(1)));
390392
assertThat(
391393
errorMessages.get(0),
392-
is("The target webhook address is denied in WireMock's configuration."));
394+
is(
395+
"The target webhook address http://169.254.2.34/foo specified by stub "
396+
+ stub.getId()
397+
+ " is denied in WireMock's configuration."));
393398
}
394399

395400
private void waitForRequestToTargetServer() throws Exception {

src/test/java/com/github/tomakehurst/wiremock/WebhooksAcceptanceViaServeEventTest.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.github.tomakehurst.wiremock.http.RequestMethod;
3939
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
4040
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
41+
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
4142
import com.github.tomakehurst.wiremock.testsupport.CompositeNotifier;
4243
import com.github.tomakehurst.wiremock.testsupport.TestNotifier;
4344
import com.github.tomakehurst.wiremock.testsupport.WireMockTestClient;
@@ -374,17 +375,18 @@ public void addsRandomDelayViaJSON() throws Exception {
374375

375376
@Test
376377
public void doesNotFireAWebhookWhenRequestedForDeniedTarget() throws Exception {
377-
rule.stubFor(
378-
post(urlPathEqualTo("/webhook"))
379-
.willReturn(aResponse().withStatus(200))
380-
.withServeEventListener(
381-
"webhook",
382-
webhook()
383-
.withMethod(POST)
384-
.withUrl("http://169.254.2.34/foo")
385-
.withHeader("Content-Type", "application/json")
386-
.withHeader("X-Multi", "one", "two")
387-
.withBody("{ \"result\": \"SUCCESS\" }")));
378+
StubMapping stub =
379+
rule.stubFor(
380+
post(urlPathEqualTo("/webhook"))
381+
.willReturn(aResponse().withStatus(200))
382+
.withServeEventListener(
383+
"webhook",
384+
webhook()
385+
.withMethod(POST)
386+
.withUrl("http://169.254.2.34/foo")
387+
.withHeader("Content-Type", "application/json")
388+
.withHeader("X-Multi", "one", "two")
389+
.withBody("{ \"result\": \"SUCCESS\" }")));
388390

389391
client.post("/webhook", new StringEntity("", TEXT_PLAIN));
390392

@@ -398,7 +400,10 @@ public void doesNotFireAWebhookWhenRequestedForDeniedTarget() throws Exception {
398400
await().until(() -> testNotifier.getErrorMessages(), hasSize(greaterThanOrEqualTo(1)));
399401
assertThat(
400402
errorMessages.get(0),
401-
is("The target webhook address is denied in WireMock's configuration."));
403+
is(
404+
"The target webhook address http://169.254.2.34/foo specified by stub "
405+
+ stub.getId()
406+
+ " is denied in WireMock's configuration."));
402407
}
403408

404409
private void waitForRequestToTargetServer() throws Exception {

0 commit comments

Comments
 (0)