1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.utils;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24
25 import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
26 import org.apache.hc.core5.http.HttpEntity;
27 import org.junit.Test;
28
29 import static java.nio.charset.StandardCharsets.UTF_8;
30 import static org.junit.Assert.assertTrue;
31 import org.junit.Before;
32
33
34
35
36
37 public class DownloaderIT extends BaseTest {
38
39
40
41
42 @Before
43 @Override
44 public void setUp() {
45 super.setUp();
46 }
47
48
49
50
51
52
53 @Test
54 public void testFetchFile() throws Exception {
55 final String str = getSettings().getString(Settings.KEYS.ENGINE_VERSION_CHECK_URL, "https://jeremylong.github.io/DependencyCheck/current.txt");
56 URL url = new URL(str);
57 File outputPath = new File("target/current.txt");
58 Downloader.getInstance().configure(getSettings());
59 Downloader.getInstance().fetchFile(url, outputPath);
60 assertTrue(outputPath.isFile());
61 }
62
63
64
65
66
67
68 @Test
69 public void testfetchAndHandleContent() throws Exception {
70 URL url = new URL(getSettings().getString(Settings.KEYS.ENGINE_VERSION_CHECK_URL));
71 AbstractHttpClientResponseHandler<String> versionHandler = new AbstractHttpClientResponseHandler<String>() {
72 @Override
73 public String handleEntity(HttpEntity entity) throws IOException {
74 try (InputStream in = entity.getContent()) {
75 byte[] read = new byte[90];
76 in.read(read);
77 String text = new String(read, UTF_8);
78 assertTrue(text.matches("^\\d+\\.\\d+\\.\\d+.*"));
79 }
80 return "";
81 }
82 };
83 Downloader.getInstance().fetchAndHandle(url, versionHandler);
84 }
85
86 }