ConfluenceRestClient implements ConfluenceClient { <T> T sendRequest(HttpRequestBase httpRequest, Function<HttpResponse, T> responseHandler) { httpRequest.addHeader(AUTHORIZATION, basicAuthorizationHeaderValue(this.username, this.password)); if (this.rateLimiter != null) { this.rateLimiter.acquire(1); } try (CloseableHttpResponse response = this.httpClient.execute(httpRequest)) { return responseHandler.apply(response); } catch (IOException e) { throw new RequestFailedException(httpRequest, null, e); } } ConfluenceRestClient(String rootConfluenceUrl, boolean disableSslVerification, Double maxRequestsPerSecond, String username, String password);  ConfluenceRestClient(String rootConfluenceUrl, ProxyConfiguration proxyConfiguration, boolean disableSslVerification, Double maxRequestsPerSecond, String username, String password);  ConfluenceRestClient(String rootConfluenceUrl, CloseableHttpClient httpClient, Double maxRequestsPerSecond, String username, String password); @Override String addPageUnderAncestor(String spaceKey, String ancestorId, String title, String content, String versionMessage); @Override void updatePage(String contentId, String ancestorId, String title, String content, int newVersion, String versionMessage); @Override void deletePage(String contentId); @Override String getPageByTitle(String spaceKey, String title); @Override void addAttachment(String contentId, String attachmentFileName, InputStream attachmentContent); @Override void updateAttachmentContent(String contentId, String attachmentId, InputStream attachmentContent); @Override void deleteAttachment(String attachmentId); @Override ConfluenceAttachment getAttachmentByFileName(String contentId, String attachmentFileName); @Override ConfluencePage getPageWithContentAndVersionById(String contentId); @Override List<ConfluencePage> getChildPages(String contentId); @Override List<ConfluenceAttachment> getAttachments(String contentId); @Override void setPropertyByKey(String contentId, String key, String value); @Override String getPropertyByKey(String contentId, String key); @Override void deletePropertyByKey(String contentId, String key); @Override List<String> getLabels(String contentId); @Override void addLabels(String contentId, List<String> labels); @Override void deleteLabel(String contentId, String label);  }
@Test public void sendRequest_withProvidedUsernameAndPassword_setsCredentialsProvider() throws Exception { CloseableHttpClient closeableHttpClient = anyCloseableHttpClient(); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient("http: HttpGet httpRequest = new HttpGet("http: ArgumentCaptor<HttpRequestBase> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); confluenceRestClient.sendRequest(httpRequest, (response) -> null); verify(closeableHttpClient, times(1)).execute(httpRequestArgumentCaptor.capture()); HttpRequestBase httpRequestBase = httpRequestArgumentCaptor.getValue(); assertThat(httpRequestBase, is(httpRequest)); assertThat(httpRequestBase.getHeaders("Authorization").length, is(1)); assertThat(httpRequestBase.getFirstHeader("Authorization").getValue(), is("Basic dXNlcm5hbWU6cGFzc3dvcmQ=")); }
@Test public void sendRequest_withRateLimitEnabled_blocksBeforeSendingSecondRequest() { CloseableHttpClient closeableHttpClient = anyCloseableHttpClient(); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient("http: HttpGet httpRequest = new HttpGet("http: long startTime = currentTimeMillis(); confluenceRestClient.sendRequest(httpRequest, (response) -> null); confluenceRestClient.sendRequest(httpRequest, (response) -> null); long endTime = currentTimeMillis(); long durationInMillisOfBothRequests = endTime - startTime; assertThat(durationInMillisOfBothRequests, is(greaterThan(1000L))); }