use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method getStringHeadersReflectsTheUnderlayingMapTest.
/*
* @testName: getStringHeadersReflectsTheUnderlayingMapTest
*
* @assertion_ids: JAXRS:JAVADOC:446; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
* JAXRS:SPEC:85; JAXRS:JAVADOC:427;
*
* @test_Strategy: Get a string view of header values associated with the
* message. Changes in the underlying headers map are reflected in this view.
*
* ClientRequestFilter.abortWith
*/
@Test
public void getStringHeadersReflectsTheUnderlayingMapTest() throws Fault {
final String TCK = "cts";
ContextProvider provider = new ContextProvider() {
@Override
protected void checkFilterContext(ClientRequestContext context) throws Fault {
context.getHeaders().add(TCK, TCK);
MultivaluedMap<String, String> map;
map = context.getStringHeaders();
String value = map.getFirst(TCK);
Response r = Response.ok(value).build();
context.abortWith(r);
}
};
Invocation invocation = buildBuilder(provider).buildGet();
Response response = invoke(invocation);
String body = response.readEntity(String.class);
assertContains(body, TCK);
}
use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method getAcceptableMediaTypesTest.
/*
* @testName: getAcceptableMediaTypesTest
*
* @assertion_ids: JAXRS:JAVADOC:429; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
* JAXRS:SPEC:85; JAXRS:JAVADOC:427;
*
* @test_Strategy: Get a list of media types that are acceptable for the
* response. Returns a read-only list of requested response media types sorted
* according to their q-value, with highest preference first.
*
* ClientRequestFilter.abortWith
*/
@Test
public void getAcceptableMediaTypesTest() throws Fault {
ContextProvider provider = new ContextProvider() {
@Override
protected void checkFilterContext(ClientRequestContext context) throws Fault {
List<MediaType> types = context.getAcceptableMediaTypes();
String medias = JaxrsUtil.iterableToString(";", types);
Response r = Response.ok(medias).build();
context.abortWith(r);
}
};
String media = "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5";
Invocation.Builder builder = buildBuilder(provider);
Invocation invocation = builder.header("Accept", media).buildGet();
Response response = invoke(invocation);
String entity = response.readEntity(String.class);
assertContains(entity, "text/*");
assertContains(entity, "text/html");
assertContains(entity, "*/*");
}
use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method getHeaderStringUsingHeaderDelegateTest.
/*
* @testName: getHeaderStringUsingHeaderDelegateTest
*
* @assertion_ids: JAXRS:JAVADOC:440; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
* JAXRS:SPEC:85; JAXRS:JAVADOC:427;
*
* @test_Strategy: Get a message header as a single string value. Each single
* header value is converted to String using a RuntimeDelegate.HeaderDelegate.
*
* ClientRequestFilter.abortWith
*/
@Test
public void getHeaderStringUsingHeaderDelegateTest() throws Fault {
final String name = "BEAN";
final StringBean bean = new StringBean(name);
ContextProvider provider = new ContextProvider() {
@Override
protected void checkFilterContext(ClientRequestContext context) throws Fault {
String value = context.getHeaderString(name);
Response r = Response.ok(value).build();
context.abortWith(r);
}
};
RuntimeDelegate original = RuntimeDelegate.getInstance();
RuntimeDelegate.setInstance(new StringBeanRuntimeDelegate(original));
try {
Invocation invocation = buildBuilder(provider).header(name, bean).buildGet();
Response response = invoke(invocation);
String body = response.readEntity(String.class);
assertContains(name.toLowerCase(), body.toLowerCase());
} finally {
RuntimeDelegate.setInstance(original);
StringBeanRuntimeDelegate.assertNotStringBeanRuntimeDelegate();
}
}
use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method getAcceptableMediaTypesIsImmutableTest.
/*
* @testName: getAcceptableMediaTypesIsImmutableTest
*
* @assertion_ids: JAXRS:JAVADOC:429; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
* JAXRS:SPEC:85; JAXRS:JAVADOC:427;
*
* @test_Strategy: Get a list of media types that are acceptable for the
* response. Returns a read-only list of requested response media types sorted
* according to their q-value, with highest preference first.
*
* ClientRequestFilter.abortWith
*/
@Test
public void getAcceptableMediaTypesIsImmutableTest() throws Fault {
ContextProvider provider = new ContextProvider() {
@Override
protected void checkFilterContext(ClientRequestContext context) throws Fault {
List<MediaType> types = context.getAcceptableMediaTypes();
try {
types.add(MediaType.APPLICATION_JSON_TYPE);
} catch (Exception e) {
// either exception is thrown or add does nothing
}
types = context.getAcceptableMediaTypes();
boolean b = types.contains(MediaType.APPLICATION_JSON_TYPE);
assertTrue(!b, "getAcceptableMediaTypes is not read only");
Response r = Response.ok().build();
context.abortWith(r);
}
};
String media = "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5";
Invocation.Builder builder = buildBuilder(provider);
Invocation invocation = builder.header("Accept", media).buildGet();
Response response = invoke(invocation);
assertStatus(response, Status.OK);
}
use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method setCorruptedStream.
protected AtomicInteger setCorruptedStream() {
final AtomicInteger ai = new AtomicInteger(0);
ClientResponseFilter filter = new ClientResponseFilter() {
@Override
public void filter(ClientRequestContext arg0, ClientResponseContext response) throws IOException {
CorruptedInputStream cis = new CorruptedInputStream(ResponseTest.ENTITY.getBytes(), ai);
cis.setCorrupted(true);
response.setEntityStream(cis);
}
};
addProvider(filter);
// do not use new entity stream in logging filter for the case of priority
// disfunction, the CorruptedInputStream would be then replaced, wrongly
// informing about not closing the stream
super.setPrintEntity(false);
return ai;
}
Aggregations