use of org.opencastproject.kernel.http.api.HttpClient in project opencast by opencast.
the class TrustedHttpClientImplTest method failsIfNonceReturnAndNoRetries.
@Test
public void failsIfNonceReturnAndNoRetries() throws ClientProtocolException, IOException {
// Setup bundle context for TrustedHttpClientImpl
bundleContextMock = createNiceMock(BundleContext.class);
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_USER_KEY)).andReturn("matterhorn_system_account");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_PASS_KEY)).andReturn("CHANGE_ME");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_KEY)).andReturn("0");
replay(bundleContextMock);
componentContextMock = createNiceMock(ComponentContext.class);
expect(componentContextMock.getBundleContext()).andReturn(bundleContextMock).anyTimes();
replay(componentContextMock);
client = new TrustedHttpClientImpl("matterhorn_system_account", "CHANGE_ME");
client.setServiceRegistry(serviceRegistry);
client.setSecurityService(securityService);
client.activate(componentContextMock);
HttpPost httpPost = new HttpPost("http://localhost:8080/fake");
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
ClientConnectionManager clientConnectionManager = createMock(ClientConnectionManager.class);
HttpClient requestDefaultHttpClient = createMock("Request", HttpClient.class);
expect(requestDefaultHttpClient.getParams()).andReturn(httpParams).anyTimes();
// Digest authentication and close
expect(requestDefaultHttpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
expect(requestDefaultHttpClient.getConnectionManager()).andReturn(clientConnectionManager);
// Try request and close.
expect(requestDefaultHttpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
replay(requestDefaultHttpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(requestDefaultHttpClient).atLeastOnce();
replay(httpClientFactory);
client.setHttpClientFactory(httpClientFactory);
HttpResponse response = client.execute(httpPost);
Assert.assertEquals(401, response.getStatusLine().getStatusCode());
verify(requestDefaultHttpClient);
}
use of org.opencastproject.kernel.http.api.HttpClient in project opencast by opencast.
the class TrustedHttpClientImplTest method testNotAcceptsUrlSigningService.
@Test
public void testNotAcceptsUrlSigningService() throws IOException {
String notAcceptsUrl = "http://notaccepts.com";
HttpGet get = new HttpGet(notAcceptsUrl);
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(notAcceptsUrl)).andReturn(false);
EasyMock.replay(urlSigningService);
CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
Capture<HttpUriRequest> request = EasyMock.newCapture();
// Setup Http Client
HttpClient httpClient = createMock("Request", HttpClient.class);
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
expect(httpClient.getCredentialsProvider()).andReturn(cp);
expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
EasyMock.replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client = new TrustedHttpClientImpl("user", "pass");
client.setHttpClientFactory(httpClientFactory);
client.setSecurityService(securityService);
client.setUrlSigningService(urlSigningService);
client.execute(get);
assertTrue(request.hasCaptured());
assertEquals(get.getURI().toString(), request.getValue().getURI().toString());
}
use of org.opencastproject.kernel.http.api.HttpClient in project opencast by opencast.
the class TrustedHttpClientImplTest method successfullRequestResultsInNoRetries.
@Test
public void successfullRequestResultsInNoRetries() throws ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost("http://localhost:8080/fake");
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
ClientConnectionManager clientConnectionManager = createMock(ClientConnectionManager.class);
// Setup DefaultHttpClients
HttpClient securityDefaultHttpClient = createMock("Digest", HttpClient.class);
expect(securityDefaultHttpClient.getParams()).andReturn(httpParams).anyTimes();
expect(securityDefaultHttpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
expect(securityDefaultHttpClient.getConnectionManager()).andReturn(clientConnectionManager);
replay(securityDefaultHttpClient);
HttpClient requestDefaultHttpClient = createMock("Request", HttpClient.class);
expect(requestDefaultHttpClient.getParams()).andReturn(httpParams).anyTimes();
expect(requestDefaultHttpClient.execute(isA(HttpUriRequest.class))).andReturn(okResponse);
replay(requestDefaultHttpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(requestDefaultHttpClient);
expect(httpClientFactory.makeHttpClient()).andReturn(securityDefaultHttpClient);
replay(httpClientFactory);
client.setHttpClientFactory(httpClientFactory);
HttpResponse response = client.execute(httpPost);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}
use of org.opencastproject.kernel.http.api.HttpClient in project opencast by opencast.
the class TrustedHttpClientImplTest method failsIfNonceReturnAndOneRetries.
@Test
public void failsIfNonceReturnAndOneRetries() throws ClientProtocolException, IOException {
// Setup bundle context for TrustedHttpClientImpl
bundleContextMock = createNiceMock(BundleContext.class);
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_USER_KEY)).andReturn("matterhorn_system_account");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_PASS_KEY)).andReturn("CHANGE_ME");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_KEY)).andReturn("1");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_BASE_TIME_KEY)).andReturn("0");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_MAXIMUM_VARIABLE_TIME_KEY)).andReturn("0");
replay(bundleContextMock);
componentContextMock = createNiceMock(ComponentContext.class);
expect(componentContextMock.getBundleContext()).andReturn(bundleContextMock).anyTimes();
replay(componentContextMock);
client = new TrustedHttpClientImpl("matterhorn_system_account", "CHANGE_ME");
client.setServiceRegistry(serviceRegistry);
client.setSecurityService(securityService);
client.activate(componentContextMock);
HttpPost httpPost = new HttpPost("http://localhost:8080/fake");
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
ClientConnectionManager clientConnectionManager = createMock(ClientConnectionManager.class);
HttpClient httpClient = createMock("Request", HttpClient.class);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
// Security Handshake and close.
expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
// First request and close.
expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
// Second Security Handshake and close.
expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
// Retry request and close.
expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client.setHttpClientFactory(httpClientFactory);
HttpResponse response = client.execute(httpPost);
Assert.assertEquals(401, response.getStatusLine().getStatusCode());
}
use of org.opencastproject.kernel.http.api.HttpClient in project opencast by opencast.
the class TrustedHttpClientImplTest method successIfNonceReturnOnceAndOneRetries.
@Test
public void successIfNonceReturnOnceAndOneRetries() throws ClientProtocolException, IOException {
// Setup bundle context for TrustedHttpClientImpl
bundleContextMock = createNiceMock(BundleContext.class);
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_USER_KEY)).andReturn("matterhorn_system_account");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_PASS_KEY)).andReturn("CHANGE_ME");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_KEY)).andReturn("1");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_BASE_TIME_KEY)).andReturn("0");
expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_MAXIMUM_VARIABLE_TIME_KEY)).andReturn("0");
replay(bundleContextMock);
componentContextMock = createNiceMock(ComponentContext.class);
expect(componentContextMock.getBundleContext()).andReturn(bundleContextMock).anyTimes();
replay(componentContextMock);
client = new TrustedHttpClientImpl("matterhorn_system_account", "CHANGE_ME");
client.setServiceRegistry(serviceRegistry);
client.setSecurityService(securityService);
client.activate(componentContextMock);
HttpPost httpPost = new HttpPost("http://localhost:8080/fake");
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
ClientConnectionManager clientConnectionManager = createMock(ClientConnectionManager.class);
HttpClient httpClient = createMock("Request", HttpClient.class);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
// Security Handshake and close.
expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
// First request and close
expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
// Nonce retry and close.
expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
// Final request with success.
expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(okResponse);
replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client.setHttpClientFactory(httpClientFactory);
HttpResponse response = client.execute(httpPost);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}
Aggregations