Search in sources :

Example 31 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method createRequestFilter.

// ///////////////////////////////////////////////////////////////////////
/**
 * Simulates server side
 *
 * @return Response containing request method and entity
 */
protected ClientRequestFilter createRequestFilter() {
    ClientRequestFilter filter = new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext ctx) throws IOException {
            StringBuilder sb = new StringBuilder();
            sb.append(ctx.getMethod()).append(";");
            if (ctx.hasEntity())
                sb.append(ctx.getEntity()).append(";");
            Response r = Response.ok(sb.toString()).build();
            ctx.abortWith(r);
        }
    };
    return filter;
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response)

Example 32 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method fromResourceMethodLinkUsedInInvocationTest.

/*
     * @testName: fromResourceMethodLinkUsedInInvocationTest
     * 
     * @assertion_ids: JAXRS:JAVADOC:787;
     * 
     * @test_Strategy: Generate a link by introspecting a resource method.
     */
@Test
public void fromResourceMethodLinkUsedInInvocationTest() throws Fault {
    final String linkName = "link";
    Client client = ClientBuilder.newClient();
    client.register(new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext ctx) throws IOException {
            String uri = ctx.getUri().toASCIIString();
            Link.Builder builder = Link.fromMethod(Resource.class, "consumesAppJson").rel(linkName);
            Link link = builder.build();
            Response response = Response.ok(uri).links(link).build();
            ctx.abortWith(response);
        }
    });
    // Phase 1, ask for a link;
    WebTarget target = client.target(url() + "resource/get");
    Response response = target.request().get();
    String entity = response.readEntity(String.class);
    assertTrue(response.hasLink(linkName), "No link received");
    assertContains(url() + "resource/get", entity);
    // Phase 2, use the link, check the correctness
    assertTrue(response.hasLink(linkName), "No link received");
    Link link = response.getLink(linkName);
    response = client.invocation(link).post(null);
    entity = response.readEntity(String.class);
    assertTrue(response.hasLink(linkName), "No link received");
    assertContains(url() + "resource/consumesappjson", entity);
    logMsg("Opaque Link has been used in Client.invocation() sucessfully");
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) Builder(jakarta.ws.rs.core.Link.Builder) UriBuilder(jakarta.ws.rs.core.UriBuilder) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) IOException(java.io.IOException) WebTarget(jakarta.ws.rs.client.WebTarget) JAXRSCommonClient(ee.jakarta.tck.ws.rs.common.JAXRSCommonClient) Client(jakarta.ws.rs.client.Client) Link(jakarta.ws.rs.core.Link) Test(org.junit.jupiter.api.Test)

Example 33 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class ReaderClient method createRequestFilter.

/**
 * Create a request filter to be aborted with given fake response simulating
 * the resource from a request
 *
 * @param response
 * @return
 */
protected static ClientRequestFilter createRequestFilter(final Response response) {
    ClientRequestFilter outFilter = new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext context) throws IOException {
            Response r;
            if (response == null)
                r = Response.ok().build();
            else
                r = response;
            context.abortWith(r);
        }
    };
    return outFilter;
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response)

Example 34 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method getHeaderStringUsesToStringTest.

/*
   * @testName: getHeaderStringUsesToStringTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:780;
   * 
   * @test_Strategy: Get a HTTP header as a single string value. Each single
   * header value is converted to String using its toString method if a header
   * delegate is not available.
   */
@Test
public void getHeaderStringUsesToStringTest() throws Fault {
    final StringBean bean = new StringBean("bean");
    ClientRequestFilter filter = new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext ctx) throws IOException {
            ctx.getHeaders().add(bean.get(), bean);
        }
    };
    setProperty(Property.REQUEST, buildRequest(Request.POST, "headerstring"));
    setProperty(Property.CONTENT, bean.get());
    addProvider(filter);
    setProperty(Property.SEARCH_STRING, bean.toString());
    invoke();
    logMsg("#getHeaderString() returned expected header converted by toString() method");
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) StringBean(ee.jakarta.tck.ws.rs.common.provider.StringBean) Test(org.junit.jupiter.api.Test)

Example 35 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method setMethodTest.

/*
   * @testName: setMethodTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:452; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * 
   * @test_Strategy: Set the request method.
   */
@Test
public void setMethodTest() throws Fault {
    String entity = "ENTITY";
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            context.setMethod("PUT");
        }
    };
    addProvider(provider);
    setRequestContentEntity(entity);
    setProperty(Property.REQUEST, buildRequest(Request.POST, "put"));
    invoke();
    String body = getResponseBody();
    assertContains(body, entity);
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) ContextProvider(ee.jakarta.tck.ws.rs.api.client.clientrequestcontext.ContextProvider) Test(org.junit.jupiter.api.Test)

Aggregations

ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)77 Response (jakarta.ws.rs.core.Response)71 Test (org.junit.jupiter.api.Test)70 Invocation (jakarta.ws.rs.client.Invocation)38 ClientResponseContext (jakarta.ws.rs.client.ClientResponseContext)29 ClientRequestFilter (jakarta.ws.rs.client.ClientRequestFilter)10 MediaType (jakarta.ws.rs.core.MediaType)8 Locale (java.util.Locale)6 WebTarget (jakarta.ws.rs.client.WebTarget)5 ContextProvider (ee.jakarta.tck.ws.rs.api.client.clientrequestcontext.ContextProvider)4 StringBean (ee.jakarta.tck.ws.rs.common.provider.StringBean)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Date (java.util.Date)4 JAXRSCommonClient (ee.jakarta.tck.ws.rs.common.JAXRSCommonClient)3 StringBeanRuntimeDelegate (ee.jakarta.tck.ws.rs.common.provider.StringBeanRuntimeDelegate)3 StringBeanWithAnnotation (ee.jakarta.tck.ws.rs.common.provider.StringBeanWithAnnotation)3 Client (jakarta.ws.rs.client.Client)3 Link (jakarta.ws.rs.core.Link)3 RuntimeDelegate (jakarta.ws.rs.ext.RuntimeDelegate)3 IOException (java.io.IOException)3