Search in sources :

Example 36 with ClientRequestContext

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

the class JAXRSClientIT method getHeadersIsMutableTest.

/*
   * @testName: getHeadersIsMutableTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:439; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * 
   * @test_Strategy: Get the generic entity type information.
   */
@Test
public void getHeadersIsMutableTest() throws Fault {
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            MultivaluedMap<String, Object> headers = context.getHeaders();
            headers.add("Accept-Language", "en_gb");
            headers.add("Date", "Tue, 15 Nov 1994 08:12:31 GMT");
            headers.add("tck", "cts");
        }
    };
    addProvider(provider);
    setProperty(Property.REQUEST, buildRequest(Request.GET, "headers"));
    invoke();
    String body = getResponseBody().toLowerCase();
    assertContains(body, "accept-language");
    assertContains(body, "date");
    assertContains(body, "tck");
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) ContextProvider(ee.jakarta.tck.ws.rs.api.client.clientrequestcontext.ContextProvider) Test(org.junit.jupiter.api.Test)

Example 37 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
 */
private static 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 38 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 static ClientRequestFilter createRequestFilter() {
    ClientRequestFilter filter = new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext ctx) throws IOException {
            StringBuilder sb = new StringBuilder();
            sb.append(ctx.getMethod()).append(";");
            sb.append(ctx.getUri().toASCIIString()).append(";");
            if (ctx.hasEntity())
                sb.append(ctx.getEntity()).append(";");
            List<MediaType> list = ctx.getAcceptableMediaTypes();
            for (MediaType type : list) sb.append(type).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) MediaType(jakarta.ws.rs.core.MediaType)

Example 39 with ClientRequestContext

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

the class JAXRSClientIT method getAllowedMethodsTest.

/*
   * @testName: getAllowedMethodsTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:457; JAXRS:JAVADOC:479; JAXRS:JAVADOC:480;
   * 
   * @test_Strategy: Get the allowed HTTP methods from the Allow HTTP header.
   * All methods will returned as upper case strings.
   * 
   * ClientResponseFilter.filter
   */
@Test
public void getAllowedMethodsTest() throws Fault {
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext requestContext, ClientResponseContext responseContext) throws Fault {
            Set<String> map = responseContext.getAllowedMethods();
            logMsg("found methods:", JaxrsUtil.iterableToString(" ", map));
            assertTrue(map.size() == 2, "Allowed mthods were not set");
            assertTrue(map.contains("OPTIONS"), "OPTIONS allowed method were not found");
            assertTrue(map.contains("GET"), "GET allowed method was not found");
        }
    };
    Response response = Response.ok().header(HttpHeaders.ALLOW, "get").header(HttpHeaders.ALLOW, "options").build();
    invokeWithResponseAndAssertStatus(response, Status.OK, provider);
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) ClientResponseContext(jakarta.ws.rs.client.ClientResponseContext) Test(org.junit.jupiter.api.Test)

Example 40 with ClientRequestContext

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

the class JAXRSClientIT method setEntityStreamTest.

/*
   * @testName: setEntityStreamTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:476; JAXRS:JAVADOC:479; JAXRS:JAVADOC:480;
   * 
   * @test_Strategy: Set a new entity input stream. ClientResponseFilter.filter
   */
@Test
public void setEntityStreamTest() throws Fault {
    final String entity = "ENTITY";
    ContextProvider in = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext requestContext, ClientResponseContext responseContext) throws Fault {
            responseContext.setEntityStream(new ByteArrayInputStream(entity.getBytes()));
            InputStream stream = responseContext.getEntityStream();
            assertTrue(stream != null, "the #getEntityStream is null");
            InputStreamReader isr = new InputStreamReader(stream);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            try {
                line = br.readLine();
            } catch (IOException e) {
                throw new Fault(e);
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                }
            }
            assertTrue(entity.equals(line), "The #getEntityStream " + line + " is not equal to what is inserted to the response: " + entity);
            logMsg("#setEntityStream(", entity, ") set entity", line);
        }
    };
    Response response = Response.ok().build();
    invokeWithResponseAndAssertStatus(response, Status.OK, in);
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ClientResponseContext(jakarta.ws.rs.client.ClientResponseContext) 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