Skip to content

Commit

Permalink
feat: Update to Wiremock 3.5.2
Browse files Browse the repository at this point in the history
spotless
  • Loading branch information
holomekc committed Apr 1, 2024
1 parent 455744c commit f9f2f9a
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 328 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 Thomas Akehurst
* Copyright (C) 2023-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
252 changes: 132 additions & 120 deletions src/main/java/com/github/tomakehurst/wiremock/core/WireMockApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,51 +104,57 @@ public WireMockApp(Options options, Container container) {
this.settingsStore = stores.getSettingsStore();

extensions =
new Extensions(
options.getDeclaredExtensions(),
this,
options,
stores,
options.filesRoot().child(FILES_ROOT));
new Extensions(
options.getDeclaredExtensions(),
this,
options,
stores,
options.filesRoot().child(FILES_ROOT));
extensions.load();

Map<String, RequestMatcherExtension> customMatchers =
extensions.ofType(RequestMatcherExtension.class);
extensions.ofType(RequestMatcherExtension.class);

requestJournal =
options.requestJournalDisabled()
? new DisabledRequestJournal()
: new StoreBackedRequestJournal(
options.maxRequestJournalEntries().orElse(null),
customMatchers,
stores.getRequestJournalStore());
options.requestJournalDisabled()
? new DisabledRequestJournal()
: new StoreBackedRequestJournal(
options.maxRequestJournalEntries().orElse(null),
customMatchers,
stores.getRequestJournalStore());

scenarios = new InMemoryScenarios(stores.getScenariosStore());
stubMappings =
new StoreBackedStubMappings(
stores.getStubStore(),
scenarios,
customMatchers,
extensions.ofType(ResponseDefinitionTransformer.class),
extensions.ofType(ResponseDefinitionTransformerV2.class),
stores.getFilesBlobStore(),
List.copyOf(extensions.ofType(StubLifecycleListener.class).values()));
new StoreBackedStubMappings(
stores.getStubStore(),
scenarios,
customMatchers,
extensions.ofType(ResponseDefinitionTransformer.class),
extensions.ofType(ResponseDefinitionTransformerV2.class),
stores.getFilesBlobStore(),
List.copyOf(extensions.ofType(StubLifecycleListener.class).values()));
nearMissCalculator = new NearMissCalculator(stubMappings, requestJournal, scenarios);
recorder =
new Recorder(this, extensions, stores.getFilesBlobStore(), stores.getRecorderStateStore());
new Recorder(this, extensions, stores.getFilesBlobStore(), stores.getRecorderStateStore());
globalSettingsListeners = List.copyOf(extensions.ofType(GlobalSettingsListener.class).values());
this.mappingsLoaderExtensions = extensions.ofType(MappingsLoaderExtension.class);

this.container = container;
loadDefaultMappings();
}

public WireMockApp(boolean browserProxyingEnabled, MappingsLoader defaultMappingsLoader,
Map<String, MappingsLoaderExtension> mappingsLoaderExtensions, MappingsSaver mappingsSaver,
boolean requestJournalDisabled, Integer maxRequestJournalEntries,
Map<String, ResponseDefinitionTransformer> transformers,
Map<String, ResponseDefinitionTransformerV2> v2transformers, Map<String, RequestMatcherExtension> requestMatchers,
FileSource rootFileSource, Container container) {
public WireMockApp(
boolean browserProxyingEnabled,
MappingsLoader defaultMappingsLoader,
Map<String, MappingsLoaderExtension> mappingsLoaderExtensions,
MappingsSaver mappingsSaver,
boolean requestJournalDisabled,
Integer maxRequestJournalEntries,
Map<String, ResponseDefinitionTransformer> transformers,
Map<String, ResponseDefinitionTransformerV2> v2transformers,
Map<String, RequestMatcherExtension> requestMatchers,
FileSource rootFileSource,
Container container) {

this.proxyHandler = new ProxyHandler(this);

Expand All @@ -160,119 +166,121 @@ public WireMockApp(boolean browserProxyingEnabled, MappingsLoader defaultMapping
this.mappingsSaver = mappingsSaver;
this.settingsStore = stores.getSettingsStore();
requestJournal =
requestJournalDisabled
? new DisabledRequestJournal()
: new StoreBackedRequestJournal(
maxRequestJournalEntries, requestMatchers, stores.getRequestJournalStore());
requestJournalDisabled
? new DisabledRequestJournal()
: new StoreBackedRequestJournal(
maxRequestJournalEntries, requestMatchers, stores.getRequestJournalStore());
scenarios = new InMemoryScenarios(stores.getScenariosStore());
stubMappings =
new StoreBackedStubMappings(
stores.getStubStore(),
scenarios,
requestMatchers,
transformers,
v2transformers,
stores.getFilesBlobStore(),
Collections.emptyList());
new StoreBackedStubMappings(
stores.getStubStore(),
scenarios,
requestMatchers,
transformers,
v2transformers,
stores.getFilesBlobStore(),
Collections.emptyList());
this.container = container;
nearMissCalculator = new NearMissCalculator(stubMappings, requestJournal, scenarios);
recorder =
new Recorder(this, extensions, stores.getFilesBlobStore(), stores.getRecorderStateStore());
new Recorder(this, extensions, stores.getFilesBlobStore(), stores.getRecorderStateStore());
globalSettingsListeners = Collections.emptyList();
loadDefaultMappings();
}

public AdminRequestHandler buildAdminRequestHandler() {
AdminRoutes adminRoutes =
AdminRoutes.forServer(extensions.ofType(AdminApiExtension.class).values(), stores);
AdminRoutes.forServer(extensions.ofType(AdminApiExtension.class).values(), stores);
return new AdminRequestHandler(
adminRoutes,
this,
new BasicResponseRenderer(),
options.getAdminAuthenticator(),
options.getHttpsRequiredForAdminApi(),
getAdminRequestFilters(),
getV2AdminRequestFilters(),
options.getDataTruncationSettings());
adminRoutes,
this,
new BasicResponseRenderer(),
options.getAdminAuthenticator(),
options.getHttpsRequiredForAdminApi(),
getAdminRequestFilters(),
getV2AdminRequestFilters(),
options.getDataTruncationSettings());
}

public StubRequestHandler buildStubRequestHandler() {
Map<String, PostServeAction> postServeActions = extensions.ofType(PostServeAction.class);

Map<String, ServeEventListener> concatenatedMap = new HashMap<>(extensions.ofType(ServeEventListener.class));
Map<String, ServeEventListener> concatenatedMap =
new HashMap<>(extensions.ofType(ServeEventListener.class));
concatenatedMap.put("wiremock-gui", new GuiServeEventListener());

Map<String, ServeEventListener> serveEventListeners = Collections.unmodifiableMap(concatenatedMap);
Map<String, ServeEventListener> serveEventListeners =
Collections.unmodifiableMap(concatenatedMap);

BrowserProxySettings browserProxySettings = options.browserProxySettings();

final com.github.tomakehurst.wiremock.http.client.HttpClientFactory httpClientFactory =
extensions
.ofType(com.github.tomakehurst.wiremock.http.client.HttpClientFactory.class)
.values()
.stream()
.findFirst()
.orElse(options.httpClientFactory());
extensions
.ofType(com.github.tomakehurst.wiremock.http.client.HttpClientFactory.class)
.values()
.stream()
.findFirst()
.orElse(options.httpClientFactory());

final HttpClient reverseProxyClient =
httpClientFactory.buildHttpClient(options, true, Collections.emptyList(), true);
httpClientFactory.buildHttpClient(options, true, Collections.emptyList(), true);
final HttpClient forwardProxyClient =
httpClientFactory.buildHttpClient(
options,
browserProxySettings.trustAllProxyTargets(),
browserProxySettings.trustAllProxyTargets()
? Collections.emptyList()
: browserProxySettings.trustedProxyTargets(),
false);
httpClientFactory.buildHttpClient(
options,
browserProxySettings.trustAllProxyTargets(),
browserProxySettings.trustAllProxyTargets()
? Collections.emptyList()
: browserProxySettings.trustedProxyTargets(),
false);

return new StubRequestHandler(
this,
new StubResponseRenderer(
options.getStores().getFilesBlobStore(),
settingsStore,
new ProxyResponseRenderer(
options.shouldPreserveHostHeader(),
options.proxyHostHeader(),
settingsStore,
options.getStubCorsEnabled(),
options.getSupportedProxyEncodings(),
reverseProxyClient,
forwardProxyClient),
List.copyOf(extensions.ofType(ResponseTransformer.class).values()),
List.copyOf(extensions.ofType(ResponseTransformerV2.class).values())),
this,
postServeActions,
serveEventListeners,
requestJournal,
getStubRequestFilters(),
getV2StubRequestFilters(),
options.getStubRequestLoggingDisabled(),
options.getDataTruncationSettings(),
options.getNotMatchedRendererFactory().apply(extensions));
this,
new StubResponseRenderer(
options.getStores().getFilesBlobStore(),
settingsStore,
new ProxyResponseRenderer(
options.shouldPreserveHostHeader(),
options.proxyHostHeader(),
settingsStore,
options.getStubCorsEnabled(),
options.getSupportedProxyEncodings(),
reverseProxyClient,
forwardProxyClient),
List.copyOf(extensions.ofType(ResponseTransformer.class).values()),
List.copyOf(extensions.ofType(ResponseTransformerV2.class).values())),
this,
postServeActions,
serveEventListeners,
requestJournal,
getStubRequestFilters(),
getV2StubRequestFilters(),
options.getStubRequestLoggingDisabled(),
options.getDataTruncationSettings(),
options.getNotMatchedRendererFactory().apply(extensions));
}

private List<RequestFilter> getAdminRequestFilters() {
return extensions.ofType(RequestFilter.class).values().stream()
.filter(RequestFilter::applyToAdmin)
.collect(Collectors.toList());
.filter(RequestFilter::applyToAdmin)
.collect(Collectors.toList());
}

private List<RequestFilterV2> getV2AdminRequestFilters() {
return extensions.ofType(RequestFilterV2.class).values().stream()
.filter(RequestFilterV2::applyToAdmin)
.collect(Collectors.toList());
.filter(RequestFilterV2::applyToAdmin)
.collect(Collectors.toList());
}

private List<RequestFilter> getStubRequestFilters() {
return extensions.ofType(RequestFilter.class).values().stream()
.filter(RequestFilter::applyToStubs)
.collect(Collectors.toList());
.filter(RequestFilter::applyToStubs)
.collect(Collectors.toList());
}

private List<RequestFilterV2> getV2StubRequestFilters() {
return extensions.ofType(RequestFilterV2.class).values().stream()
.filter(RequestFilterV2::applyToStubs)
.collect(Collectors.toList());
.filter(RequestFilterV2::applyToStubs)
.collect(Collectors.toList());
}

private void loadDefaultMappings() {
Expand All @@ -291,11 +299,11 @@ public ServeEvent serveStubFor(ServeEvent initialServeEvent) {
ServeEvent serveEvent = stubMappings.serveFor(initialServeEvent);

if (serveEvent.isNoExactMatch()
&& browserProxyingEnabled
&& serveEvent.getRequest().isBrowserProxyRequest()
&& getGlobalSettings().getSettings().getProxyPassThrough()) {
&& browserProxyingEnabled
&& serveEvent.getRequest().isBrowserProxyRequest()
&& getGlobalSettings().getSettings().getProxyPassThrough()) {
return ServeEvent.ofUnmatched(
serveEvent.getRequest(), ResponseDefinition.browserProxy(serveEvent.getRequest()));
serveEvent.getRequest(), ResponseDefinition.browserProxy(serveEvent.getRequest()));
}

return serveEvent;
Expand All @@ -318,13 +326,13 @@ public void addStubMapping(StubMapping stubMapping) {
@Override
public void removeStubMapping(StubMapping stubMapping) {
stubMappings
.get(stubMapping.getId())
.ifPresent(
stubToDelete -> {
if (stubToDelete.shouldBePersisted()) {
mappingsSaver.remove(stubToDelete);
}
});
.get(stubMapping.getId())
.ifPresent(
stubToDelete -> {
if (stubToDelete.shouldBePersisted()) {
mappingsSaver.remove(stubToDelete);
}
});

stubMappings.removeMapping(stubMapping);

Expand Down Expand Up @@ -419,7 +427,7 @@ public GetServeEventsResult getServeEvents(ServeEventQuery query) {
return GetServeEventsResult.requestJournalEnabled(LimitAndOffsetPaginator.none(serveEvents));
} catch (RequestJournalDisabledException e) {
return GetServeEventsResult.requestJournalDisabled(
LimitAndOffsetPaginator.none(requestJournal.getAllServeEvents()));
LimitAndOffsetPaginator.none(requestJournal.getAllServeEvents()));
}
}

Expand Down Expand Up @@ -451,10 +459,10 @@ public FindRequestsResult findRequestsMatching(RequestPattern requestPattern) {
public FindRequestsResult findUnmatchedRequests() {
try {
List<LoggedRequest> requests =
requestJournal.getAllServeEvents().stream()
.filter(ServeEvent::isNoExactMatch)
.map(ServeEvent::getRequest)
.collect(Collectors.toList());
requestJournal.getAllServeEvents().stream()
.filter(ServeEvent::isNoExactMatch)
.map(ServeEvent::getRequest)
.collect(Collectors.toList());
return FindRequestsResult.withRequests(requests);
} catch (RequestJournalDisabledException e) {
return FindRequestsResult.withRequestJournalDisabled();
Expand All @@ -472,17 +480,19 @@ public FindServeEventsResult removeServeEventsMatching(RequestPattern requestPat
}

@Override
public FindServeEventsResult removeServeEventsForStubsMatchingMetadata(StringValuePattern metadataPattern) {
return new FindServeEventsResult(requestJournal.removeServeEventsForStubsMatchingMetadata(metadataPattern));
public FindServeEventsResult removeServeEventsForStubsMatchingMetadata(
StringValuePattern metadataPattern) {
return new FindServeEventsResult(
requestJournal.removeServeEventsForStubsMatchingMetadata(metadataPattern));
}

@Override
public FindNearMissesResult findNearMissesForUnmatchedRequests() {
List<NearMiss> nearMisses = new ArrayList<>();
List<ServeEvent> unmatchedServeEvents =
requestJournal.getAllServeEvents().stream()
.filter(ServeEvent::isNoExactMatch)
.collect(Collectors.toList());
requestJournal.getAllServeEvents().stream()
.filter(ServeEvent::isNoExactMatch)
.collect(Collectors.toList());

for (ServeEvent serveEvent : unmatchedServeEvents) {
nearMisses.addAll(nearMissCalculator.findNearestTo(serveEvent.getRequest()));
Expand Down Expand Up @@ -622,7 +632,8 @@ public RecordingStatusResult getRecordingStatus() {

@Override
public ListStubMappingsResult findAllStubsByMetadata(StringValuePattern pattern) {
return new ListStubMappingsResult(LimitAndOffsetPaginator.none(stubMappings.findByMetadata(pattern)));
return new ListStubMappingsResult(
LimitAndOffsetPaginator.none(stubMappings.findByMetadata(pattern)));
}

@Override
Expand All @@ -636,7 +647,8 @@ public void removeStubsByMetadata(StringValuePattern pattern) {
@Override
public void importStubs(StubImport stubImport) {
List<StubMapping> mappings = stubImport.getMappings();
StubImport.Options importOptions = getFirstNonNull(stubImport.getImportOptions(), StubImport.Options.DEFAULTS);
StubImport.Options importOptions =
getFirstNonNull(stubImport.getImportOptions(), StubImport.Options.DEFAULTS);

for (int i = mappings.size() - 1; i >= 0; i--) {
StubMapping mapping = mappings.get(i);
Expand Down
Loading

0 comments on commit f9f2f9a

Please sign in to comment.