use of com.google.mockwebserver.MockWebServer in project ribbon by Netflix.
the class RibbonTest method testValidator.
@Test
public void testValidator() throws IOException, InterruptedException {
// LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
MockWebServer server = new MockWebServer();
String content = "Hello world";
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content));
server.play();
HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withConfigurationBasedServerList("localhost:" + server.getPort()));
HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("/").withMethod("GET").withResponseValidator(new ResponseValidator<HttpClientResponse<ByteBuf>>() {
@Override
public void validate(HttpClientResponse<ByteBuf> t1) throws UnsuccessfulResponseException {
throw new UnsuccessfulResponseException("error", new IllegalArgumentException());
}
}).build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
request.toObservable().subscribe(new Action1<ByteBuf>() {
@Override
public void call(ByteBuf t1) {
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable t1) {
error.set(t1);
latch.countDown();
}
}, new Action0() {
@Override
public void call() {
}
});
latch.await();
assertTrue(error.get() instanceof HystrixBadRequestException);
assertTrue(error.get().getCause() instanceof UnsuccessfulResponseException);
}
use of com.google.mockwebserver.MockWebServer in project ribbon by Netflix.
the class RibbonTest method testCommand.
@Test
public void testCommand() throws IOException, InterruptedException, ExecutionException {
MockWebServer server = new MockWebServer();
String content = "Hello world";
MockResponse response = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content);
server.enqueue(response);
server.enqueue(response);
server.enqueue(response);
server.play();
HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withMaxAutoRetriesNextServer(3).withReadTimeout(300000).withConfigurationBasedServerList("localhost:12345, localhost:10092, localhost:" + server.getPort()));
HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("/").withMethod("GET").build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
String result = request.execute().toString(Charset.defaultCharset());
assertEquals(content, result);
// repeat the same request
ByteBuf raw = request.execute();
result = raw.toString(Charset.defaultCharset());
raw.release();
assertEquals(content, result);
result = request.queue().get().toString(Charset.defaultCharset());
assertEquals(content, result);
}
use of com.google.mockwebserver.MockWebServer in project ribbon by Netflix.
the class RibbonTest method testCommandWithMetaData.
@Test
@Ignore
public void testCommandWithMetaData() throws IOException, InterruptedException, ExecutionException {
// LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
MockWebServer server = new MockWebServer();
String content = "Hello world";
for (int i = 0; i < 6; i++) {
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content));
}
server.play();
HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withConfigurationBasedServerList("localhost:" + server.getPort()).withMaxAutoRetriesNextServer(3));
HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test").withUriTemplate("/").withMethod("GET").withCacheProvider("somekey", new CacheProvider<ByteBuf>() {
@Override
public Observable<ByteBuf> get(String key, Map<String, Object> vars) {
return Observable.error(new Exception("Cache miss"));
}
}).build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
final AtomicBoolean success = new AtomicBoolean(false);
RequestWithMetaData<ByteBuf> metaRequest = request.withMetadata();
Observable<String> result = metaRequest.toObservable().flatMap(new Func1<RibbonResponse<Observable<ByteBuf>>, Observable<String>>() {
@Override
public Observable<String> call(final RibbonResponse<Observable<ByteBuf>> response) {
success.set(response.getHystrixInfo().isSuccessfulExecution());
return response.content().map(new Func1<ByteBuf, String>() {
@Override
public String call(ByteBuf t1) {
return t1.toString(Charset.defaultCharset());
}
});
}
});
String s = result.toBlocking().single();
assertEquals(content, s);
assertTrue(success.get());
Future<RibbonResponse<ByteBuf>> future = metaRequest.queue();
RibbonResponse<ByteBuf> response = future.get();
assertTrue(future.isDone());
assertEquals(content, response.content().toString(Charset.defaultCharset()));
assertTrue(response.getHystrixInfo().isSuccessfulExecution());
RibbonResponse<ByteBuf> result1 = metaRequest.execute();
assertEquals(content, result1.content().toString(Charset.defaultCharset()));
assertTrue(result1.getHystrixInfo().isSuccessfulExecution());
}
use of com.google.mockwebserver.MockWebServer in project ribbon by Netflix.
the class ListenerTest method testSuccessExecutionOnAbosoluteURI.
@Test
public void testSuccessExecutionOnAbosoluteURI() throws IOException {
MockWebServer server = new MockWebServer();
String content = "OK";
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json").setBody(content));
server.play();
IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "2000").withProperty(CommonClientConfigKey.MaxAutoRetries, 1).withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://localhost:" + server.getPort() + "/testAsync/person");
Server badServer = new Server("localhost:12345");
Server goodServer = new Server("localhost:" + server.getPort());
List<Server> servers = Lists.newArrayList(goodServer, badServer);
BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500);
TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last();
assertEquals(200, response.getStatus().code());
assertEquals(1, listener.executionStartCounter.get());
assertEquals(1, listener.startWithServerCounter.get());
assertEquals(0, listener.exceptionWithServerCounter.get());
assertEquals(0, listener.executionFailedCounter.get());
assertEquals(1, listener.executionSuccessCounter.get());
assertEquals(500, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue());
assertTrue(listener.isContextChecked());
assertTrue(listener.isCheckExecutionInfo());
assertSame(response, listener.getResponse());
}
use of com.google.mockwebserver.MockWebServer in project j2objc by google.
the class URLConnectionTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
savedUrlCache = setNewUrlCache();
server = new MockWebServer();
hostName = server.getHostName();
}
Aggregations