Search in sources :

Example 51 with Context

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");
}
Also used : Context(reactor.util.context.Context) Test(org.junit.Test)

Example 52 with Context

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");
}
Also used : Context(reactor.util.context.Context) ThrowableAssertAlternative(org.assertj.core.api.ThrowableAssertAlternative) Step(reactor.test.StepVerifier.Step) Context(reactor.util.context.Context) HashMap(java.util.HashMap) Test(org.junit.Test) DefaultContextExpectations(reactor.test.DefaultStepVerifierBuilder.DefaultContextExpectations) Function(java.util.function.Function) Objects(java.util.Objects) Flux(reactor.core.publisher.Flux) ContextExpectations(reactor.test.StepVerifier.ContextExpectations) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) Collections(java.util.Collections) Test(org.junit.Test)

Example 53 with Context

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;
    }));
}
Also used : UsernameThreadContext(de.spring.example.context.UsernameThreadContext) WebFilter(org.springframework.web.server.WebFilter) MDC(org.slf4j.MDC) Context(reactor.util.context.Context) Mono(reactor.core.publisher.Mono) UsernameContext(de.spring.example.context.UsernameContext) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilterChain(org.springframework.web.server.WebFilterChain) UsernameThreadContext(de.spring.example.context.UsernameThreadContext) Context(reactor.util.context.Context) UsernameContext(de.spring.example.context.UsernameContext) UsernameContext(de.spring.example.context.UsernameContext) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 54 with Context

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);
}
Also used : Context(reactor.util.context.Context) SpringTestContext(org.springframework.security.config.test.SpringTestContext) BaseSubscriber(reactor.core.publisher.BaseSubscriber) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Test(org.junit.jupiter.api.Test)

Example 55 with Context

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();
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) Context(reactor.util.context.Context) OAuth2AuthorizationContext(org.springframework.security.oauth2.client.OAuth2AuthorizationContext) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HashMap(java.util.HashMap) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) BodyInserter(org.springframework.web.reactive.function.BodyInserter) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter)

Aggregations

Context (reactor.util.context.Context)66 Test (org.junit.jupiter.api.Test)29 Test (org.junit.Test)28 Mono (reactor.core.publisher.Mono)25 Authentication (org.springframework.security.core.Authentication)23 SecurityContext (org.springframework.security.core.context.SecurityContext)23 MethodParameter (org.springframework.core.MethodParameter)17 CurrentSecurityContext (org.springframework.security.core.annotation.CurrentSecurityContext)17 BindingContext (org.springframework.web.reactive.BindingContext)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 ArrayList (java.util.ArrayList)13 Consumer (java.util.function.Consumer)13 List (java.util.List)11 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)9 Collections (java.util.Collections)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Nullable (javax.annotation.Nullable)8 Flux (reactor.core.publisher.Flux)8 StepVerifier (reactor.test.StepVerifier)8