Search in sources :

Example 81 with Exchange

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));
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Test(org.junit.Test)

Example 82 with Exchange

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);
}
Also used : Request(com.predic8.membrane.core.http.Request) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Exchange(com.predic8.membrane.core.exchange.Exchange) HttpClient(com.predic8.membrane.core.transport.http.HttpClient) HttpRouter(com.predic8.membrane.core.HttpRouter) Test(org.junit.Test)

Example 83 with Exchange

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;
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) MembraneAuthorizationService(com.predic8.membrane.core.interceptor.oauth2.authorizationservice.MembraneAuthorizationService) IOException(java.io.IOException)

Example 84 with Exchange

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();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) WebServerInterceptor(com.predic8.membrane.core.interceptor.server.WebServerInterceptor) InvalidParameterException(java.security.InvalidParameterException) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 85 with Exchange

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();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) Before(org.junit.Before)

Aggregations

Exchange (com.predic8.membrane.core.exchange.Exchange)107 Test (org.junit.Test)39 IOException (java.io.IOException)32 Request (com.predic8.membrane.core.http.Request)25 Outcome (com.predic8.membrane.core.interceptor.Outcome)24 Response (com.predic8.membrane.core.http.Response)16 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)16 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)16 HttpRouter (com.predic8.membrane.core.HttpRouter)14 Before (org.junit.Before)13 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)12 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)11 Header (com.predic8.membrane.core.http.Header)10 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)10 CacheBuilder (com.google.common.cache.CacheBuilder)9 Rule (com.predic8.membrane.core.rules.Rule)6 URISyntaxException (java.net.URISyntaxException)6 UnknownHostException (java.net.UnknownHostException)6 ArrayList (java.util.ArrayList)6 Session (com.predic8.membrane.core.interceptor.authentication.session.SessionManager.Session)5