Search in sources :

Example 1 with ClientResponse

use of io.knotx.dataobjects.ClientResponse in project knotx by Cognifide.

the class HttpClientFacadeTest method whenServiceRequestedWithoutPathParam_expectNoServiceRequestAndBadRequest.

@Test
@KnotxConfiguration("knotx-action-adapter-http-test.json")
public void whenServiceRequestedWithoutPathParam_expectNoServiceRequestAndBadRequest(TestContext context) throws Exception {
    Async async = context.async();
    // given
    final WebClient mockedWebClient = PowerMockito.spy(webClient());
    HttpClientFacade clientFacade = new HttpClientFacade(mockedWebClient, getConfiguration());
    // when
    Single<ClientResponse> result = clientFacade.process(new AdapterRequest().setParams(new JsonObject()).setRequest(clientRequest), HttpMethod.POST);
    // then
    result.doOnError(error -> {
        context.assertEquals("Parameter `path` was not defined in `params`!", error.getMessage());
        Mockito.verify(mockedWebClient, Mockito.times(0)).request(Matchers.any(), Matchers.anyInt(), Matchers.anyString(), Matchers.anyString());
    }).subscribe(response -> context.fail("Error should occur!"), error -> async.complete());
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Matchers(org.mockito.Matchers) UnsupportedServiceException(io.knotx.adapter.common.exception.UnsupportedServiceException) RunWith(org.junit.runner.RunWith) Logback(io.knotx.junit.rule.Logback) Single(io.reactivex.Single) ClientResponse(io.knotx.dataobjects.ClientResponse) Vertx(io.vertx.reactivex.core.Vertx) Lists(com.google.common.collect.Lists) AdapterRequest(io.knotx.dataobjects.AdapterRequest) FileReader(io.knotx.junit.util.FileReader) ClientRequest(io.knotx.dataobjects.ClientRequest) JsonObject(io.vertx.core.json.JsonObject) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) PowerMockito(org.powermock.api.mockito.PowerMockito) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) HttpAdapterConfiguration(io.knotx.adapter.common.http.HttpAdapterConfiguration) WebClient(io.vertx.reactivex.ext.web.client.WebClient) RuleChain(org.junit.rules.RuleChain) Mockito(org.mockito.Mockito) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) Rule(org.junit.Rule) ServiceMetadata(io.knotx.adapter.common.http.ServiceMetadata) HttpMethod(io.vertx.core.http.HttpMethod) Pattern(java.util.regex.Pattern) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Collections(java.util.Collections) AdapterRequest(io.knotx.dataobjects.AdapterRequest) Async(io.vertx.ext.unit.Async) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) JsonObject(io.vertx.core.json.JsonObject) WebClient(io.vertx.reactivex.ext.web.client.WebClient) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Example 2 with ClientResponse

use of io.knotx.dataobjects.ClientResponse in project knotx by Cognifide.

the class ActionKnotProxyImpl method processAdapterResponse.

private KnotContext processAdapterResponse(KnotContext knotContext, List<FormEntity> forms, FormEntity form, AdapterResponse response) {
    final ClientResponse clientResponse = response.getResponse();
    final String signal = response.getSignal();
    if (HttpResponseStatus.OK.code() != clientResponse.getStatusCode()) {
        return errorKnotResponse(clientResponse, knotContext, form);
    } else {
        String redirectLocation = form.url(signal).orElse(FORM_NO_REDIRECT_SIGNAL);
        return shouldRedirect(redirectLocation) ? redirectKnotResponse(knotContext, form, clientResponse, redirectLocation) : routeToNextKnotResponse(clientResponse, knotContext, forms, form);
    }
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse)

Example 3 with ClientResponse

use of io.knotx.dataobjects.ClientResponse in project knotx by Cognifide.

the class ActionKnotProxyVerticleTest method createMockAdapter.

private void createMockAdapter(String address, String addToBody, String signal, Map<String, List<String>> headers) {
    Func1<AdapterRequest, AdapterResponse> adapter = adapterRequest -> {
        ClientResponse response = new ClientResponse();
        response.setStatusCode(HttpResponseStatus.OK.code());
        response.setBody(Buffer.buffer().appendString(addToBody));
        response.setHeaders(headers.keySet().stream().collect(MultiMapCollector.toMultiMap(o -> o, headers::get)));
        return new AdapterResponse().setResponse(response).setSignal(signal);
    };
    new ServiceBinder(vertx.vertx()).setAddress(address).register(AdapterProxy.class, new MockAdapterImpl(adapter));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) KnotContext(io.knotx.dataobjects.KnotContext) Fragment(io.knotx.dataobjects.Fragment) AdapterProxy(io.knotx.proxy.AdapterProxy) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) RunWith(org.junit.runner.RunWith) MultiMapCollector(io.knotx.http.MultiMapCollector) Logback(io.knotx.junit.rule.Logback) Action1(rx.functions.Action1) Parser(org.jsoup.parser.Parser) ClientResponse(io.knotx.dataobjects.ClientResponse) Vertx(io.vertx.reactivex.core.Vertx) Func1(rx.functions.Func1) Lists(com.google.common.collect.Lists) AdapterRequest(io.knotx.dataobjects.AdapterRequest) FileReader(io.knotx.junit.util.FileReader) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) KnotProxy(io.knotx.reactivex.proxy.KnotProxy) KnotContextFactory(io.knotx.junit.util.KnotContextFactory) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Consumer(io.reactivex.functions.Consumer) Future(io.vertx.core.Future) RuleChain(org.junit.rules.RuleChain) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) Rule(org.junit.Rule) AdapterResponse(io.knotx.dataobjects.AdapterResponse) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) Document(org.jsoup.nodes.Document) Optional(java.util.Optional) Entities(org.jsoup.nodes.Entities) Jsoup(org.jsoup.Jsoup) Handler(io.vertx.core.Handler) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Collections(java.util.Collections) ClientResponse(io.knotx.dataobjects.ClientResponse) AdapterRequest(io.knotx.dataobjects.AdapterRequest) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) AdapterResponse(io.knotx.dataobjects.AdapterResponse)

Example 4 with ClientResponse

use of io.knotx.dataobjects.ClientResponse in project knotx by Cognifide.

the class ServiceKnotProxyImpl method processError.

@Override
protected KnotContext processError(KnotContext knotContext, Throwable error) {
    LOGGER.error("Error happened during Template processing", error);
    ClientResponse errorResponse = new ClientResponse().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
    return new KnotContext().setClientRequest(knotContext.getClientRequest()).setClientResponse(errorResponse);
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) KnotContext(io.knotx.dataobjects.KnotContext)

Example 5 with ClientResponse

use of io.knotx.dataobjects.ClientResponse in project knotx by Cognifide.

the class FragmentAssemblerKnotProxyImpl method createSuccessResponse.

private KnotContext createSuccessResponse(KnotContext inputContext, String renderedContent) {
    ClientResponse clientResponse = inputContext.getClientResponse();
    if (StringUtils.isBlank(renderedContent)) {
        clientResponse.setStatusCode(HttpResponseStatus.NO_CONTENT.code());
    } else {
        MultiMap headers = clientResponse.getHeaders();
        headers.add(HttpHeaders.CONTENT_LENGTH.toString().toLowerCase(), Integer.toString(renderedContent.length()));
        clientResponse.setBody(Buffer.buffer(renderedContent)).setHeaders(headers);
        clientResponse.setStatusCode(HttpResponseStatus.OK.code());
    }
    return new KnotContext().setClientRequest(inputContext.getClientRequest()).setClientResponse(clientResponse);
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) MultiMap(io.vertx.reactivex.core.MultiMap) KnotContext(io.knotx.dataobjects.KnotContext)

Aggregations

ClientResponse (io.knotx.dataobjects.ClientResponse)14 MultiMap (io.vertx.reactivex.core.MultiMap)9 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)8 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)7 Logback (io.knotx.junit.rule.Logback)7 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)7 HttpMethod (io.vertx.core.http.HttpMethod)7 JsonObject (io.vertx.core.json.JsonObject)7 Async (io.vertx.ext.unit.Async)7 TestContext (io.vertx.ext.unit.TestContext)7 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)7 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)7 Vertx (io.vertx.reactivex.core.Vertx)7 Rule (org.junit.Rule)7 Test (org.junit.Test)7 RuleChain (org.junit.rules.RuleChain)7 RunWith (org.junit.runner.RunWith)7 Lists (com.google.common.collect.Lists)6 AdapterRequest (io.knotx.dataobjects.AdapterRequest)6 ClientRequest (io.knotx.dataobjects.ClientRequest)6