Search in sources :

Example 6 with IndyClientHttp

use of org.commonjava.indy.client.core.IndyClientHttp in project indy by Commonjava.

the class StoreOneAndVerifyInHtmlListingTest method storeOneFileAndVerifyItInParentDirectoryListing.

@Test
public void storeOneFileAndVerifyItInParentDirectoryListing() throws Exception {
    final byte[] data = "this is a test".getBytes();
    final ByteArrayInputStream stream = new ByteArrayInputStream(data);
    final String root = "/path/to/";
    final String path = root + "foo.txt";
    client.content().store(hosted, STORE, path, stream);
    final IndyClientHttp http = getHttp();
    final HttpGet request = http.newRawGet(client.content().contentUrl(hosted, STORE, root));
    request.addHeader("Accept", "text/html");
    final CloseableHttpClient hc = http.newClient();
    final CloseableHttpResponse response = hc.execute(request);
    final InputStream listing = response.getEntity().getContent();
    final String html = IOUtils.toString(listing);
    // TODO: Charset!!
    final Document doc = Jsoup.parse(html);
    for (final Element item : doc.select("a.item-link")) {
        final String fname = item.text();
        System.out.printf("Listing contains: '%s'\n", fname);
        final String href = item.attr("href");
        final String expected = client.content().contentUrl(hosted, STORE, root, fname);
        assertThat(fname + " does not have a href", href, notNullValue());
        assertThat(fname + " has incorrect link: '" + href + "' (" + href.getClass().getName() + ")\nshould be: '" + expected + "' (String)", href, equalTo(expected));
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IndyClientHttp(org.commonjava.indy.client.core.IndyClientHttp) HttpGet(org.apache.http.client.methods.HttpGet) Element(org.jsoup.nodes.Element) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 7 with IndyClientHttp

use of org.commonjava.indy.client.core.IndyClientHttp in project indy by Commonjava.

the class GroupHttpHeadersFromSameRepoWhenNotInPathMaskTest method assertGetContentAndLength.

private void assertGetContentAndLength(ArtifactStore store, String path, String content) throws IndyClientException, IOException {
    IndyClientHttp http = httpModule.getHttp();
    try (HttpResources resources = http.getRaw(client.content().contentPath(store.getKey(), path))) {
        HttpResponse response = resources.getResponse();
        assertThat("Request " + store.getKey() + ":" + path + " failed with status: " + response.getStatusLine(), response.getStatusLine().getStatusCode(), equalTo(200));
        Header header = response.getFirstHeader("Content-Length");
        assertThat("Content-Length header missing: " + store.getKey() + ":" + path, header, notNullValue());
        String clStr = header.getValue();
        assertThat(clStr, notNullValue());
        long length = content.getBytes().length;
        Long len = Long.parseLong(clStr);
        assertThat("Error: " + store.getKey() + ":" + path + ": had incorrect retrieved content-length (" + len + "); should have been: " + length, len, equalTo(Long.valueOf(length)));
        assertThat("Unexpected response content: " + store.getKey() + ":" + path, IOUtils.toString(resources.getResponseEntityContent()), equalTo(content));
    }
}
Also used : Header(org.apache.http.Header) IndyClientHttp(org.commonjava.indy.client.core.IndyClientHttp) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) HttpResponse(org.apache.http.HttpResponse)

Example 8 with IndyClientHttp

use of org.commonjava.indy.client.core.IndyClientHttp in project indy by Commonjava.

the class StoreOneAndSourceStoreUrlInHtmlListingTest method storeOneFileAndVerifyItInParentDirectoryListing.

@Test
public void storeOneFileAndVerifyItInParentDirectoryListing() throws Exception {
    final byte[] data = "this is a test".getBytes();
    final ByteArrayInputStream stream = new ByteArrayInputStream(data);
    final String root = "/path/to/";
    final String path = root + "foo.txt";
    final String track = "track";
    content.store(track, hosted, STORE, path, stream);
    final IndyClientHttp http = getHttp();
    final HttpGet request = http.newRawGet(content.contentUrl(track, hosted, STORE, root));
    request.addHeader("Accept", "text/html");
    final CloseableHttpClient hc = http.newClient();
    final CloseableHttpResponse response = hc.execute(request);
    final InputStream listing = response.getEntity().getContent();
    final String html = IOUtils.toString(listing);
    // TODO: Charset!!
    final Document doc = Jsoup.parse(html);
    for (final Element item : doc.select("a.source-link")) {
        final String fname = item.text();
        System.out.printf("Listing contains: '%s'\n", fname);
        final String href = item.attr("href");
        final String expected = client.content().contentUrl(hosted, STORE);
        assertThat(fname + " does not have a href", href, notNullValue());
        assertThat(fname + " has incorrect link: '" + href + "' (" + href.getClass().getName() + ")\nshould be: '" + expected + "' (String)", href, equalTo(expected));
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IndyClientHttp(org.commonjava.indy.client.core.IndyClientHttp) HttpGet(org.apache.http.client.methods.HttpGet) Element(org.jsoup.nodes.Element) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Aggregations

IndyClientHttp (org.commonjava.indy.client.core.IndyClientHttp)8 InputStream (java.io.InputStream)5 HttpGet (org.apache.http.client.methods.HttpGet)5 Test (org.junit.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 Document (org.jsoup.nodes.Document)4 Element (org.jsoup.nodes.Element)4 HttpResources (org.commonjava.indy.client.core.helper.HttpResources)2 Header (org.apache.http.Header)1 HttpResponse (org.apache.http.HttpResponse)1 Mirror (org.apache.maven.settings.Mirror)1 Settings (org.apache.maven.settings.Settings)1 SettingsXpp3Reader (org.apache.maven.settings.io.xpp3.SettingsXpp3Reader)1