Search in sources :

Example 1 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project camel by apache.

the class LinkedInOAuthRequestFilter method getAccessToken.

private OAuthToken getAccessToken(String refreshToken) throws IOException {
    final String tokenUrl = String.format(ACCESS_TOKEN_URL, refreshToken, oAuthParams.getRedirectUri(), oAuthParams.getClientId(), oAuthParams.getClientSecret());
    final WebRequest webRequest = new WebRequest(new URL(tokenUrl), HttpMethod.POST);
    final WebResponse webResponse = webClient.loadWebResponse(webRequest);
    if (webResponse.getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException(String.format("Error getting access token: [%s: %s]", webResponse.getStatusCode(), webResponse.getStatusMessage()));
    }
    final long currentTime = System.currentTimeMillis();
    final ObjectMapper mapper = new ObjectMapper();
    final Map map = mapper.readValue(webResponse.getContentAsStream(), Map.class);
    final String accessToken = map.get("access_token").toString();
    final Integer expiresIn = Integer.valueOf(map.get("expires_in").toString());
    return new OAuthToken(refreshToken, accessToken, currentTime + TimeUnit.MILLISECONDS.convert(expiresIn, TimeUnit.SECONDS));
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project workflow-cps-plugin by jenkinsci.

the class SnippetizerTester method assertGenerateSnippet.

/**
 * Tests a form submitting part of snippetizer.
 *
 * @param json
 *      The form submission value from the configuration page to be tested.
 * @param responseText
 *      Expected snippet to be generated
 * @param referer
 *      needed because of {@link StaplerReferer}
 */
public void assertGenerateSnippet(@Nonnull String json, @Nonnull String responseText, @CheckForNull String referer) throws Exception {
    JenkinsRule.WebClient wc = r.createWebClient();
    WebRequest wrs = new WebRequest(new URL(r.getURL(), Snippetizer.GENERATE_URL), HttpMethod.POST);
    if (referer != null) {
        wrs.setAdditionalHeader("Referer", referer);
    }
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("json", json));
    // WebClient.addCrumb *replaces* rather than *adds*:
    params.add(new NameValuePair(r.jenkins.getCrumbIssuer().getDescriptor().getCrumbRequestField(), r.jenkins.getCrumbIssuer().getCrumb(null)));
    wrs.setRequestParameters(params);
    WebResponse response = wc.getPage(wrs).getWebResponse();
    assertEquals("text/plain", response.getContentType());
    assertEquals(responseText, response.getContentAsString().trim());
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) ArrayList(java.util.ArrayList) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) URL(java.net.URL)

Example 3 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project archiva by apache.

the class RepositoryServletRepositoryGroupTest method testGetFromLastManagedRepositoryReturnOk.

/*
    * Test Case 3.c
    */
@Test
public void testGetFromLastManagedRepositoryReturnOk() throws Exception {
    String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
    Path dummyReleasesResourceFile = repoRootLast.resolve(resourceName);
    Files.createDirectories(dummyReleasesResourceFile.getParent());
    org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyReleasesResourceFile, Charset.defaultCharset(), "last");
    WebRequest request = new GetMethodWebRequest("http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName);
    WebResponse response = getServletUnitClient().getResponse(request);
    assertResponseOK(response);
    assertThat(response.getContentAsString()).isEqualTo("last");
}
Also used : Path(java.nio.file.Path) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Test(org.junit.Test)

Example 4 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project archiva by apache.

the class RepositoryServletRepositoryGroupTest method testBrowseWithTwoArtifactsWithSameGroupIdInRepos.

// MRM-901
@Test
public void testBrowseWithTwoArtifactsWithSameGroupIdInRepos() throws Exception {
    String resourceName = "dummy/dummy-artifact/1.0/dummy-artifact-1.0.txt";
    Path dummyInternalResourceFile = repoRootFirst.resolve(resourceName);
    Files.createDirectories(dummyInternalResourceFile.getParent());
    org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyInternalResourceFile, Charset.defaultCharset(), "first");
    resourceName = "dummy/dummy-artifact/2.0/dummy-artifact-2.0.txt";
    Path dummyReleasesResourceFile = repoRootLast.resolve(resourceName);
    Files.createDirectories(dummyReleasesResourceFile.getParent());
    org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyReleasesResourceFile, Charset.defaultCharset(), "last");
    WebRequest request = new GetMethodWebRequest("http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/dummy-artifact/");
    WebResponse response = getServletUnitClient().getResource(request);
    assertResponseOK(response);
    assertThat(response.getContentAsString()).contains("Collection").contains("dummy/dummy-artifact").contains("1.0").contains("2.0");
}
Also used : Path(java.nio.file.Path) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Test(org.junit.Test)

Example 5 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project archiva by apache.

the class RepositoryServletTest method testGetRepositoryInvalidPathPassthroughPresent.

@Test
public void testGetRepositoryInvalidPathPassthroughPresent() throws Exception {
    String path = REQUEST_PATH + ".index/filecontent/segments.gen";
    populateRepo(repoRootInternal, ".index/filecontent/segments.gen", "index file");
    WebRequest request = new GetMethodWebRequest(path);
    WebResponse response = getServletUnitClient().getResponse(request);
    assertResponseOK(response);
    assertEquals("index file", response.getContentAsString());
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Test(org.junit.Test)

Aggregations

WebRequest (com.gargoylesoftware.htmlunit.WebRequest)84 Test (org.junit.Test)65 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)49 URL (java.net.URL)31 Path (java.nio.file.Path)31 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)12 TextPage (com.gargoylesoftware.htmlunit.TextPage)12 InputStream (java.io.InputStream)7 MkColMethodWebRequest (org.apache.archiva.webdav.httpunit.MkColMethodWebRequest)7 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)7 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)5 IOException (java.io.IOException)5 FreeStyleProject (hudson.model.FreeStyleProject)4 Page (com.gargoylesoftware.htmlunit.Page)3 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)3 HashMap (java.util.HashMap)3 Issue (org.jvnet.hudson.test.Issue)3 SeleniumOperationException (com.axway.ats.uiengine.exceptions.SeleniumOperationException)2 IncorrectnessListener (com.gargoylesoftware.htmlunit.IncorrectnessListener)2