URLFileContentProvider implements FileContentProvider { @Override public String fetchContent(String fileURL) throws IOException, DevfileException { URI fileURI; String requestURL; try { fileURI = new URI(fileURL); } catch (URISyntaxException e) { throw new DevfileException(e.getMessage(), e); } if (fileURI.isAbsolute()) { requestURL = fileURL; } else { if (devfileLocation == null) { throw new DevfileException( format( "It is unable to fetch a file %s as relative to devfile, since devfile location" + " is unknown. Try specifying absolute URL.", fileURL)); } requestURL = devfileLocation.resolve(fileURI).toString(); } try { return urlFetcher.fetch(requestURL); } catch (IOException e) { throw new IOException( format( "Failed to fetch a file %s specified in devfile %s from URL %s. Make sure the URL" + " of the devfile points to the raw content of it (e.g. not to the webpage" + " showing it but really just its contents). Additionally, if you're using " + " relative form, make sure the referenced files are actually stored" + " relative to the devfile on the same host," + " or try to specify URL in absolute form. The current attempt to download" + " the file failed with the following error message: %s", fileURL, devfileLocation, requestURL, e.getMessage()), e); } } URLFileContentProvider(@Nullable URI devfileLocation, URLFetcher urlFetcher); @Override String fetchContent(String fileURL);  }
@Test( expectedExceptions = DevfileException.class, expectedExceptionsMessageRegExp = "It is unable to fetch a file /relative/dev.yaml as relative to devfile, since devfile location is unknown. Try specifying absolute URL.") public void shouldThrowExceptionWhenNoDevfileLocationKnownAndURLIsRelative() throws Exception { URLFileContentProvider provider = new URLFileContentProvider(null, urlFetcher); provider.fetchContent("/relative/dev.yaml"); }
@Test public void shouldFetchByAbsoluteURL() throws Exception { String url = "http: URLFileContentProvider provider = new URLFileContentProvider(null, urlFetcher); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); provider.fetchContent(url); verify(urlFetcher).fetch(captor.capture()); assertEquals(captor.getValue(), url); }
@Test public void shouldMergeDevfileLocationAndRelativeURL() throws Exception { String devfileUrl = "http: String relativeUrl = "relative.yaml"; URLFileContentProvider provider = new URLFileContentProvider(new URI(devfileUrl), urlFetcher); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); provider.fetchContent(relativeUrl); verify(urlFetcher).fetch(captor.capture()); assertEquals(captor.getValue(), "http: }