com.jcabi.http.Request Java Examples

The following examples show how to use com.jcabi.http.Request. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InitializrSpringCloudInfoServiceTests.java    From spring-cloud-release-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void getSpringCloudVersionsTest() throws Exception {
	RestTemplate rest = mock(RestTemplate.class);
	Github github = mock(Github.class);
	GithubPomReader githubPomReader = mock(GithubPomReader.class);
	Response response = mock(Response.class);
	Request request = mock(Request.class);
	RequestURI requestURI = mock(RequestURI.class);
	JsonResponse jsonResponse = new JsonResponse(new DefaultResponse(request, 200, "",
			new Array<>(),
			IOUtils.toByteArray(new ClassPathResource("spring-cloud-versions.json")
					.getInputStream())));
	doReturn(request).when(requestURI).back();
	doReturn(requestURI).when(requestURI).path(eq(SPRING_CLOUD_RELEASE_TAGS_PATH));
	doReturn(requestURI).when(request).uri();
	doReturn(jsonResponse).when(response).as(eq(JsonResponse.class));
	doReturn(response).when(request).fetch();
	doReturn(request).when(github).entry();
	InitializrSpringCloudInfoService service = spy(
			new InitializrSpringCloudInfoService(rest, github, githubPomReader));
	assertThat(service.getSpringCloudVersions(),
			Matchers.equalTo(SpringCloudInfoTestData.springCloudVersions.stream()
					.map(v -> v.replaceFirst("v", "")).collect(Collectors.toList())));
}
 
Example #2
Source File: Follow.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void perform(Command command, Logger logger) throws IOException {
    final String author = command.authorLogin();
    final Github github = command.issue().repo().github();
    final Request follow = github.entry()
        .uri().path("/user/following/").path(author).back()
        .method("PUT");
    logger.info("Following Github user " + author + " ...");
    try {
        final int status = follow.fetch().status();
        if(status != HttpURLConnection.HTTP_NO_CONTENT) {
            logger.error("User follow status response is " + status + " . Should have been 204 (NO CONTENT)");
        } else {
            logger.info("Followed user " + author + " .");
        }
    } catch (final IOException ex) {//don't rethrow, this is just a cosmetic step, not critical.
        logger.error("IOException while trying to follow the user.");
    }
    this.next().perform(command, logger);
}
 
Example #3
Source File: CachedRepo.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns true if the repository has a gh-pages branch, false otherwise.
 * The result is <b>cached</b> and so the http call to Github API is performed only at the first call.
 * @return true if there is a gh-pages branch, false otherwise.
 * @throws IOException If an error occurs
 *  while communicating with the Github API.
 */
public boolean hasGhPagesBranch() throws IOException {
    try {
        if(this.ghPagesBranch == null) {
            String branchesUrlPattern = this.json().getString("branches_url");
            String ghPagesUrl = branchesUrlPattern.substring(0, branchesUrlPattern.indexOf("{")) + "/gh-pages";
            Request req = new ApacheRequest(ghPagesUrl);
            this.ghPagesBranch = req.fetch().as(RestResponse.class)
                .assertStatus(
                    Matchers.isOneOf(
                        HttpURLConnection.HTTP_OK,
                        HttpURLConnection.HTTP_NOT_FOUND
                    )
                ).status() == HttpURLConnection.HTTP_OK;
            return this.ghPagesBranch;
        }
        return this.ghPagesBranch;
    } catch (AssertionError aerr) {
        throw new IOException ("Unexpected HTTP status response.", aerr);
    }
}
 
Example #4
Source File: Command.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the commands' author org membership.
 * @return Json membership as described
 * <a href="https://developer.github.com/v3/orgs/members/#get-organization-membership">here</a>
 * or an empty Json object if the repo owner is not an organization.
 * @throws IOException If something goes wrong.
 */
public JsonObject authorOrgMembership() throws IOException {
    if(this.repo().isOwnedByOrganization()) {
        Request req = this.issue.repo().github().entry()
            .uri().path("/orgs/")
            .path(this.repo().ownerLogin())
            .path("/memberships/")
            .path(this.authorLogin()).back();
        return req.fetch().as(JsonResponse.class).json().readObject();
    } else {
        return Json.createObjectBuilder().build();
    }
        
}
 
Example #5
Source File: CachingGithub.java    From spring-cloud-release-tools with Apache License 2.0 4 votes vote down vote up
@Override
public Request entry() {
	return this.delegate.entry();
}
 
Example #6
Source File: FtRemoteTest.java    From takes with MIT License 4 votes vote down vote up
/**
 * FtRemote can work in parallel threads.
 * @throws Exception If some problem inside
 */
@Test
public void worksInParallelThreads() throws Exception {
    final Take take = new Take() {
        @Override
        public Response act(final org.takes.Request req)
            throws IOException {
            MatcherAssert.assertThat(
                new RqFormBase(req).param("alpha"),
                Matchers.hasItem("123")
            );
            return new RsText("works fine");
        }
    };
    final Callable<Long> task = new Callable<Long>() {
        @Override
        public Long call() throws Exception {
            new FtRemote(take).exec(
                new FtRemote.Script() {
                    @Override
                    public void exec(final URI home) throws IOException {
                        new JdkRequest(home)
                            .method(Request.POST)
                            .body().set("alpha=123").back()
                            .fetch()
                            .as(RestResponse.class)
                            .assertStatus(HttpURLConnection.HTTP_OK)
                            .assertBody(Matchers.startsWith("works"));
                    }
                }
            );
            return 0L;
        }
    };
    final int total = Runtime.getRuntime().availableProcessors() << 2;
    final Collection<Callable<Long>> tasks = new ArrayList<>(total);
    for (int idx = 0; idx < total; ++idx) {
        tasks.add(task);
    }
    final Collection<Future<Long>> futures =
        Executors.newFixedThreadPool(total).invokeAll(tasks);
    for (final Future<Long> future : futures) {
        MatcherAssert.assertThat(future.get(), Matchers.equalTo(0L));
    }
}