View Javadoc
1   /*
2    * This file is part of dependency-check-core.
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   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
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   * @author Jeremy Long
36   */
37  public class DownloaderIT extends BaseTest {
38  
39      /**
40       * Initialize the {@link Settings}.
41       */
42      @Before
43      @Override
44      public void setUp() {
45          super.setUp();
46      }
47  
48      /**
49       * Test of fetchFile method, of class Downloader.
50       *
51       * @throws Exception thrown when an exception occurs.
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       * Test of fetchAndHandleContent method.
65       *
66       * @throws Exception thrown when an exception occurs.
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  }