use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class RateLimitInterceptorTest method testHandleRequestRateLimit1Second.
@Test
public void testHandleRequestRateLimit1Second() throws Exception {
Exchange exc = new Exchange(null);
exc.setResponse(ResponseBuilder.newInstance().build());
exc.setRemoteAddrIp("192.168.1.100");
int tryLimit = 16;
int rateLimitSeconds = 1;
RateLimitInterceptor rli = new RateLimitInterceptor(Duration.standardSeconds(rateLimitSeconds), tryLimit);
for (int i = 0; i < tryLimit; i++) {
assertEquals(Outcome.CONTINUE, rli.handleRequest(exc));
}
assertEquals(Outcome.RETURN, rli.handleRequest(exc));
Thread.sleep(1000);
for (int i = 0; i < tryLimit; i++) {
assertEquals(Outcome.CONTINUE, rli.handleRequest(exc));
}
assertEquals(Outcome.RETURN, rli.handleRequest(exc));
}
use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class OAuth2ResourceTest method testUseRefreshTokenOnTokenExpiration.
// this test also implicitly tests concurrency on oauth2resource
@Test
public void testUseRefreshTokenOnTokenExpiration() throws Exception {
HttpRouter mockAuthServer = new HttpRouter();
mockAuthServer.getRuleManager().addProxyAndOpenPortIfNew(getMockAuthServiceProxy());
mockAuthServer.init();
HttpRouter oauth2Resource = new HttpRouter();
oauth2Resource.getRuleManager().addProxyAndOpenPortIfNew(getConfiguredOAuth2Resource());
oauth2Resource.init();
HttpClient httpClient = new HttpClient();
Exchange excCallResource = new Request.Builder().get(getClientAddress()).buildExchange();
excCallResource = httpClient.call(excCallResource);
String cookie = excCallResource.getResponse().getHeader().getFirstValue("Set-Cookie");
Exchange excFollowRedirectToAuth = new Request.Builder().header("Cookie", cookie).get(excCallResource.getResponse().getHeader().getFirstValue("Location")).buildExchange();
excFollowRedirectToAuth = httpClient.call(excFollowRedirectToAuth);
Exchange excFollowRedirectToClient = new Request.Builder().header("Cookie", cookie).get(excFollowRedirectToAuth.getResponse().getHeader().getFirstValue("Location")).buildExchange();
excFollowRedirectToClient = httpClient.call(excFollowRedirectToClient);
Set<String> accessTokens = new HashSet<>();
int limit = 1000;
List<Thread> threadList = new ArrayList<>();
CountDownLatch cdl = new CountDownLatch(limit);
for (int i = 0; i < limit; i++) {
threadList.add(new Thread(() -> {
try {
cdl.countDown();
cdl.await();
Exchange excCallResource2 = new Request.Builder().get(getClientAddress()).header("Cookie", cookie).buildExchange();
excCallResource2 = httpClient.call(excCallResource2);
synchronized (accessTokens) {
accessTokens.add(excCallResource2.getResponse().getBodyAsStringDecoded());
}
} catch (Exception e) {
e.printStackTrace();
}
}));
}
threadList.forEach(thread -> thread.start());
threadList.forEach(thread -> {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
assertTrue(accessTokens.size() == limit);
}
use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class OAuth2ResourceTest method getConfiguredOAuth2Resource.
private ServiceProxy getConfiguredOAuth2Resource() {
ServiceProxy sp = new ServiceProxy(new ServiceProxyKey(clientPort), null, 99999);
OAuth2ResourceInterceptor oAuth2ResourceInterceptor = new OAuth2ResourceInterceptor();
MembraneAuthorizationService auth = new MembraneAuthorizationService();
auth.setSrc(getServerAddress());
auth.setClientId("2343243242");
auth.setClientSecret("3423233123123");
auth.setScope("openid profile");
oAuth2ResourceInterceptor.setAuthService(auth);
sp.getInterceptors().add(oAuth2ResourceInterceptor);
sp.getInterceptors().add(new AbstractInterceptor() {
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
OAuth2AnswerParameters answer = OAuth2AnswerParameters.deserialize(String.valueOf(exc.getProperty(Exchange.OAUTH2)));
exc.setResponse(Response.ok(answer.getAccessToken()).build());
return Outcome.RETURN;
}
});
return sp;
}
use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class ResolverTest method setup.
@BeforeClass
public static void setup() throws Exception {
ServiceProxy sp = new ServiceProxy(new ServiceProxyKey(3029), "localhost", 8080);
sp.getInterceptors().add(new AbstractInterceptor() {
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
hit = true;
return Outcome.CONTINUE;
}
});
WebServerInterceptor i = new WebServerInterceptor();
if (deployment.equals(STANDALONE))
i.setDocBase("src/test/resources");
else {
i.setDocBase("/test");
router.getResolverMap().addSchemaResolver(resolverMap.getFileSchemaResolver());
}
sp.getInterceptors().add(i);
router.add(sp);
router.init();
}
use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class IllegalCharactersInURLTest method init.
@Before
public void init() throws Exception {
r = new HttpRouter();
r.setHotDeploy(false);
r.add(new ServiceProxy(new ServiceProxyKey(3027), "localhost", 3028));
ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey(3028), null, 80);
sp2.getInterceptors().add(new AbstractInterceptor() {
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
Assert.assertEquals("/foo{}", exc.getRequestURI());
exc.setResponse(Response.ok().build());
return Outcome.RETURN;
}
});
r.add(sp2);
r.start();
}
Aggregations