Search in sources :

Example 16 with HttpClient

use of com.predic8.membrane.core.transport.http.HttpClient in project service-proxy by membrane.

the class AccessControlInterceptorIntegrationTest method setInterceptor.

/*
	 * This test can only by run on a specific machine.
	 */
/*@Test
	public void testLocalhost() throws Exception {
		setInterceptor(FILE_CLIENTS_FROM_LOCALHOST);

		HttpClient client = new HttpClient();
		HostConfiguration config = new HostConfiguration();
		config.setLocalAddress(InetAddress.getByName("localhost"));
		client.setHostConfiguration(config);

		assertEquals(200, client.executeMethod(getBLZRequestMethod()));
	}*/
/*
	 * This test can only by run on a specific machine.
	 */
/*@Test
	public void test192_168_2_Star() throws Exception {
		setInterceptor(FILE_CLIENTS_FROM_192_168_2_STAR);
		assertEquals(200, getClient(FIXED_IP).executeMethod(getBLZRequestMethod()));
	}*/
private void setInterceptor(String fileName) throws Exception {
    AccessControlInterceptor interceptor = new AccessControlInterceptor();
    interceptor.setFile(fileName);
    router.addUserFeatureInterceptor(interceptor);
    router.init();
}
Also used : AccessControlInterceptor(com.predic8.membrane.core.interceptor.acl.AccessControlInterceptor)

Example 17 with HttpClient

use of com.predic8.membrane.core.transport.http.HttpClient in project service-proxy by membrane.

the class IllegalCharactersInURLTest method doit.

@Test
public void doit() throws Exception {
    URIFactory uriFactory = new URIFactory(true);
    Response res = new HttpClient().call(new Request.Builder().method("GET").url(uriFactory, "http://localhost:3027/foo{}").body("").buildExchange()).getResponse();
    Assert.assertEquals(200, res.getStatusCode());
}
Also used : Response(com.predic8.membrane.core.http.Response) HttpResponse(org.apache.http.HttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URIFactory(com.predic8.membrane.core.util.URIFactory) Test(org.junit.Test)

Example 18 with HttpClient

use of com.predic8.membrane.core.transport.http.HttpClient in project service-proxy by membrane.

the class RegExReplaceInterceptorTest method testReplace.

@Test
public void testReplace() throws Exception {
    router = Router.init("src/test/resources/regex-monitor-beans.xml");
    Rule serverRule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3009), "www.predic8.de", 80);
    router.getRuleManager().addProxyAndOpenPortIfNew(serverRule);
    router.init();
    try {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod("http://localhost:3009");
        method.setRequestHeader(Header.CONTENT_TYPE, MimeType.TEXT_XML_UTF8);
        method.setRequestHeader(Header.SOAP_ACTION, "");
        assertEquals(200, client.executeMethod(method));
        assertTrue(new String(method.getResponseBody()).contains("Membrane RegEx Replacement Is Cool"));
    } finally {
        router.shutdown();
    }
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Rule(com.predic8.membrane.core.rules.Rule) Test(org.junit.Test)

Example 19 with HttpClient

use of com.predic8.membrane.core.transport.http.HttpClient in project service-proxy by membrane.

the class LargeBodyTest method large.

@Test
public void large() throws Exception {
    setup();
    long len = Integer.MAX_VALUE + 1l;
    Exchange e = new Request.Builder().post("http://localhost:3041/foo").body(len, new ConstantInputStream(len)).buildExchange();
    new HttpClient(hcc).call(e);
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) HttpClient(com.predic8.membrane.core.transport.http.HttpClient) Test(org.junit.Test)

Example 20 with HttpClient

use of com.predic8.membrane.core.transport.http.HttpClient in project service-proxy by membrane.

the class TelekomSMSTokenProvider method getAccessToken.

private synchronized String getAccessToken() throws Exception {
    long now = System.currentTimeMillis();
    if (token == null || tokenExpiration < now) {
        Exchange exc = new Request.Builder().post("https://global.telekom.com/gcp-web-api/oauth").header(Header.HOST, "global.telekom.com").header(Header.AUTHORIZATION, "Basic " + new String(Base64.encodeBase64((clientId + ":" + clientSecret).getBytes("UTF-8")), "UTF-8")).header(Header.ACCEPT, "application/json").header(Header.USER_AGENT, Constants.PRODUCT_NAME + " " + Constants.VERSION).header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded").body(new URLParamUtil.ParamBuilder().add("grant_type", "client_credentials").add("scope", scope).build()).buildExchange();
        new HttpClient().call(exc, false, true);
        if (exc.getResponse().getStatusCode() != 200)
            throw new RuntimeException("Telekom Authentication Server returned: " + exc.getResponse());
        HashMap<String, String> values = Util.parseSimpleJSONResponse(exc.getResponse());
        if (!values.containsKey("access_token") || !values.containsKey("expires_in"))
            throw new Exception("Telekom Authentication: Received 200 and JSON body, but no access_token or no expires_in.");
        token = values.get("access_token");
        tokenExpiration = Long.parseLong(values.get("expires_in")) + System.currentTimeMillis() - 2000;
    }
    return token;
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) URLParamUtil(com.predic8.membrane.core.util.URLParamUtil) HttpClient(com.predic8.membrane.core.transport.http.HttpClient)

Aggregations

Test (org.junit.Test)15 Exchange (com.predic8.membrane.core.exchange.Exchange)13 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)9 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)6 Outcome (com.predic8.membrane.core.interceptor.Outcome)6 Response (com.predic8.membrane.core.http.Response)5 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)5 HttpClient (org.apache.commons.httpclient.HttpClient)5 Http11Test (com.predic8.membrane.integration.Http11Test)4 Request (com.predic8.membrane.core.http.Request)3 HttpClientConfiguration (com.predic8.membrane.core.transport.http.client.HttpClientConfiguration)2 URIFactory (com.predic8.membrane.core.util.URIFactory)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 HttpRouter (com.predic8.membrane.core.HttpRouter)1 Router (com.predic8.membrane.core.Router)1 KeyStore (com.predic8.membrane.core.config.security.KeyStore)1 SSLParser (com.predic8.membrane.core.config.security.SSLParser)1 TrustStore (com.predic8.membrane.core.config.security.TrustStore)1 CountInterceptor (com.predic8.membrane.core.interceptor.CountInterceptor)1