Skip to content

Commit

Permalink
Added ability to remove serve events (logged requests) via a request …
Browse files Browse the repository at this point in the history
…pattern
  • Loading branch information
tomakehurst committed Sep 6, 2019
1 parent 25b057a commit 5b6a366
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 36 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ allprojects {
testCompile 'org.littleshoot:littleproxy:1.1.2:littleproxy-shade'
testCompile 'org.apache.httpcomponents:httpmime:4.5'

testRuntime 'org.slf4j:slf4j-log4j12:1.7.12'
// This is coming in via the shaded littleproxy already. Adding it a 2nd time breaks it.
// testRuntime 'org.slf4j:slf4j-log4j12:1.7.12'
testRuntime files('src/test/resources/classpath file source/classpathfiles.zip', 'src/test/resources/classpath-filesource.jar')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ public void removeServeEvent(UUID eventId) {
wireMockApp.removeServeEvent(eventId);
}

@Override
public FindServeEventsResult removeServeEventsMatching(RequestPattern requestPattern) {
return wireMockApp.removeServeEventsMatching(requestPattern);
}

@Override
public void updateGlobalSettings(GlobalSettings newSettings) {
wireMockApp.updateGlobalSettings(newSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ private void initDefaultRoutes(Router router) {
router.add(GET, "/requests/unmatched/near-misses", FindNearMissesForUnmatchedTask.class);
router.add(GET, "/requests/{id}", GetServedStubTask.class);
router.add(DELETE, "/requests/{id}", RemoveServeEventTask.class);
router.add(POST, "/requests/remove", RemoveServeEventsByRequestPatternTask.class);

router.add(POST, "/recordings/snapshot", SnapshotTask.class);
router.add(POST, "/recordings/start", StartRecordingTask.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.tomakehurst.wiremock.admin;

import com.github.tomakehurst.wiremock.admin.model.PathParams;
import com.github.tomakehurst.wiremock.common.Json;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.verification.FindServeEventsResult;

public class RemoveServeEventsByRequestPatternTask implements AdminTask {

@Override
public ResponseDefinition execute(Admin admin, Request request, PathParams pathParams) {
RequestPattern requestPattern = Json.read(request.getBodyAsString(), RequestPattern.class);
FindServeEventsResult findServeEventsResult = admin.removeServeEventsMatching(requestPattern);
return ResponseDefinition.okForJson(findServeEventsResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
import com.github.tomakehurst.wiremock.security.NotAuthorisedException;
import com.github.tomakehurst.wiremock.stubbing.StubImport;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.github.tomakehurst.wiremock.verification.FindNearMissesResult;
import com.github.tomakehurst.wiremock.verification.FindRequestsResult;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import com.github.tomakehurst.wiremock.verification.VerificationResult;
import com.github.tomakehurst.wiremock.verification.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
Expand Down Expand Up @@ -250,6 +247,14 @@ public void removeServeEvent(UUID eventId) {
);
}

@Override
public FindServeEventsResult removeServeEventsMatching(RequestPattern requestPattern) {
String body = postJsonAssertOkAndReturnBody(
urlFor(RemoveServeEventsByRequestPatternTask.class),
Json.write(requestPattern));
return Json.read(body, FindServeEventsResult.class);
}

@Override
public FindNearMissesResult findNearMissesForUnmatchedRequests() {
String body = getJsonAssertOkAndReturnBody(urlFor(FindNearMissesForUnmatchedTask.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,12 @@ public void removeEvent(UUID eventId) {
admin.removeServeEvent(eventId);
}

public void removeEvents(RequestPatternBuilder requestPatternBuilder) {

public List<ServeEvent> removeEvents(RequestPatternBuilder requestPatternBuilder) {
return admin.removeServeEventsMatching(requestPatternBuilder.build()).getServeEvents();
}

public static void removeServeEvents(RequestPatternBuilder requestPatternBuilder) {
defaultInstance.get().removeEvents(requestPatternBuilder);
public static List<ServeEvent> removeServeEvents(RequestPatternBuilder requestPatternBuilder) {
return defaultInstance.get().removeEvents(requestPatternBuilder);
}

public static RequestPatternBuilder getRequestedFor(UrlPattern urlPattern) {
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/github/tomakehurst/wiremock/core/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import com.github.tomakehurst.wiremock.recording.SnapshotRecordResult;
import com.github.tomakehurst.wiremock.stubbing.StubImport;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.github.tomakehurst.wiremock.verification.FindNearMissesResult;
import com.github.tomakehurst.wiremock.verification.FindRequestsResult;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import com.github.tomakehurst.wiremock.verification.VerificationResult;
import com.github.tomakehurst.wiremock.verification.*;

import java.util.UUID;

Expand All @@ -54,6 +51,7 @@ public interface Admin {
FindRequestsResult findUnmatchedRequests();

void removeServeEvent(UUID eventId);
FindServeEventsResult removeServeEventsMatching(RequestPattern requestPattern);

FindNearMissesResult findTopNearMissesFor(LoggedRequest loggedRequest);
FindNearMissesResult findTopNearMissesFor(RequestPattern requestPattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ public void removeServeEvent(UUID eventId) {
requestJournal.removeEvent(eventId);
}

@Override
public FindServeEventsResult removeServeEventsMatching(RequestPattern requestPattern) {
return new FindServeEventsResult(requestJournal.removeEventsMatching(requestPattern));
}

@Override
public FindNearMissesResult findNearMissesForUnmatchedRequests() {
ImmutableList.Builder<NearMiss> listBuilder = ImmutableList.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public ServeEvent handleRequest(Request request) {
return ServeEvent.forBadRequest(LoggedRequest.createFrom(request), iie.getErrors());
} catch (NotPermittedException npe) {
return ServeEvent.forNotAllowedRequest(LoggedRequest.createFrom(request), npe.getErrors());
} catch (Throwable t) {
notifier().error("Unrecoverable error handling admin request", t);
throw t;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.tomakehurst.wiremock.http.Cookie;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -422,4 +423,13 @@ public boolean apply(Request request) {
}
};
}

public static Predicate<ServeEvent> withRequstMatching(final RequestPattern pattern) {
return new Predicate<ServeEvent>() {
@Override
public boolean apply(ServeEvent serveEvent) {
return pattern.match(serveEvent.getRequest()).isExactMatch();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public void requestReceived(ServeEvent serveEvent) {
@Override
public void removeEvent(UUID eventId) {
}

@Override
public List<ServeEvent> removeEventsMatching(RequestPattern requestPattern) {
throw new RequestJournalDisabledException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,21 @@
*/
package com.github.tomakehurst.wiremock.verification;

import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.matching.RequestMatcherExtension;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;

import static com.github.tomakehurst.wiremock.matching.RequestPattern.thatMatch;
import static com.github.tomakehurst.wiremock.matching.RequestPattern.withRequstMatching;
import static com.google.common.collect.Iterables.*;

public class InMemoryRequestJournal implements RequestJournal {
Expand Down Expand Up @@ -78,6 +75,19 @@ public boolean apply(ServeEvent input) {
}
}

@Override
public List<ServeEvent> removeEventsMatching(RequestPattern requestPattern) {
List<ServeEvent> toDelete = FluentIterable.from(serveEvents)
.filter(withRequstMatching(requestPattern))
.toList();

for (ServeEvent event: toDelete) {
serveEvents.remove(event);
}

return toDelete;
}

@Override
public List<ServeEvent> getAllServeEvents() {
return ImmutableList.copyOf(serveEvents).reverse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ public interface RequestJournal {
void requestReceived(ServeEvent serveEvent);

void removeEvent(UUID eventId);
List<ServeEvent> removeEventsMatching(RequestPattern requestPattern);
}
Original file line number Diff line number Diff line change
Expand Up @@ -686,25 +686,48 @@ public void doesNothingWhenAttemptingToRemoveANonExistentServeEvent() {
assertThat(finalServeEvents.size(), is(3));
}

// @Test
// public void removesEventsPerSuppliedFilter() {
// stubFor(get(anyUrl()).willReturn(ok()));
//
// testClient.get("/one", withHeader("My-Header", "one"));
// testClient.get("/one", withHeader("My-Header", "two"));
// testClient.get("/two");
//
// removeServeEvents(getRequestedFor(urlPathEqualTo("/one")).withHeader("My-Header", equalTo("two")));
//
// List<ServeEvent> serveEvents = getAllServeEvents();
// assertThat(serveEvents.size(), is(2));
//
// ServeEvent event1 = findServeEventWithUrl(serveEvents, "/one");
// assertThat(event1.getRequest().header("My-Header").firstValue(), is("one"));
//
// ServeEvent event2 = findServeEventWithUrl(serveEvents, "/two");
// assertThat(event2, notNullValue());
// }
@Test
public void removesEventsPerSuppliedFilter() {
stubFor(get(anyUrl()).willReturn(ok()));

testClient.get("/one", withHeader("My-Header", "one"));
testClient.get("/one", withHeader("My-Header", "two"));
testClient.get("/two");

List<ServeEvent> removedEvents = removeServeEvents(
getRequestedFor(urlPathEqualTo("/one")).withHeader("My-Header", equalTo("two"))
);

assertThat(removedEvents.size(), is(1));
assertThat(removedEvents.get(0).getRequest().header("My-Header").firstValue(), is("two"));

List<ServeEvent> serveEvents = getAllServeEvents();
assertThat(serveEvents.size(), is(2));

ServeEvent event1 = findServeEventWithUrl(serveEvents, "/one");
assertThat(event1.getRequest().header("My-Header").firstValue(), is("one"));

ServeEvent event2 = findServeEventWithUrl(serveEvents, "/two");
assertThat(event2, notNullValue());
}

@Test
public void returnsEmptyListWhenNoEventsMatchedForRemoval() {
stubFor(get(anyUrl()).willReturn(ok()));

testClient.get("/one", withHeader("My-Header", "one"));
testClient.get("/one", withHeader("My-Header", "two"));
testClient.get("/two");

List<ServeEvent> removedEvents = removeServeEvents(
getRequestedFor(urlPathEqualTo("/one")).withHeader("My-Header", equalTo("wrong"))
);

assertThat(removedEvents.size(), is(0));

List<ServeEvent> serveEvents = getAllServeEvents();
assertThat(serveEvents.size(), is(3));
}

}

Expand Down

0 comments on commit 5b6a366

Please sign in to comment.