use of org.apache.http.entity.BasicHttpEntity in project carbon-apimgt by wso2.
the class BlockingConditionRetrieverTest method run.
@Test
public void run() throws Exception {
String content = "{\"api\":[\"/pizzashack/1.0.0\"],\"application\":[\"admin:DefaultApplication\"]," + "\"ip\":[{\"fixedIp\":\"127.0.0.1\",\"invert\":false,\"type\":\"IP\",\"tenantDomain\":\"carbon" + ".super\"}],\"user\":[\"admin\"],\"custom\":[]}";
PowerMockito.mockStatic(APIUtil.class);
HttpClient httpClient = Mockito.mock(HttpClient.class);
HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(new ByteArrayInputStream(content.getBytes()));
Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity);
StatusLine status = Mockito.mock(StatusLine.class);
Mockito.when(status.getStatusCode()).thenReturn(200);
Mockito.when(httpResponse.getStatusLine()).thenReturn(status);
Mockito.when(httpClient.execute(Mockito.any(HttpGet.class))).thenReturn(httpResponse);
BDDMockito.given(APIUtil.getHttpClient(Mockito.anyInt(), Mockito.anyString())).willReturn(httpClient);
EventHubConfigurationDto eventHubConfigurationDto = new EventHubConfigurationDto();
eventHubConfigurationDto.setUsername("admin");
eventHubConfigurationDto.setPassword("admin".toCharArray());
eventHubConfigurationDto.setEnabled(true);
eventHubConfigurationDto.setServiceUrl("http://localhost:18083/internal/data/v1");
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
BlockingConditionRetriever blockingConditionRetriever = new BlockingConditionRetrieverWrapper(eventHubConfigurationDto, throttleDataHolder);
blockingConditionRetriever.run();
Assert.assertTrue(throttleDataHolder.isRequestBlocked("/pizzashack/1.0.0", "admin:DefaultApplication", "admin", "127.0.0.1", "carbon.super", "/pizzashack/1.0.0:1.0.0:admin-DefaultApplication"));
}
use of org.apache.http.entity.BasicHttpEntity in project cxf by apache.
the class AsyncHTTPConduit method setupConnection.
@Override
protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException {
if (factory.isShutdown()) {
message.put(USE_ASYNC, Boolean.FALSE);
super.setupConnection(message, address, csPolicy);
return;
}
propagateJaxwsSpecTimeoutSettings(message, csPolicy);
boolean addressChanged = false;
// need to do some clean up work on the URI address
URI uri = address.getURI();
String uriString = uri.toString();
if (uriString.startsWith("hc://")) {
try {
uriString = uriString.substring(5);
uri = new URI(uriString);
addressChanged = true;
} catch (URISyntaxException ex) {
throw new MalformedURLException("unsupport uri: " + uriString);
}
}
String s = uri.getScheme();
if (!"http".equals(s) && !"https".equals(s)) {
throw new MalformedURLException("unknown protocol: " + s);
}
Object o = message.getContextualProperty(USE_ASYNC);
if (o == null) {
o = factory.getUseAsyncPolicy();
}
switch(UseAsyncPolicy.getPolicy(o)) {
case ALWAYS:
o = true;
break;
case NEVER:
o = false;
break;
case ASYNC_ONLY:
default:
o = !message.getExchange().isSynchronous();
break;
}
// check tlsClientParameters from message header
TLSClientParameters clientParameters = message.get(TLSClientParameters.class);
if (clientParameters == null) {
clientParameters = tlsClientParameters;
}
if ("https".equals(uri.getScheme()) && clientParameters != null && clientParameters.getSSLSocketFactory() != null) {
// if they configured in an SSLSocketFactory, we cannot do anything
// with it as the NIO based transport cannot use socket created from
// the SSLSocketFactory.
o = false;
}
if (!PropertyUtils.isTrue(o)) {
message.put(USE_ASYNC, Boolean.FALSE);
super.setupConnection(message, addressChanged ? new Address(uriString, uri) : address, csPolicy);
return;
}
if (StringUtils.isEmpty(uri.getPath())) {
// hc needs to have the path be "/"
uri = uri.resolve("/");
}
message.put(USE_ASYNC, Boolean.TRUE);
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Asynchronous connection to " + uri.toString() + " has been set up");
}
message.put("http.scheme", uri.getScheme());
String httpRequestMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
if (httpRequestMethod == null) {
httpRequestMethod = "POST";
message.put(Message.HTTP_REQUEST_METHOD, httpRequestMethod);
}
final CXFHttpRequest e = new CXFHttpRequest(httpRequestMethod);
BasicHttpEntity entity = new BasicHttpEntity() {
public boolean isRepeatable() {
return e.getOutputStream().retransmitable();
}
};
entity.setChunked(true);
entity.setContentType((String) message.get(Message.CONTENT_TYPE));
e.setURI(uri);
e.setEntity(entity);
RequestConfig.Builder b = RequestConfig.custom().setConnectTimeout((int) csPolicy.getConnectionTimeout()).setSocketTimeout((int) csPolicy.getReceiveTimeout()).setConnectionRequestTimeout((int) csPolicy.getConnectionRequestTimeout());
Proxy p = proxyFactory.createProxy(csPolicy, uri);
if (p != null && p.type() != Proxy.Type.DIRECT) {
InetSocketAddress isa = (InetSocketAddress) p.address();
HttpHost proxy = new HttpHost(isa.getHostString(), isa.getPort());
b.setProxy(proxy);
}
e.setConfig(b.build());
message.put(CXFHttpRequest.class, e);
}
use of org.apache.http.entity.BasicHttpEntity in project gocd by gocd.
the class HttpServiceTest method shouldDownloadArtifact.
@Test
public void shouldDownloadArtifact() throws IOException, URISyntaxException {
String url = "http://blah";
FetchHandler fetchHandler = mock(FetchHandler.class);
HttpGet mockGetMethod = mock(HttpGet.class);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
ByteArrayInputStream instream = new ByteArrayInputStream(new byte[] {});
basicHttpEntity.setContent(instream);
when(response.getEntity()).thenReturn(basicHttpEntity);
when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
when(httpClient.execute(mockGetMethod)).thenReturn(response);
when(httpClientFactory.createGet(url)).thenReturn(mockGetMethod);
when(mockGetMethod.getURI()).thenReturn(new URI(url));
service.download(url, fetchHandler);
verify(httpClient).execute(mockGetMethod);
verify(fetchHandler).handle(instream);
}
use of org.apache.http.entity.BasicHttpEntity in project robolectric by robolectric.
the class ShadowDefaultRequestDirector method interceptResponseContent.
private void interceptResponseContent(HttpResponse response) {
HttpEntity entity = response.getEntity();
if (entity instanceof HttpEntityWrapper) {
HttpEntityWrapper entityWrapper = (HttpEntityWrapper) entity;
try {
Field wrappedEntity = HttpEntityWrapper.class.getDeclaredField("wrappedEntity");
wrappedEntity.setAccessible(true);
entity = (HttpEntity) wrappedEntity.get(entityWrapper);
} catch (Exception e) {
// fail to record
}
}
if (entity instanceof BasicHttpEntity) {
BasicHttpEntity basicEntity = (BasicHttpEntity) entity;
try {
Field contentField = BasicHttpEntity.class.getDeclaredField("content");
contentField.setAccessible(true);
InputStream content = (InputStream) contentField.get(basicEntity);
byte[] buffer = Util.readBytes(content);
FakeHttp.getFakeHttpLayer().addHttpResponseContent(buffer);
contentField.set(basicEntity, new ByteArrayInputStream(buffer));
} catch (Exception e) {
// fail to record
}
}
}
use of org.apache.http.entity.BasicHttpEntity in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancingHttpClientTests method testRetryOnStatusCode.
@Test
public void testRetryOnStatusCode() throws Exception {
int retriesNextServer = 0;
int retriesSameServer = 1;
boolean retryable = true;
boolean retryOnAllOps = false;
String serviceName = "foo";
String host = serviceName;
int port = 80;
HttpMethod method = HttpMethod.GET;
URI uri = new URI("http://" + host + ":" + port);
CloseableHttpClient delegate = mock(CloseableHttpClient.class);
final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
Locale locale = new Locale("en");
doReturn(locale).when(response).getLocale();
StatusLine statusLine = mock(StatusLine.class);
doReturn(200).when(statusLine).getStatusCode();
doReturn(statusLine).when(response).getStatusLine();
final CloseableHttpResponse fourOFourResponse = mock(CloseableHttpResponse.class);
doReturn(locale).when(fourOFourResponse).getLocale();
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContentLength(5);
entity.setContent(new ByteArrayInputStream("error".getBytes()));
doReturn(entity).when(fourOFourResponse).getEntity();
StatusLine fourOFourStatusLine = mock(StatusLine.class);
doReturn(404).when(fourOFourStatusLine).getStatusCode();
doReturn(fourOFourStatusLine).when(fourOFourResponse).getStatusLine();
doReturn(fourOFourResponse).doReturn(response).when(delegate).execute(any(HttpUriRequest.class));
ILoadBalancer lb = mock(ILoadBalancer.class);
MyBackOffPolicy myBackOffPolicy = new MyBackOffPolicy();
RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "404", myBackOffPolicy);
RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
doReturn(uri).when(request).getURI();
doReturn(method).when(request).getMethod();
doReturn(request).when(request).withNewUri(any(URI.class));
HttpUriRequest uriRequest = mock(HttpUriRequest.class);
doReturn(uri).when(uriRequest).getURI();
doReturn(uriRequest).when(request).toRequest(any(RequestConfig.class));
client.execute(request, null);
verify(fourOFourResponse, times(1)).close();
verify(delegate, times(2)).execute(any(HttpUriRequest.class));
verify(lb, times(1)).chooseServer(eq(serviceName));
assertEquals(1, myBackOffPolicy.getCount());
}
Aggregations