1 package org.apache.archiva.webdav;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import com.gargoylesoftware.htmlunit.WebRequest;
24 import com.gargoylesoftware.htmlunit.WebResponse;
25 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
26 import org.apache.commons.io.FileUtils;
27 import org.junit.Before;
28 import org.junit.Test;
29
30 import java.io.File;
31 import java.nio.charset.Charset;
32 import java.util.ArrayList;
33 import javax.servlet.http.HttpServletResponse;
34 import org.springframework.mock.web.MockHttpServletRequest;
35 import org.springframework.mock.web.MockHttpServletResponse;
36
37
38
39
40 public class RepositoryServletNoProxyTest
41 extends AbstractRepositoryServletTestCase
42 {
43
44 @Before
45 @Override
46 public void setUp()
47 throws Exception
48 {
49 super.setUp();
50 archivaConfiguration.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
51 startRepository();
52 }
53
54 @Test
55 public void testLastModifiedHeaderExists()
56 throws Exception
57 {
58 String commonsLangSha1 = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar.sha1";
59
60 File checksumFile = new File( repoRootInternal, commonsLangSha1 );
61 checksumFile.getParentFile().mkdirs();
62
63 FileUtils.writeStringToFile( checksumFile, "dummy-checksum", Charset.defaultCharset() );
64
65
66 WebResponse response = getWebResponse( "/repository/internal/" + commonsLangSha1 );
67 assertNotNull( response.getResponseHeaderValue( "Last-Modified" ) );
68 }
69
70 @Test
71 public void testGetNoProxyChecksumDefaultLayout()
72 throws Exception
73 {
74 String commonsLangSha1 = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar.sha1";
75
76 File checksumFile = new File( repoRootInternal, commonsLangSha1 );
77 checksumFile.getParentFile().mkdirs();
78
79 FileUtils.writeStringToFile( checksumFile, "dummy-checksum", Charset.defaultCharset() );
80
81
82 WebResponse response = getWebResponse( "/repository/internal/" + commonsLangSha1 );
83 assertResponseOK( response );
84
85 assertEquals( "Expected file contents", "dummy-checksum", response.getContentAsString() );
86 }
87
88 @Test
89 public void testGetNoProxyChecksumLegacyLayout()
90 throws Exception
91 {
92 String commonsLangSha1 = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar.sha1";
93
94 File checksumFile = new File( repoRootInternal, commonsLangSha1 );
95 checksumFile.getParentFile().mkdirs();
96
97 FileUtils.writeStringToFile( checksumFile, "dummy-checksum", Charset.defaultCharset() );
98
99
100
101 WebResponse response =
102 getWebResponse( "/repository/internal/" + "commons-lang/jars/commons-lang-2.1.jar.sha1" );
103 assertResponseNotFound( response );
104 }
105
106 @Test
107 public void testGetNoProxyVersionedMetadataDefaultLayout()
108 throws Exception
109 {
110 String commonsLangMetadata = "commons-lang/commons-lang/2.1/maven-metadata.xml";
111 String expectedMetadataContents = "dummy-versioned-metadata";
112
113 File metadataFile = new File( repoRootInternal, commonsLangMetadata );
114 metadataFile.getParentFile().mkdirs();
115
116 FileUtils.writeStringToFile( metadataFile, expectedMetadataContents, Charset.defaultCharset() );
117
118
119 WebResponse response = getWebResponse( "/repository/internal/" + commonsLangMetadata );
120 assertResponseOK( response );
121
122 assertEquals( "Expected file contents", expectedMetadataContents, response.getContentAsString() );
123 }
124
125 @Test
126 public void testGetNoProxyProjectMetadataDefaultLayout()
127 throws Exception
128 {
129 String commonsLangMetadata = "commons-lang/commons-lang/maven-metadata.xml";
130 String expectedMetadataContents = "dummy-project-metadata";
131
132 File metadataFile = new File( repoRootInternal, commonsLangMetadata );
133 metadataFile.getParentFile().mkdirs();
134
135 FileUtils.writeStringToFile( metadataFile, expectedMetadataContents, Charset.defaultCharset() );
136
137
138 WebResponse response = getWebResponse( "/repository/internal/" + commonsLangMetadata );
139 assertResponseOK( response );
140
141 assertEquals( "Expected file contents", expectedMetadataContents, response.getContentAsString() );
142 }
143
144 @Test
145 public void testGetNoProxyGroupMetadataDefaultLayout()
146 throws Exception
147 {
148 String commonsLangMetadata = "commons-lang/maven-metadata.xml";
149 String expectedMetadataContents = "dummy-group-metadata";
150
151 File metadataFile = new File( repoRootInternal, commonsLangMetadata );
152 metadataFile.getParentFile().mkdirs();
153
154 FileUtils.writeStringToFile( metadataFile, expectedMetadataContents, Charset.defaultCharset() );
155
156
157 WebResponse response = getWebResponse( "/repository/internal/" + commonsLangMetadata );
158 assertResponseOK( response );
159
160 assertEquals( "Expected file contents", expectedMetadataContents, response.getContentAsString() );
161 }
162
163 @Test
164 public void testGetNoProxyArtifactDefaultLayout()
165 throws Exception
166 {
167 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
168 String expectedArtifactContents = "dummy-commons-lang-artifact";
169
170 File artifactFile = new File( repoRootInternal, commonsLangJar );
171 artifactFile.getParentFile().mkdirs();
172
173 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
174
175
176 WebResponse response = getWebResponse( "/repository/internal/" + commonsLangJar );
177 assertResponseOK( response );
178
179 assertEquals( "Expected file contents", expectedArtifactContents, response.getContentAsString() );
180 }
181
182 @Test
183 public void testGetNoProxyArtifactLegacyLayout()
184 throws Exception
185 {
186 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
187 String expectedArtifactContents = "dummy-commons-lang-artifact";
188
189 File artifactFile = new File( repoRootInternal, commonsLangJar );
190 artifactFile.getParentFile().mkdirs();
191
192 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
193
194
195
196 WebResponse response = getWebResponse( "/repository/internal/" + "commons-lang/jars/commons-lang-2.1.jar" );
197 assertResponseNotFound( response );
198
199 }
200
201 @Test
202 public void testGetNoProxySnapshotArtifactDefaultLayout()
203 throws Exception
204 {
205 String commonsLangJar = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-SNAPSHOT.jar";
206 String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
207
208 File artifactFile = new File( repoRootInternal, commonsLangJar );
209 artifactFile.getParentFile().mkdirs();
210
211 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
212
213
214 WebResponse response = getWebResponse( "/repository/internal/" + commonsLangJar );
215 assertResponseOK( response );
216
217 assertEquals( "Expected file contents", expectedArtifactContents, response.getContentAsString() );
218 }
219
220 @Test
221 public void testGetNoProxySnapshotArtifactLegacyLayout()
222 throws Exception
223 {
224 String commonsLangJar = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-SNAPSHOT.jar";
225 String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
226
227 File artifactFile = new File( repoRootInternal, commonsLangJar );
228 artifactFile.getParentFile().mkdirs();
229
230 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
231
232
233
234 WebResponse response = getWebResponse( "/repository/internal/commons-lang/jars/commons-lang-2.1-SNAPSHOT.jar" );
235 assertResponseNotFound( response );
236 }
237
238 @Test
239 public void testGetNoProxyTimestampedSnapshotArtifactDefaultLayout()
240 throws Exception
241 {
242 String commonsLangJar = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-20050821.023400-1.jar";
243 String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
244
245 File artifactFile = new File( repoRootInternal, commonsLangJar );
246 artifactFile.getParentFile().mkdirs();
247
248 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
249
250
251 WebResponse response = getWebResponse( "/repository/internal/" + commonsLangJar );
252 assertResponseOK( response );
253
254 assertEquals( "Expected file contents", expectedArtifactContents, response.getContentAsString() );
255 }
256
257 @Test
258 public void testGetNoProxyTimestampedSnapshotArtifactLegacyLayout()
259 throws Exception
260 {
261 String commonsLangJar = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-20050821.023400-1.jar";
262 String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
263
264 File artifactFile = new File( repoRootInternal, commonsLangJar );
265 artifactFile.getParentFile().mkdirs();
266
267 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
268
269 WebRequest request = new GetMethodWebRequest(
270 "http://machine.com/repository/internal/" + "commons-lang/jars/commons-lang-2.1-20050821.023400-1.jar" );
271 WebResponse response = getServletUnitClient().getResponse( request );
272 assertResponseNotFound( response );
273 }
274
275
276
277
278 @Test
279 public void testGetNoProxyDualExtensionDefaultLayout()
280 throws Exception
281 {
282 String expectedContents = "the-contents-of-the-dual-extension";
283 String dualExtensionPath = "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip";
284
285 File checksumFile = new File( repoRootInternal, dualExtensionPath );
286 checksumFile.getParentFile().mkdirs();
287
288 FileUtils.writeStringToFile( checksumFile, expectedContents, Charset.defaultCharset() );
289
290 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + dualExtensionPath );
291 WebResponse response = getServletUnitClient().getResponse( request );
292 assertResponseOK( response );
293
294 assertEquals( "Expected file contents", expectedContents, response.getContentAsString() );
295 }
296
297 @Test
298 public void testGetNoProxyDistributionLegacyLayout()
299 throws Exception
300 {
301 String expectedContents = "the-contents-of-the-dual-extension";
302 String dualExtensionPath = "org/project/example-presentation/3.2/example-presentation-3.2.zip";
303
304 File checksumFile = new File( repoRootInternal, dualExtensionPath );
305 checksumFile.getParentFile().mkdirs();
306
307 FileUtils.writeStringToFile( checksumFile, expectedContents, Charset.defaultCharset() );
308
309 WebRequest request = new GetMethodWebRequest(
310 "http://machine.com/repository/internal/" + "org.project/distributions/example-presentation-3.2.zip" );
311 WebResponse response = getServletUnitClient().getResponse( request );
312 assertResponseNotFound( response );
313
314 }
315
316 @Test
317 public void testGetNoProxyChecksumDefaultLayoutManagedLegacy()
318 throws Exception
319 {
320 String commonsLangSha1 = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar.sha1";
321
322 File checksumFile = new File( repoRootLegacy, "commons-lang/jars/commons-lang-2.1.jar.sha1" );
323 checksumFile.getParentFile().mkdirs();
324
325 FileUtils.writeStringToFile( checksumFile, "dummy-checksum", Charset.defaultCharset() );
326
327 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangSha1 );
328 WebResponse response = getServletUnitClient().getResponse( request );
329 assertResponseNotFound( response );
330 }
331
332 @Test
333 public void testGetNoProxyChecksumLegacyLayoutManagedLegacy()
334 throws Exception
335 {
336 String commonsLangSha1 = "commons-lang/jars/commons-lang-2.1.jar.sha1";
337 File checksumFile = new File( repoRootLegacy, commonsLangSha1 );
338 checksumFile.getParentFile().mkdirs();
339
340 FileUtils.writeStringToFile( checksumFile, "dummy-checksum", Charset.defaultCharset() );
341
342 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangSha1 );
343 WebResponse response = getServletUnitClient().getResponse( request );
344 assertResponseNotFound( response );
345 }
346
347 @Test
348 public void testGetNoProxyVersionedMetadataDefaultLayoutManagedLegacy()
349 throws Exception
350 {
351 String commonsLangMetadata = "commons-lang/commons-lang/2.1/maven-metadata.xml";
352 String expectedMetadataContents = "dummy-versioned-metadata";
353
354
355 File metadataFile = new File( repoRootLegacy, commonsLangMetadata );
356 metadataFile.getParentFile().mkdirs();
357
358 FileUtils.writeStringToFile( metadataFile, expectedMetadataContents, Charset.defaultCharset() );
359
360 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangMetadata );
361 WebResponse response = getServletUnitClient().getResponse( request );
362 assertResponseNotFound( response );
363 }
364
365 @Test
366 public void testGetNoProxyProjectMetadataDefaultLayoutManagedLegacy()
367 throws Exception
368 {
369
370 String commonsLangMetadata = "commons-lang/commons-lang/maven-metadata.xml";
371 String expectedMetadataContents = "dummy-project-metadata";
372
373 File metadataFile = new File( repoRootLegacy, commonsLangMetadata );
374 metadataFile.getParentFile().mkdirs();
375
376 FileUtils.writeStringToFile( metadataFile, expectedMetadataContents, Charset.defaultCharset() );
377
378 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangMetadata );
379 WebResponse response = getServletUnitClient().getResponse( request );
380 assertResponseNotFound( response );
381 }
382
383 @Test
384 public void testGetNoProxyGroupMetadataDefaultLayoutManagedLegacy()
385 throws Exception
386 {
387 String commonsLangMetadata = "commons-lang/maven-metadata.xml";
388 String expectedMetadataContents = "dummy-group-metadata";
389
390 File metadataFile = new File( repoRootLegacy, commonsLangMetadata );
391 metadataFile.getParentFile().mkdirs();
392
393 FileUtils.writeStringToFile( metadataFile, expectedMetadataContents, Charset.defaultCharset() );
394
395 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangMetadata );
396 WebResponse response = getServletUnitClient().getResponse( request );
397 assertResponseNotFound( response );
398 }
399
400 @Test
401 public void testGetNoProxyArtifactDefaultLayoutManagedLegacy()
402 throws Exception
403 {
404 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
405 String expectedArtifactContents = "dummy-commons-lang-artifact";
406
407 File artifactFile = new File( repoRootLegacy, "commons-lang/jars/commons-lang-2.1.jar" );
408 artifactFile.getParentFile().mkdirs();
409
410 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
411
412 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangJar );
413 WebResponse response = getServletUnitClient().getResponse( request );
414 assertResponseNotFound( response );
415 }
416
417 @Test
418 public void testGetNoProxyArtifactLegacyLayoutManagedLegacy()
419 throws Exception
420 {
421 String commonsLangJar = "commons-lang/jars/commons-lang-2.1.jar";
422 String expectedArtifactContents = "dummy-commons-lang-artifact";
423
424 File artifactFile = new File( repoRootLegacy, commonsLangJar );
425 artifactFile.getParentFile().mkdirs();
426
427 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
428
429 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangJar );
430 WebResponse response = getServletUnitClient().getResponse( request );
431 assertResponseNotFound( response );
432 }
433
434 @Test
435 public void testGetNoProxySnapshotArtifactDefaultLayoutManagedLegacy()
436 throws Exception
437 {
438 String commonsLangJar = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-SNAPSHOT.jar";
439 String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
440
441 File artifactFile = new File( repoRootLegacy, "commons-lang/jars/commons-lang-2.1-SNAPSHOT.jar" );
442 artifactFile.getParentFile().mkdirs();
443
444 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
445
446 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangJar );
447 WebResponse response = getServletUnitClient().getResponse( request );
448 assertResponseNotFound( response );
449 }
450
451 @Test
452 public void testGetNoProxySnapshotArtifactLegacyLayoutManagedLegacy()
453 throws Exception
454 {
455 String commonsLangJar = "commons-lang/jars/commons-lang-2.1-SNAPSHOT.jar";
456 String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
457
458 File artifactFile = new File( repoRootLegacy, commonsLangJar );
459 artifactFile.getParentFile().mkdirs();
460
461 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
462
463 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangJar );
464 WebResponse response = getServletUnitClient().getResponse( request );
465 assertResponseNotFound( response );
466 }
467
468 @Test
469 public void testGetNoProxyTimestampedSnapshotArtifactDefaultLayoutManagedLegacy()
470 throws Exception
471 {
472 String filename = "commons-lang-2.1-20050821.023400-1.jar";
473 String commonsLangJar = "commons-lang/commons-lang/2.1-SNAPSHOT/" + filename;
474 String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
475
476 File artifactFile = new File( repoRootLegacy, "commons-lang/jars/" + filename );
477 artifactFile.getParentFile().mkdirs();
478
479 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
480
481 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangJar );
482 WebResponse response = getServletUnitClient().getResponse( request );
483 assertResponseNotFound( response );
484 }
485
486 @Test
487 public void testGetNoProxyTimestampedSnapshotArtifactLegacyLayoutManagedLegacy()
488 throws Exception
489 {
490 String commonsLangJar = "commons-lang/jars/commons-lang-2.1-20050821.023400-1.jar";
491 String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
492
493 File artifactFile = new File( repoRootLegacy, commonsLangJar );
494 artifactFile.getParentFile().mkdirs();
495
496 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
497
498 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangJar );
499 WebResponse response = getServletUnitClient().getResponse( request );
500 assertResponseNotFound( response );
501 }
502
503
504
505
506 @Test
507 public void testGetNoProxyDualExtensionDefaultLayoutManagedLegacy()
508 throws Exception
509 {
510 String expectedContents = "the-contents-of-the-dual-extension";
511 String dualExtensionPath = "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip";
512
513 File checksumFile = new File( repoRootLegacy, "org.project/distributions/example-presentation-3.2.xml.zip" );
514 checksumFile.getParentFile().mkdirs();
515
516 FileUtils.writeStringToFile( checksumFile, expectedContents, Charset.defaultCharset() );
517
518 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + dualExtensionPath );
519 WebResponse response = getServletUnitClient().getResponse( request );
520 assertResponseNotFound( response );
521 }
522
523 @Test
524 public void testGetNoProxyDistributionLegacyLayoutManagedLegacy()
525 throws Exception
526 {
527 String expectedContents = "the-contents-of-the-dual-extension";
528 String dualExtensionPath = "org.project/distributions/example-presentation-3.2.zip";
529
530 File checksumFile = new File( repoRootLegacy, dualExtensionPath );
531 checksumFile.getParentFile().mkdirs();
532
533 FileUtils.writeStringToFile( checksumFile, expectedContents, Charset.defaultCharset() );
534
535 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + dualExtensionPath );
536 WebResponse response = getServletUnitClient().getResponse( request );
537 assertResponseNotFound( response );
538 }
539
540 @Test
541 public void testGetNoProxySnapshotRedirectToTimestampedSnapshot()
542 throws Exception
543 {
544 String commonsLangQuery = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-SNAPSHOT.jar";
545 String commonsLangMetadata = "commons-lang/commons-lang/2.1-SNAPSHOT/maven-metadata.xml";
546 String commonsLangJar = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-20050821.023400-1.jar";
547 String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
548
549 archivaConfiguration.getConfiguration().getWebapp().getUi().setApplicationUrl("http://localhost");
550
551 File artifactFile = new File( repoRootInternal, commonsLangJar );
552 artifactFile.getParentFile().mkdirs();
553 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
554
555 File metadataFile = new File( repoRootInternal, commonsLangMetadata );
556 metadataFile.getParentFile().mkdirs();
557 FileUtils.writeStringToFile( metadataFile, createVersionMetadata("commons-lang", "commons-lang",
558 "2.1-SNAPSHOT", "20050821.023400", "1", "20050821.023400"));
559
560 WebRequest webRequest = new GetMethodWebRequest(
561 "http://localhost/repository/internal/" + commonsLangQuery );
562
563 MockHttpServletRequest request = new MockHttpServletRequest();
564 request.setRequestURI( webRequest.getUrl().getPath() );
565 request.addHeader( "User-Agent", "Apache Archiva unit test" );
566 request.setMethod( webRequest.getHttpMethod().name() );
567
568 final MockHttpServletResponse response = execute( request );
569
570 assertEquals( HttpServletResponse.SC_MOVED_TEMPORARILY,
571 response.getStatus() );
572
573 assertEquals( "http://localhost/repository/internal/" + commonsLangJar,
574 response.getHeader("Location") );
575 }
576
577 }