Skip to content

Commit aa7ad86

Browse files
committed
refactor: updated to latest tproxy API
1 parent b83d81e commit aa7ad86

8 files changed

Lines changed: 79 additions & 56 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ WebReplayProxy proxy = WebReplayProxy.builder()
357357
proxy.start(8080);
358358

359359
// Execute requests directly through the proxy API
360-
ProxyRequest request = new ProxyRequest(
360+
ProxyRequest request = ProxyRequest.fromBytes(
361361
"GET",
362362
URI.create("http://api.example.com/users"),
363363
Headers.of("Accept", "application/json"),

app.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: java-webreplay
2+
description: A library for recording and replaying web traffic for testing purposes.
3+
documentation: |
4+
# Java Web Replay
5+
6+
A library for recording and replaying web traffic for testing purposes. Built on top of [java-tproxy](https://github.com/codejive/java-tproxy), it provides a simple way to record HTTP/HTTPS traffic and replay it later for deterministic testing.
7+
authors:
8+
- Tako Schotanus (tako@codejive.org)
9+
contributors:
10+
- copilot
11+
links:
12+
homepage: https://github.com/codejive/java-webreplay
13+
repository: https://github.com/codejive/java-webreplay
14+
documentation: https://github.com/codejive/java-webreplay/blob/main/README.md
15+
java: 21
16+
dependencies:
17+
actions:
18+
clean: ./mvnw clean
19+
build: ./mvnw spotless:apply package -DskipTests
20+
test: ./mvnw test
21+
format: ./mvnw spotless:apply

src/main/java/org/codejive/webreplay/RecordingInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private ProxyResponse handleReplayMode(ProxyRequest request, InterceptorChain ch
125125

126126
// No cached response - return 404 in replay mode
127127
logger.debug("REPLAY mode: No cached response, returning 404 for: {}", request.uri());
128-
return new ProxyResponse(
128+
return ProxyResponse.fromBytes(
129129
404, Headers.of("Content-Type", "text/plain"), "Recording not found".getBytes());
130130
}
131131

src/main/java/org/codejive/webreplay/storage/ExchangeJsonSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public ProxyRequest deserialize(
102102
URI uri = URI.create(obj.get("uri").getAsString());
103103
Headers headers = context.deserialize(obj.get("headers"), Headers.class);
104104
byte[] body = Base64.getDecoder().decode(obj.get("body").getAsString());
105-
return new ProxyRequest(method, uri, headers, body);
105+
return ProxyRequest.fromBytes(method, uri, headers, body);
106106
}
107107
}
108108

@@ -127,7 +127,7 @@ public ProxyResponse deserialize(
127127
int statusCode = obj.get("statusCode").getAsInt();
128128
Headers headers = context.deserialize(obj.get("headers"), Headers.class);
129129
byte[] body = Base64.getDecoder().decode(obj.get("body").getAsString());
130-
return new ProxyResponse(statusCode, headers, body);
130+
return ProxyResponse.fromBytes(statusCode, headers, body);
131131
}
132132
}
133133

src/test/java/org/codejive/webreplay/WebReplayProxyIntegrationTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void testRecordMode() throws Exception {
7272

7373
// Execute
7474
ProxyRequest request =
75-
new ProxyRequest(
75+
ProxyRequest.fromBytes(
7676
"GET",
7777
URI.create("http://127.0.0.1:" + BACKEND_PORT + "/api/test"),
7878
Headers.of("User-Agent", "TestClient"),
@@ -104,7 +104,7 @@ void testCacheMode() throws Exception {
104104
.willReturn(aResponse().withStatus(200).withBody("original response")));
105105

106106
ProxyRequest request =
107-
new ProxyRequest(
107+
ProxyRequest.fromBytes(
108108
"GET",
109109
URI.create("http://127.0.0.1:" + BACKEND_PORT + "/api/cached"),
110110
Headers.empty(),
@@ -141,7 +141,7 @@ void testCacheModeMiss() throws Exception {
141141
.willReturn(aResponse().withStatus(200).withBody("new response")));
142142

143143
ProxyRequest request =
144-
new ProxyRequest(
144+
ProxyRequest.fromBytes(
145145
"GET",
146146
URI.create("http://127.0.0.1:" + BACKEND_PORT + "/api/new"),
147147
Headers.empty(),
@@ -168,7 +168,7 @@ void testReplayModeNotFound() throws Exception {
168168
.willReturn(aResponse().withStatus(200).withBody("should not see this")));
169169

170170
ProxyRequest request =
171-
new ProxyRequest(
171+
ProxyRequest.fromBytes(
172172
"GET",
173173
URI.create("http://127.0.0.1:" + BACKEND_PORT + "/api/missing"),
174174
Headers.empty(),
@@ -197,7 +197,7 @@ void testReplayModeWithCache() throws Exception {
197197
.willReturn(aResponse().withStatus(200).withBody("recorded response")));
198198

199199
ProxyRequest request =
200-
new ProxyRequest(
200+
ProxyRequest.fromBytes(
201201
"GET",
202202
URI.create("http://127.0.0.1:" + BACKEND_PORT + "/api/replay"),
203203
Headers.empty(),
@@ -253,7 +253,7 @@ void testClearRecordings() throws Exception {
253253
.willReturn(aResponse().withStatus(200).withBody("test")));
254254

255255
ProxyRequest request =
256-
new ProxyRequest(
256+
ProxyRequest.fromBytes(
257257
"GET",
258258
URI.create("http://127.0.0.1:" + BACKEND_PORT + "/api/clear"),
259259
Headers.empty(),

src/test/java/org/codejive/webreplay/matching/DefaultRequestMatcherTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ void shouldMatchIdenticalRequests() {
1717
RequestMatcher matcher = DefaultRequestMatcher.methodAndUri();
1818

1919
ProxyRequest request1 =
20-
new ProxyRequest(
20+
ProxyRequest.fromBytes(
2121
"GET", URI.create("http://example.com/api"), Headers.empty(), null);
2222
ProxyRequest request2 =
23-
new ProxyRequest(
23+
ProxyRequest.fromBytes(
2424
"GET", URI.create("http://example.com/api"), Headers.empty(), null);
2525

2626
assertThat(matcher.matches(request1, request2)).isTrue();
@@ -32,10 +32,10 @@ void shouldNotMatchDifferentMethods() {
3232
RequestMatcher matcher = DefaultRequestMatcher.methodAndUri();
3333

3434
ProxyRequest request1 =
35-
new ProxyRequest(
35+
ProxyRequest.fromBytes(
3636
"GET", URI.create("http://example.com/api"), Headers.empty(), null);
3737
ProxyRequest request2 =
38-
new ProxyRequest(
38+
ProxyRequest.fromBytes(
3939
"POST", URI.create("http://example.com/api"), Headers.empty(), null);
4040

4141
assertThat(matcher.matches(request1, request2)).isFalse();
@@ -47,10 +47,10 @@ void shouldNotMatchDifferentUris() {
4747
RequestMatcher matcher = DefaultRequestMatcher.methodAndUri();
4848

4949
ProxyRequest request1 =
50-
new ProxyRequest(
50+
ProxyRequest.fromBytes(
5151
"GET", URI.create("http://example.com/api"), Headers.empty(), null);
5252
ProxyRequest request2 =
53-
new ProxyRequest(
53+
ProxyRequest.fromBytes(
5454
"GET", URI.create("http://example.com/other"), Headers.empty(), null);
5555

5656
assertThat(matcher.matches(request1, request2)).isFalse();
@@ -62,10 +62,10 @@ void shouldMatchDifferentQueryOrder() {
6262
RequestMatcher matcher = DefaultRequestMatcher.methodAndUri();
6363

6464
ProxyRequest request1 =
65-
new ProxyRequest(
65+
ProxyRequest.fromBytes(
6666
"GET", URI.create("http://example.com/api?a=1&b=2"), Headers.empty(), null);
6767
ProxyRequest request2 =
68-
new ProxyRequest(
68+
ProxyRequest.fromBytes(
6969
"GET", URI.create("http://example.com/api?b=2&a=1"), Headers.empty(), null);
7070

7171
// Note: Query parameters order matters in URIs, so this will be false
@@ -80,13 +80,13 @@ void shouldMatchHeaders() {
8080
DefaultRequestMatcher.builder().matchHeader("Authorization").build();
8181

8282
ProxyRequest request1 =
83-
new ProxyRequest(
83+
ProxyRequest.fromBytes(
8484
"GET",
8585
URI.create("http://example.com/api"),
8686
Headers.of("Authorization", "Bearer token123"),
8787
null);
8888
ProxyRequest request2 =
89-
new ProxyRequest(
89+
ProxyRequest.fromBytes(
9090
"GET",
9191
URI.create("http://example.com/api"),
9292
Headers.of("Authorization", "Bearer token123"),
@@ -102,13 +102,13 @@ void shouldNotMatchDifferentHeaders() {
102102
DefaultRequestMatcher.builder().matchHeader("Authorization").build();
103103

104104
ProxyRequest request1 =
105-
new ProxyRequest(
105+
ProxyRequest.fromBytes(
106106
"GET",
107107
URI.create("http://example.com/api"),
108108
Headers.of("Authorization", "Bearer token123"),
109109
null);
110110
ProxyRequest request2 =
111-
new ProxyRequest(
111+
ProxyRequest.fromBytes(
112112
"GET",
113113
URI.create("http://example.com/api"),
114114
Headers.of("Authorization", "Bearer token456"),
@@ -123,13 +123,13 @@ void shouldMatchBodies() {
123123
RequestMatcher matcher = DefaultRequestMatcher.builder().matchBody().build();
124124

125125
ProxyRequest request1 =
126-
new ProxyRequest(
126+
ProxyRequest.fromBytes(
127127
"POST",
128128
URI.create("http://example.com/api"),
129129
Headers.empty(),
130130
"request body".getBytes());
131131
ProxyRequest request2 =
132-
new ProxyRequest(
132+
ProxyRequest.fromBytes(
133133
"POST",
134134
URI.create("http://example.com/api"),
135135
Headers.empty(),
@@ -144,13 +144,13 @@ void shouldNotMatchDifferentBodies() {
144144
RequestMatcher matcher = DefaultRequestMatcher.builder().matchBody().build();
145145

146146
ProxyRequest request1 =
147-
new ProxyRequest(
147+
ProxyRequest.fromBytes(
148148
"POST",
149149
URI.create("http://example.com/api"),
150150
Headers.empty(),
151151
"body1".getBytes());
152152
ProxyRequest request2 =
153-
new ProxyRequest(
153+
ProxyRequest.fromBytes(
154154
"POST",
155155
URI.create("http://example.com/api"),
156156
Headers.empty(),
@@ -165,13 +165,13 @@ void shouldIgnoreHeadersByDefault() {
165165
RequestMatcher matcher = DefaultRequestMatcher.methodAndUri();
166166

167167
ProxyRequest request1 =
168-
new ProxyRequest(
168+
ProxyRequest.fromBytes(
169169
"GET",
170170
URI.create("http://example.com/api"),
171171
Headers.of("User-Agent", "Browser1"),
172172
null);
173173
ProxyRequest request2 =
174-
new ProxyRequest(
174+
ProxyRequest.fromBytes(
175175
"GET",
176176
URI.create("http://example.com/api"),
177177
Headers.of("User-Agent", "Browser2"),
@@ -186,13 +186,13 @@ void shouldIgnoreBodyByDefault() {
186186
RequestMatcher matcher = DefaultRequestMatcher.methodAndUri();
187187

188188
ProxyRequest request1 =
189-
new ProxyRequest(
189+
ProxyRequest.fromBytes(
190190
"POST",
191191
URI.create("http://example.com/api"),
192192
Headers.empty(),
193193
"body1".getBytes());
194194
ProxyRequest request2 =
195-
new ProxyRequest(
195+
ProxyRequest.fromBytes(
196196
"POST",
197197
URI.create("http://example.com/api"),
198198
Headers.empty(),

src/test/java/org/codejive/webreplay/storage/FileSystemStoreTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ void shouldCreateDirectory() {
5252
@DisplayName("Should save and retrieve exchange")
5353
void shouldSaveAndRetrieve() throws Exception {
5454
ProxyRequest request =
55-
new ProxyRequest(
55+
ProxyRequest.fromBytes(
5656
"GET", URI.create("http://example.com/api"), Headers.empty(), null);
5757
ProxyResponse response =
58-
new ProxyResponse(
58+
ProxyResponse.fromBytes(
5959
200, Headers.of("Content-Type", "application/json"), "{}".getBytes());
6060
RecordedExchange exchange = new RecordedExchange(request, response);
6161

@@ -76,9 +76,9 @@ void shouldSaveAndRetrieve() throws Exception {
7676
@DisplayName("Should persist across store instances")
7777
void shouldPersist() throws Exception {
7878
ProxyRequest request =
79-
new ProxyRequest(
79+
ProxyRequest.fromBytes(
8080
"GET", URI.create("http://example.com/api"), Headers.empty(), null);
81-
ProxyResponse response = new ProxyResponse(200, Headers.empty(), "test".getBytes());
81+
ProxyResponse response = ProxyResponse.fromBytes(200, Headers.empty(), "test".getBytes());
8282

8383
store.save(new RecordedExchange(request, response));
8484

@@ -97,9 +97,9 @@ void shouldUseCustomNaming() throws Exception {
9797
FileSystemStore customStore = new FileSystemStore(tempDir, customStrategy);
9898

9999
ProxyRequest request =
100-
new ProxyRequest(
100+
ProxyRequest.fromBytes(
101101
"GET", URI.create("http://example.com/api"), Headers.empty(), null);
102-
ProxyResponse response = new ProxyResponse(200, Headers.empty(), new byte[0]);
102+
ProxyResponse response = ProxyResponse.fromBytes(200, Headers.empty(), new byte[0]);
103103

104104
customStore.save(new RecordedExchange(request, response));
105105

@@ -112,12 +112,12 @@ void shouldUseCustomNaming() throws Exception {
112112
@DisplayName("Should list all exchanges")
113113
void shouldListAll() throws Exception {
114114
ProxyRequest request1 =
115-
new ProxyRequest(
115+
ProxyRequest.fromBytes(
116116
"GET", URI.create("http://example.com/api1"), Headers.empty(), null);
117117
ProxyRequest request2 =
118-
new ProxyRequest(
118+
ProxyRequest.fromBytes(
119119
"GET", URI.create("http://example.com/api2"), Headers.empty(), null);
120-
ProxyResponse response = new ProxyResponse(200, Headers.empty(), new byte[0]);
120+
ProxyResponse response = ProxyResponse.fromBytes(200, Headers.empty(), new byte[0]);
121121

122122
store.save(new RecordedExchange(request1, response));
123123
store.save(new RecordedExchange(request2, response));
@@ -130,9 +130,9 @@ void shouldListAll() throws Exception {
130130
@DisplayName("Should clear all files")
131131
void shouldClear() throws Exception {
132132
ProxyRequest request =
133-
new ProxyRequest(
133+
ProxyRequest.fromBytes(
134134
"GET", URI.create("http://example.com/api"), Headers.empty(), null);
135-
ProxyResponse response = new ProxyResponse(200, Headers.empty(), new byte[0]);
135+
ProxyResponse response = ProxyResponse.fromBytes(200, Headers.empty(), new byte[0]);
136136

137137
store.save(new RecordedExchange(request, response));
138138
assertThat(store.count()).isEqualTo(1);
@@ -147,7 +147,7 @@ void shouldClear() throws Exception {
147147
@DisplayName("Should preserve headers and body")
148148
void shouldPreserveData() throws Exception {
149149
ProxyRequest request =
150-
new ProxyRequest(
150+
ProxyRequest.fromBytes(
151151
"POST",
152152
URI.create("http://example.com/api"),
153153
Headers.of(
@@ -157,7 +157,7 @@ void shouldPreserveData() throws Exception {
157157
"Bearer token"),
158158
"{\"key\":\"value\"}".getBytes());
159159
ProxyResponse response =
160-
new ProxyResponse(
160+
ProxyResponse.fromBytes(
161161
201, Headers.of("Location", "/resource/123"), "{\"id\":123}".getBytes());
162162

163163
store.save(new RecordedExchange(request, response));

0 commit comments

Comments
 (0)