Skip to content

Commit e6958d8

Browse files
author
Kirill Peshin
committed
Merge branch 'master' into guava3
2 parents 3af3f5e + ad7fd92 commit e6958d8

File tree

14 files changed

+199
-54
lines changed

14 files changed

+199
-54
lines changed

src/main/java/com/github/tomakehurst/wiremock/admin/AdminRoutes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ private void initDefaultRoutes(Router router) {
111111
router.add(GET, "/docs", new GetDocIndexTask());
112112

113113
router.add(GET, "/certs/wiremock-ca.crt", new GetCaCertTask());
114+
115+
router.add(GET, "/health", new HealthCheckTask());
114116
}
115117

116118
protected void initAdditionalRoutes(Router routeBuilder) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2023 Thomas Akehurst
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.tomakehurst.wiremock.admin.model;
17+
18+
import java.lang.management.ManagementFactory;
19+
import java.time.Instant;
20+
21+
public class HealthCheckResult {
22+
private final String status;
23+
private final String message;
24+
private final long uptimeInSeconds;
25+
private final Instant timestamp;
26+
27+
public HealthCheckResult(String status, String message) {
28+
this.status = status;
29+
this.message = message;
30+
this.timestamp = Instant.now();
31+
this.uptimeInSeconds = ManagementFactory.getRuntimeMXBean().getUptime() / 1000;
32+
}
33+
34+
public String getStatus() {
35+
return status;
36+
}
37+
38+
public String getMessage() {
39+
return message;
40+
}
41+
42+
public Instant getTimestamp() {
43+
return timestamp;
44+
}
45+
46+
public long getUptimeInSeconds() {
47+
return uptimeInSeconds;
48+
}
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2023 Thomas Akehurst
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.tomakehurst.wiremock.admin.tasks;
17+
18+
import static com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder.responseDefinition;
19+
import static java.net.HttpURLConnection.HTTP_OK;
20+
21+
import com.github.tomakehurst.wiremock.admin.AdminTask;
22+
import com.github.tomakehurst.wiremock.admin.model.HealthCheckResult;
23+
import com.github.tomakehurst.wiremock.common.Json;
24+
import com.github.tomakehurst.wiremock.common.url.PathParams;
25+
import com.github.tomakehurst.wiremock.core.Admin;
26+
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
27+
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
28+
29+
public class HealthCheckTask implements AdminTask {
30+
@Override
31+
public ResponseDefinition execute(Admin admin, ServeEvent serveEvent, PathParams pathParams) {
32+
33+
return responseDefinition()
34+
.withStatus(HTTP_OK)
35+
.withStatusMessage("Wiremock is ok")
36+
.withBody(
37+
Json.write(
38+
new HealthCheckResult(
39+
HealthCheckStatus.HEALTHY.name().toLowerCase(), "Wiremock is ok")))
40+
.withHeader("Content-Type", "application/json")
41+
.build();
42+
}
43+
44+
protected enum HealthCheckStatus {
45+
HEALTHY,
46+
UNHEALTHY
47+
}
48+
}

src/main/java/com/github/tomakehurst/wiremock/core/Admin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2011-2022 Thomas Akehurst
2+
* Copyright (C) 2011-2023 Thomas Akehurst
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/main/java/com/github/tomakehurst/wiremock/recording/Recorder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.github.tomakehurst.wiremock.store.RecorderStateStore;
2828
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
2929
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
30-
import com.google.common.collect.Lists;
3130
import java.util.List;
3231
import java.util.UUID;
3332
import java.util.function.Predicate;
@@ -109,7 +108,7 @@ private static Predicate<ServeEvent> withId(final UUID id) {
109108
public SnapshotRecordResult takeSnapshot(List<ServeEvent> serveEvents, RecordSpec recordSpec) {
110109
final List<StubMapping> stubMappings =
111110
serveEventsToStubMappings(
112-
Lists.reverse(serveEvents),
111+
serveEvents,
113112
recordSpec.getFilters(),
114113
new SnapshotStubMappingGenerator(
115114
recordSpec.getCaptureHeaders(), recordSpec.getRequestBodyPatternFactory()),

src/main/java/com/github/tomakehurst/wiremock/store/InMemoryRequestJournalStore.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,30 @@
1616
package com.github.tomakehurst.wiremock.store;
1717

1818
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
19-
import java.util.Map;
20-
import java.util.Optional;
21-
import java.util.Queue;
22-
import java.util.UUID;
19+
import java.util.*;
2320
import java.util.concurrent.ConcurrentHashMap;
24-
import java.util.concurrent.ConcurrentLinkedQueue;
21+
import java.util.concurrent.ConcurrentLinkedDeque;
2522
import java.util.stream.Stream;
2623

2724
public class InMemoryRequestJournalStore implements RequestJournalStore {
2825

29-
private final Queue<UUID> queue = new ConcurrentLinkedQueue<>();
26+
private final Deque<UUID> deque = new ConcurrentLinkedDeque<>();
3027
private final Map<UUID, ServeEvent> serveEvents = new ConcurrentHashMap<>();
3128

3229
@Override
3330
public void add(ServeEvent event) {
3431
serveEvents.put(event.getId(), event);
35-
queue.add(event.getId());
32+
deque.addFirst(event.getId());
3633
}
3734

3835
@Override
3936
public Stream<ServeEvent> getAll() {
40-
return queue.stream().map(serveEvents::get);
37+
return deque.stream().map(serveEvents::get);
4138
}
4239

4340
@Override
4441
public void removeLast() {
45-
final UUID id = queue.poll();
42+
final UUID id = deque.pollLast();
4643
if (id != null) {
4744
serveEvents.remove(id);
4845
}
@@ -60,20 +57,20 @@ public Optional<ServeEvent> get(UUID id) {
6057

6158
@Override
6259
public void put(UUID id, ServeEvent event) {
63-
if (queue.contains(id)) {
60+
if (deque.contains(id)) {
6461
serveEvents.put(id, event);
6562
}
6663
}
6764

6865
@Override
6966
public void remove(UUID id) {
70-
queue.stream().filter(eventId -> eventId.equals(id)).forEach(queue::remove);
67+
deque.stream().filter(eventId -> eventId.equals(id)).forEach(deque::remove);
7168
serveEvents.remove(id);
7269
}
7370

7471
@Override
7572
public void clear() {
76-
queue.clear();
73+
deque.clear();
7774
serveEvents.clear();
7875
}
7976
}

src/main/java/com/github/tomakehurst/wiremock/verification/AbstractRequestJournal.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.github.tomakehurst.wiremock.store.RequestJournalStore;
2727
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
2828
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
29-
import com.google.common.collect.ImmutableList;
3029
import java.util.*;
3130
import java.util.function.Predicate;
3231
import java.util.stream.Stream;
@@ -59,7 +58,10 @@ public int countRequestsMatching(RequestPattern requestPattern) {
5958

6059
@Override
6160
public List<LoggedRequest> getRequestsMatching(RequestPattern requestPattern) {
62-
return getRequests().filter(thatMatch(requestPattern, customMatchers)).collect(toList());
61+
List<LoggedRequest> loggedRequests =
62+
getRequests().filter(thatMatch(requestPattern, customMatchers)).collect(toList());
63+
Collections.reverse(loggedRequests);
64+
return loggedRequests;
6365
}
6466

6567
@Override
@@ -101,7 +103,7 @@ private List<ServeEvent> removeServeEvents(Predicate<ServeEvent> predicate) {
101103

102104
@Override
103105
public List<ServeEvent> getAllServeEvents() {
104-
return ImmutableList.copyOf(store.getAll().collect(toList())).reverse();
106+
return store.getAll().collect(toList());
105107
}
106108

107109
@Override

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package com.github.tomakehurst.wiremock;
1717

1818
import static com.github.tomakehurst.wiremock.client.WireMock.*;
19-
import static com.github.tomakehurst.wiremock.client.WireMock.any;
20-
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
2119
import static com.github.tomakehurst.wiremock.common.ContentTypes.CONTENT_ENCODING;
2220
import static com.github.tomakehurst.wiremock.common.ParameterUtils.getLast;
2321
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void proxyServerShutdown() {
7878
+ " \"mappings\": [ \n"
7979
+ " { \n"
8080
+ " \"request\" : { \n"
81-
+ " \"url\" : \"/foo/bar\", \n"
81+
+ " \"url\" : \"/foo/bar/baz\", \n"
8282
+ " \"method\" : \"GET\" \n"
8383
+ " }, \n"
8484
+ " \"response\" : { \n"
@@ -87,13 +87,13 @@ public void proxyServerShutdown() {
8787
+ " }, \n"
8888
+ " { \n"
8989
+ " \"request\" : { \n"
90-
+ " \"url\" : \"/foo/bar/baz\", \n"
90+
+ " \"url\" : \"/foo/bar\", \n"
9191
+ " \"method\" : \"GET\" \n"
9292
+ " }, \n"
9393
+ " \"response\" : { \n"
9494
+ " \"status\" : 200 \n"
9595
+ " } \n"
96-
+ " }, \n"
96+
+ " } \n"
9797
+ " ] \n"
9898
+ "} ";
9999

@@ -128,22 +128,22 @@ public void returnsRequestsWithDefaultOptions() throws Exception {
128128
+ " \"mappings\": [ \n"
129129
+ " { \n"
130130
+ " \"request\" : { \n"
131-
+ " \"url\" : \"/foo/bar\", \n"
131+
+ " \"url\" : \"/foo/bar/baz\", \n"
132132
+ " \"method\" : \"GET\" \n"
133133
+ " }, \n"
134134
+ " \"response\" : { \n"
135135
+ " \"status\" : 200 \n"
136136
+ " } \n"
137-
+ " }, \n"
137+
+ " }, \n"
138138
+ " { \n"
139139
+ " \"request\" : { \n"
140-
+ " \"url\" : \"/foo/bar/baz\", \n"
140+
+ " \"url\" : \"/foo/bar\", \n"
141141
+ " \"method\" : \"GET\" \n"
142142
+ " }, \n"
143143
+ " \"response\" : { \n"
144144
+ " \"status\" : 200 \n"
145145
+ " } \n"
146-
+ " } \n"
146+
+ " } \n"
147147
+ " ] \n"
148148
+ "} ";
149149

@@ -368,7 +368,7 @@ public void returnsStubMappingsWithScenariosForRepeatedRequests() {
368368
+ " \"mappings\": [ \n"
369369
+ " { \n"
370370
+ " \"request\" : { \n"
371-
+ " \"url\" : \"/?transformed=global\", \n"
371+
+ " \"url\" : \"/foo?transformed=global\", \n"
372372
+ " \"method\" : \"GET\" \n"
373373
+ " }, \n"
374374
+ " \"response\" : { \n"
@@ -377,13 +377,13 @@ public void returnsStubMappingsWithScenariosForRepeatedRequests() {
377377
+ " }, \n"
378378
+ " { \n"
379379
+ " \"request\" : { \n"
380-
+ " \"url\" : \"/foo?transformed=global\", \n"
380+
+ " \"url\" : \"/?transformed=global\", \n"
381381
+ " \"method\" : \"GET\" \n"
382382
+ " }, \n"
383383
+ " \"response\" : { \n"
384384
+ " \"status\" : 200 \n"
385385
+ " } \n"
386-
+ " }, \n"
386+
+ " } \n"
387387
+ " ] \n"
388388
+ "} ";
389389

@@ -419,7 +419,7 @@ public void returnsTransformedStubMappingWithGlobalTransformer() {
419419
+ " \"mappings\": [ \n"
420420
+ " { \n"
421421
+ " \"request\" : { \n"
422-
+ " \"url\" : \"/?transformed=nonglobal\", \n"
422+
+ " \"url\" : \"/foo?transformed=nonglobal\", \n"
423423
+ " \"method\" : \"GET\", \n"
424424
+ " \"headers\": { \n"
425425
+ " \"Accept\": { \n"
@@ -430,10 +430,10 @@ public void returnsTransformedStubMappingWithGlobalTransformer() {
430430
+ " \"response\" : { \n"
431431
+ " \"status\" : 200 \n"
432432
+ " } \n"
433-
+ " }, \n"
433+
+ " }, \n"
434434
+ " { \n"
435435
+ " \"request\" : { \n"
436-
+ " \"url\" : \"/foo?transformed=nonglobal\", \n"
436+
+ " \"url\" : \"/?transformed=nonglobal\", \n"
437437
+ " \"method\" : \"GET\", \n"
438438
+ " \"headers\": { \n"
439439
+ " \"Accept\": { \n"
@@ -444,7 +444,7 @@ public void returnsTransformedStubMappingWithGlobalTransformer() {
444444
+ " \"response\" : { \n"
445445
+ " \"status\" : 200 \n"
446446
+ " } \n"
447-
+ " } \n"
447+
+ " } \n"
448448
+ " ] \n"
449449
+ "} ";
450450

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void getsAllServeEventsThatMatchedStubId() {
208208
}
209209

210210
private Matcher<LoggedRequest> withUrl(final String url) {
211-
return new TypeSafeMatcher<LoggedRequest>() {
211+
return new TypeSafeMatcher<>() {
212212
@Override
213213
public boolean matchesSafely(LoggedRequest loggedRequest) {
214214
return loggedRequest.getUrl().equals(url);

0 commit comments

Comments
 (0)