use of reactor.util.context.Context in project reactor-core by reactor.
the class SignalTest method errorStateWithContext.
@Test
public void errorStateWithContext() {
Context context = Context.of("foo", "bar");
Signal<Integer> s = Signal.error(e, context);
assertThat(s.getContext().isEmpty()).as("has context").isFalse();
assertThat(s.isOnComplete()).isFalse();
assertThat(s.isOnSubscribe()).isFalse();
assertThat(s.hasError()).isTrue();
assertThat(s.hasValue()).isFalse();
assertThat(s).isEqualTo(Signal.error(e));
assertThat(s).isNotEqualTo(Signal.error(new Exception("test2")));
assertThat(s).isNotEqualTo(Signal.complete());
assertThat(s).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()));
assertThat(s).isNotEqualTo(Signal.next(1));
assertThat(s.hashCode()).isEqualTo(Signal.error(e).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.error(new Exception("test2")).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.complete().hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.next(1).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()).hashCode());
assertThat(Signal.isComplete(s)).isFalse();
assertThat(Signal.isError(s)).isTrue();
assertThat(s.getThrowable()).isEqualTo(e);
assertThat(s.getType()).isEqualTo(SignalType.ON_ERROR);
assertThat(s.toString()).contains("onError");
StepVerifier.create(Flux.<Integer>from(sub -> {
sub.onSubscribe(Operators.emptySubscription());
s.accept(sub);
})).verifyErrorMessage("test");
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class DefaultContextExpectationsTest method notContainsOnlyOfContextSize.
@Test
public void notContainsOnlyOfContextSize() throws Exception {
Context expected = Context.of("foo", "bar", "other", "stuff");
assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar")), e -> e.containsOnly(expected)).withMessage("Expected Context Context1{foo=bar} to contain same values as " + "Context2{foo=bar, other=stuff}, but they differ in size");
}
use of reactor.util.context.Context in project JavaForFun by gumartinm.
the class UsernameFilter method filter.
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
if (!request.getHeaders().containsKey(UsernameThreadContext.USERNAME_HEADER)) {
return chain.filter(exchange);
}
String username = request.getHeaders().get(UsernameThreadContext.USERNAME_HEADER).get(0);
return chain.filter(exchange).compose(function -> function.then(Mono.subscriberContext()).doOnSubscribe(onSubscribe -> {
MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
}).doOnError(throwable -> {
MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
}).onErrorMap(throwable -> {
MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
return throwable;
}).doFinally(onFinally -> {
MDC.remove(UsernameThreadContext.USERNAME_HEADER);
}).flatMap(context -> {
Mono<Void> continuation = Mono.empty();
return continuation;
}).subscriberContext(context -> {
Context updatedContext = context;
if (!context.hasKey(UsernameContext.class)) {
updatedContext = context.put(UsernameContext.class, new UsernameContext(username));
}
return updatedContext;
}));
}
use of reactor.util.context.Context in project spring-security by spring-projects.
the class SecurityReactorContextConfigurationTests method createSubscriberIfNecessaryWhenParentContextContainsSecurityContextAttributesThenUseParentContext.
@Test
public void createSubscriberIfNecessaryWhenParentContextContainsSecurityContextAttributesThenUseParentContext() {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.servletRequest, this.servletResponse));
SecurityContextHolder.getContext().setAuthentication(this.authentication);
Context parentContext = Context.of(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES, new HashMap<>());
BaseSubscriber<Object> parent = new BaseSubscriber<Object>() {
@Override
public Context currentContext() {
return parentContext;
}
};
CoreSubscriber<Object> subscriber = this.subscriberRegistrar.createSubscriberIfNecessary(parent);
Context resultContext = subscriber.currentContext();
assertThat(resultContext).isSameAs(parentContext);
}
use of reactor.util.context.Context in project spring-security by spring-projects.
the class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests method getBody.
private static String getBody(ClientRequest request) {
final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
messageWriters.add(new ResourceHttpMessageWriter());
Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder));
messageWriters.add(new FormHttpMessageWriter());
messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
messageWriters.add(new MultipartHttpMessageWriter(messageWriters));
BodyInserter.Context context = new BodyInserter.Context() {
@Override
public List<HttpMessageWriter<?>> messageWriters() {
return messageWriters;
}
@Override
public Optional<ServerHttpRequest> serverRequest() {
return Optional.empty();
}
@Override
public Map<String, Object> hints() {
return new HashMap<>();
}
};
MockClientHttpRequest body = new MockClientHttpRequest(HttpMethod.GET, "/");
request.body().insert(body, context).block();
return body.getBodyAsString().block();
}
Aggregations