Skip to content

Commit eaf8ea4

Browse files
committed
Use generated protocol headers in Java tests
1 parent 0cf5e18 commit eaf8ea4

3 files changed

Lines changed: 99 additions & 121 deletions

File tree

‎src/test/java/io/tus/java/client/MockServerProvider.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import org.junit.After;
44
import org.junit.Before;
55
import org.mockserver.client.MockServerClient;
6+
import org.mockserver.model.HttpRequest;
7+
import org.mockserver.model.HttpResponse;
68
import org.mockserver.socket.PortFactory;
79

810
import java.net.URL;
11+
import java.util.Map;
912

1013
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
1114

@@ -36,4 +39,18 @@ public void setUp() throws Exception {
3639
public void tearDown() {
3740
mockServer.stop();
3841
}
42+
43+
protected HttpRequest withDefaultProtocolRequestHeaders(HttpRequest request) {
44+
for (Map.Entry<String, String> entry : TusProtocol.DEFAULT_REQUEST_HEADERS.entrySet()) {
45+
request.withHeader(entry.getKey(), entry.getValue());
46+
}
47+
return request;
48+
}
49+
50+
protected HttpResponse withDefaultProtocolResponseHeaders(HttpResponse response) {
51+
for (Map.Entry<String, String> entry : TusProtocol.DEFAULT_RESPONSE_HEADERS.entrySet()) {
52+
response.withHeader(entry.getKey(), entry.getValue());
53+
}
54+
return response;
55+
}
3956
}

‎src/test/java/io/tus/java/client/TestTusClient.java‎

Lines changed: 50 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,15 @@ public void testEnableResuming() {
8181
*/
8282
@Test
8383
public void testCreateUpload() throws IOException, ProtocolException {
84-
mockServer.when(new HttpRequest()
84+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
8585
.withMethod("POST")
8686
.withPath("/files")
8787
.withHeader("Connection", "keep-alive")
88-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
8988
.withHeader("Upload-Metadata", "foo aGVsbG8=,bar d29ybGQ=")
90-
.withHeader("Upload-Length", "10"))
91-
.respond(new HttpResponse()
89+
.withHeader("Upload-Length", "10")))
90+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
9291
.withStatusCode(201)
93-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
94-
.withHeader("Location", mockServerURL + "/foo"));
92+
.withHeader("Location", mockServerURL + "/foo")));
9593

9694
Map<String, String> metadata = new LinkedHashMap<String, String>();
9795
metadata.put("foo", "hello");
@@ -115,15 +113,13 @@ public void testCreateUpload() throws IOException, ProtocolException {
115113
*/
116114
@Test
117115
public void testCreateUploadWithDeferredLength() throws IOException, ProtocolException {
118-
mockServer.when(new HttpRequest()
116+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
119117
.withMethod("POST")
120118
.withPath("/files")
121-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
122-
.withHeader("Upload-Defer-Length", "1"))
123-
.respond(new HttpResponse()
119+
.withHeader("Upload-Defer-Length", "1")))
120+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
124121
.withStatusCode(201)
125-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
126-
.withHeader("Location", mockServerURL + "/foo"));
122+
.withHeader("Location", mockServerURL + "/foo")));
127123

128124
TusClient client = new TusClient();
129125
client.setUploadCreationURL(mockServerURL);
@@ -148,17 +144,15 @@ public void testCreateUploadWithDeferredLength() throws IOException, ProtocolExc
148144
*/
149145
@Test
150146
public void testCreateUploadWithProxy() throws IOException, ProtocolException {
151-
mockServer.when(new HttpRequest()
147+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
152148
.withMethod("POST")
153149
.withPath("/files")
154150
.withHeader("Proxy-Connection", "keep-alive")
155-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
156151
.withHeader("Upload-Metadata", "foo aGVsbG8=,bar d29ybGQ=")
157-
.withHeader("Upload-Length", "11"))
158-
.respond(new HttpResponse()
152+
.withHeader("Upload-Length", "11")))
153+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
159154
.withStatusCode(201)
160-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
161-
.withHeader("Location", mockServerURL + "/foo"));
155+
.withHeader("Location", mockServerURL + "/foo")));
162156

163157
Map<String, String> metadata = new LinkedHashMap<String, String>();
164158
metadata.put("foo", "hello");
@@ -182,14 +176,12 @@ public void testCreateUploadWithProxy() throws IOException, ProtocolException {
182176
*/
183177
@Test
184178
public void testCreateUploadWithMissingLocationHeader() throws Exception {
185-
mockServer.when(new HttpRequest()
179+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
186180
.withMethod("POST")
187181
.withPath("/files")
188-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
189-
.withHeader("Upload-Length", "10"))
190-
.respond(new HttpResponse()
191-
.withStatusCode(201)
192-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION));
182+
.withHeader("Upload-Length", "10")))
183+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
184+
.withStatusCode(201)));
193185

194186
TusClient client = new TusClient();
195187
client.setUploadCreationURL(mockServerURL);
@@ -214,24 +206,21 @@ public void testCreateUploadWithRelativeLocation() throws Exception {
214206
System.setProperty("http.strictPostRedirect", "true");
215207

216208
// Attempt a real redirect
217-
mockServer.when(new HttpRequest()
209+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
218210
.withMethod("POST")
219211
.withPath("/filesRedirect")
220-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
221-
.withHeader("Upload-Length", "10"))
212+
.withHeader("Upload-Length", "10")))
222213
.respond(new HttpResponse()
223214
.withStatusCode(301)
224215
.withHeader("Location", mockServerURL + "Redirected/"));
225216

226-
mockServer.when(new HttpRequest()
217+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
227218
.withMethod("POST")
228219
.withPath("/filesRedirected/")
229-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
230-
.withHeader("Upload-Length", "10"))
231-
.respond(new HttpResponse()
220+
.withHeader("Upload-Length", "10")))
221+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
232222
.withStatusCode(201)
233-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
234-
.withHeader("Location", "foo"));
223+
.withHeader("Location", "foo")));
235224

236225
TusClient client = new TusClient();
237226
client.setUploadCreationURL(new URL(mockServerURL + "Redirect"));
@@ -256,14 +245,12 @@ public void testCreateUploadWithRelativeLocation() throws Exception {
256245
@Test
257246
public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException,
258247
ProtocolException {
259-
mockServer.when(new HttpRequest()
248+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
260249
.withMethod("HEAD")
261-
.withPath("/files/foo")
262-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION))
263-
.respond(new HttpResponse()
250+
.withPath("/files/foo")))
251+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
264252
.withStatusCode(204)
265-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
266-
.withHeader("Upload-Offset", "3"));
253+
.withHeader("Upload-Offset", "3")));
267254

268255
TusClient client = new TusClient();
269256
client.setUploadCreationURL(mockServerURL);
@@ -309,16 +296,14 @@ public void remove(String fingerprint) {
309296
*/
310297
@Test
311298
public void testResumeOrCreateUpload() throws IOException, ProtocolException {
312-
mockServer.when(new HttpRequest()
299+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
313300
.withMethod("POST")
314301
.withPath("/files")
315302
.withHeader("Connection", "keep-alive")
316-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
317-
.withHeader("Upload-Length", "10"))
318-
.respond(new HttpResponse()
303+
.withHeader("Upload-Length", "10")))
304+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
319305
.withStatusCode(201)
320-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
321-
.withHeader("Location", mockServerURL + "/foo"));
306+
.withHeader("Location", mockServerURL + "/foo")));
322307

323308
TusClient client = new TusClient();
324309
client.setUploadCreationURL(mockServerURL);
@@ -338,16 +323,14 @@ public void testResumeOrCreateUpload() throws IOException, ProtocolException {
338323
*/
339324
@Test
340325
public void testResumeOrCreateUploadWithProxy() throws IOException, ProtocolException {
341-
mockServer.when(new HttpRequest()
326+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
342327
.withMethod("POST")
343328
.withPath("/files")
344329
.withHeader("Proxy-Connection", "keep-alive")
345-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
346-
.withHeader("Upload-Length", "11"))
347-
.respond(new HttpResponse()
330+
.withHeader("Upload-Length", "11")))
331+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
348332
.withStatusCode(201)
349-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
350-
.withHeader("Location", mockServerURL + "/foo"));
333+
.withHeader("Location", mockServerURL + "/foo")));
351334

352335
TusClient client = new TusClient();
353336
client.setUploadCreationURL(mockServerURL);
@@ -369,22 +352,19 @@ public void testResumeOrCreateUploadWithProxy() throws IOException, ProtocolExce
369352
*/
370353
@Test
371354
public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolException {
372-
mockServer.when(new HttpRequest()
355+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
373356
.withMethod("HEAD")
374-
.withPath("/files/not_found")
375-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION))
357+
.withPath("/files/not_found")))
376358
.respond(new HttpResponse()
377359
.withStatusCode(404));
378360

379-
mockServer.when(new HttpRequest()
361+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
380362
.withMethod("POST")
381363
.withPath("/files")
382-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
383-
.withHeader("Upload-Length", "10"))
384-
.respond(new HttpResponse()
364+
.withHeader("Upload-Length", "10")))
365+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
385366
.withStatusCode(201)
386-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
387-
.withHeader("Location", mockServerURL + "/foo"));
367+
.withHeader("Location", mockServerURL + "/foo")));
388368

389369
TusClient client = new TusClient();
390370
client.setUploadCreationURL(mockServerURL);
@@ -409,14 +389,12 @@ public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolExcep
409389
*/
410390
@Test
411391
public void testBeginOrResumeUploadFromURL() throws IOException, ProtocolException {
412-
mockServer.when(new HttpRequest()
392+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
413393
.withMethod("HEAD")
414-
.withPath("/files/fooFromURL")
415-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION))
416-
.respond(new HttpResponse()
394+
.withPath("/files/fooFromURL")))
395+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
417396
.withStatusCode(204)
418-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
419-
.withHeader("Upload-Offset", "3"));
397+
.withHeader("Upload-Offset", "3")));
420398

421399
TusClient client = new TusClient();
422400
URL uploadURL = new URL(mockServerURL.toString() + "/fooFromURL");
@@ -507,24 +485,21 @@ public void testFollowRedirects() throws Exception {
507485
assertTrue(connection.getInstanceFollowRedirects());
508486

509487
// Attempt a real redirect
510-
mockServer.when(new HttpRequest()
488+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
511489
.withMethod("POST")
512490
.withPath("/filesRedirect")
513-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
514-
.withHeader("Upload-Length", "10"))
491+
.withHeader("Upload-Length", "10")))
515492
.respond(new HttpResponse()
516493
.withStatusCode(301)
517494
.withHeader("Location", mockServerURL + "Redirected"));
518495

519-
mockServer.when(new HttpRequest()
496+
mockServer.when(withDefaultProtocolRequestHeaders(new HttpRequest()
520497
.withMethod("POST")
521498
.withPath("/filesRedirected")
522-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
523-
.withHeader("Upload-Length", "10"))
524-
.respond(new HttpResponse()
499+
.withHeader("Upload-Length", "10")))
500+
.respond(withDefaultProtocolResponseHeaders(new HttpResponse()
525501
.withStatusCode(201)
526-
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
527-
.withHeader("Location", mockServerURL + "/foo"));
502+
.withHeader("Location", mockServerURL + "/foo")));
528503

529504
client.setUploadCreationURL(new URL(mockServerURL + "Redirect"));
530505
TusUpload upload = new TusUpload();

0 commit comments

Comments
 (0)