@Test @SuppressWarnings("unchecked") public void resolveAsyncAttributes() { TestBean testBean1 = new TestBean("Bean1"); TestBean testBean2 = new TestBean("Bean2"); Map<String, Object> attributes = new HashMap<>(); attributes.put("attr1", Mono.just(testBean1)); attributes.put("attr2", Flux.just(testBean1, testBean2)); attributes.put("attr3", Single.just(testBean2)); attributes.put("attr4", Observable.just(testBean1, testBean2)); attributes.put("attr5", Mono.empty()); TestView view = new TestView(); StepVerifier.create( view.render(attributes, null, this.exchange)).verifyComplete(); Map<String, Object> actual = view.attributes; assertEquals(testBean1, actual.get("attr1")); assertArrayEquals(new TestBean[] { testBean1, testBean2 }, ((List<TestBean>) actual.get("attr2")).toArray()); assertEquals(testBean2, actual.get("attr3")); assertArrayEquals(new TestBean[] { testBean1, testBean2 }, ((List<TestBean>) actual.get("attr4")).toArray()); assertNull(actual.get("attr5")); }
@Test public void defaultContentType() { Mono<RenderingResponse> result = RenderingResponse.create("view").build(); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost")); TestView view = new TestView(); ViewResolver viewResolver = mock(ViewResolver.class); when(viewResolver.resolveViewName(any(), any())).thenReturn(Mono.just(view)); List<ViewResolver> viewResolvers = new ArrayList<>(); viewResolvers.add(viewResolver); ServerResponse.Context context = mock(ServerResponse.Context.class); when(context.viewResolvers()).thenReturn(viewResolvers); StepVerifier.create(result.flatMap(response -> response.writeTo(exchange, context))) .verifyComplete(); assertEquals(ViewResolverSupport.DEFAULT_CONTENT_TYPE, exchange.getResponse().getHeaders().getContentType()); }
private void testTokenize(List<String> source, List<String> expected, boolean tokenizeArrayElements) { Flux<TokenBuffer> tokenBufferFlux = Jackson2Tokenizer.tokenize( Flux.fromIterable(source).map(this::stringBuffer), this.jsonFactory, tokenizeArrayElements); Flux<String> result = tokenBufferFlux .map(tokenBuffer -> { try { TreeNode root = this.objectMapper.readTree(tokenBuffer.asParser()); return this.objectMapper.writeValueAsString(root); } catch (IOException ex) { throw new UncheckedIOException(ex); } }); StepVerifier.FirstStep<String> builder = StepVerifier.create(result); expected.forEach(s -> builder.assertNext(new JSONAssertConsumer(s))); builder.verifyComplete(); }
@Test public void loadAuthorizationRequestWhenNoStateParamThenEmpty() { this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); Mono<OAuth2AuthorizationRequest> saveAndLoad = this.repository.saveAuthorizationRequest(this.authorizationRequest, this.exchange) .then(this.repository.loadAuthorizationRequest(this.exchange)); StepVerifier.create(saveAndLoad) .verifyComplete(); }
@Test public void removeAuthorizedClientWhenClientIdThenNoException() { when(this.clientRegistrationRepository.findByRegistrationId(this.clientRegistrationId)).thenReturn(Mono.empty()); OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.clientRegistration, this.principalName, this.accessToken); Mono<Void> saveAndDeleteAndLoad = this.authorizedClientService.saveAuthorizedClient(authorizedClient, this.principal) .then(this.authorizedClientService.removeAuthorizedClient(this.clientRegistrationId, this.principalName)); StepVerifier.create(saveAndDeleteAndLoad) .verifyComplete(); }
@Test public void loadAuthorizedClientWhenClientRegistrationFoundAndNotAuthorizedClientThenEmpty() { when(this.clientRegistrationRepository.findByRegistrationId(this.clientRegistrationId)).thenReturn(Mono.just(this.clientRegistration)); StepVerifier .create(this.authorizedClientService.loadAuthorizedClient(this.clientRegistrationId, this.principalName)) .verifyComplete(); }
@Test public void setContextAndClearAndGetContextThenEmitsEmpty() { SecurityContext expectedContext = new SecurityContextImpl( new TestingAuthenticationToken("user", "password", "ROLE_USER")); Mono<SecurityContext> context = Mono.subscriberContext() .flatMap( c -> ReactiveSecurityContextHolder.getContext()) .subscriberContext(ReactiveSecurityContextHolder.clearContext()) .subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext))); StepVerifier.create(context) .verifyComplete(); }
@Test public void getContextWhenEmpty() { Mono<SecurityContext> context = ReactiveSecurityContextHolder.getContext(); StepVerifier.create(context) .verifyComplete(); }
@Test public void removeAuthorizedClientWhenClientRegistrationFoundRemovedThenNotFound() { when(this.clientRegistrationRepository.findByRegistrationId(this.clientRegistrationId)).thenReturn(Mono.just(this.clientRegistration)); OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.clientRegistration, this.principalName, this.accessToken); Mono<OAuth2AuthorizedClient> saveAndDeleteAndLoad = this.authorizedClientService.saveAuthorizedClient(authorizedClient, this.principal) .then(this.authorizedClientService.removeAuthorizedClient(this.clientRegistrationId, this.principalName)) .then(this.authorizedClientService.loadAuthorizedClient(this.clientRegistrationId, this.principalName)); StepVerifier.create(saveAndDeleteAndLoad) .verifyComplete(); } }
@Test public void findByRegistrationIdWhenNotValidIdThenEmpty() { StepVerifier.create(this.repository.findByRegistrationId(this.registration.getRegistrationId() + "invalid")) .verifyComplete(); }
@Test public void resources() { Resource resource = new ClassPathResource("/org/springframework/web/reactive/function/server/"); assertTrue(resource.exists()); RouterFunction<ServerResponse> route = RouterFunctions.route() .resources("/resources/**", resource) .build(); MockServerRequest resourceRequest = MockServerRequest.builder(). method(HttpMethod.GET). uri(URI.create("http://localhost/resources/response.txt")) .build(); Mono<Integer> responseMono = route.route(resourceRequest) .flatMap(handlerFunction -> handlerFunction.handle(resourceRequest)) .map(ServerResponse::statusCode) .map(HttpStatus::value); StepVerifier.create(responseMono) .expectNext(200) .verifyComplete(); MockServerRequest invalidRequest = MockServerRequest.builder(). method(HttpMethod.POST). uri(URI.create("http://localhost/resources/foo.txt")) .build(); responseMono = route.route(invalidRequest) .flatMap(handlerFunction -> handlerFunction.handle(invalidRequest)) .map(ServerResponse::statusCode) .map(HttpStatus::value); StepVerifier.create(responseMono) .verifyComplete(); }
/** * Test a {@link Encoder#encode encode} scenario where the input stream is empty. * The output is expected to be empty as well. * * @param inputType the input type * @param mimeType the mime type to use for decoding. May be {@code null}. * @param hints the hints used for decoding. May be {@code null}. */ protected void testEncodeEmpty(ResolvableType inputType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { Flux<?> input = Flux.empty(); Flux<DataBuffer> result = encoder().encode(input, this.bufferFactory, inputType, mimeType, hints); StepVerifier.create(result) .verifyComplete(); }
/** * Test a {@link Decoder#decodeToMono decode} scenario where the input stream is empty. * The output is expected to be empty as well. * * @param outputType the desired output type * @param mimeType the mime type to use for decoding. May be {@code null}. * @param hints the hints used for decoding. May be {@code null}. */ protected void testDecodeToMonoEmpty(ResolvableType outputType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { Flux<DataBuffer> input = Flux.empty(); Mono<?> result = this.decoder.decodeToMono(input, outputType, mimeType, hints); StepVerifier.create(result) .verifyComplete(); }
/** * Test a {@link Decoder#decode decode} scenario where the input stream is empty. * The output is expected to be empty as well. * * @param outputType the desired output type * @param mimeType the mime type to use for decoding. May be {@code null}. * @param hints the hints used for decoding. May be {@code null}. */ protected void testDecodeEmpty(ResolvableType outputType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { Flux<DataBuffer> input = Flux.empty(); Flux<?> result = this.decoder.decode(input, outputType, mimeType, hints); StepVerifier.create(result) .verifyComplete(); }
@Test public void personNotRequired() { MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Person.class); ServerWebExchange exchange = createExchange(new MultipartBodyBuilder()); Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange); StepVerifier.create(result).verifyComplete(); }
@Test public void decodeEmptyArrayToFlux() { Flux<DataBuffer> input = Flux.from(stringBuffer("[]")); testDecode(input, Pojo.class, step -> step.verifyComplete()); }
@Test public void defaultInstance() { Mono<Msg> result = this.webClient.get() .uri("/default-instance") .retrieve() .bodyToMono(Msg.class); StepVerifier.create(result) .verifyComplete(); }
@Test public void empty() { Mono<Msg> result = this.webClient.get() .uri("/empty") .retrieve() .bodyToMono(Msg.class); StepVerifier.create(result) .verifyComplete(); }
@Test public void partNotRequired() { MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Part.class); ServerWebExchange exchange = createExchange(new MultipartBodyBuilder()); Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange); StepVerifier.create(result).verifyComplete(); }
private <T> void testWrite( Publisher<T> source, MediaType mediaType, MockServerHttpResponse response, Class<T> clazz) { Mono<Void> result = this.messageWriter.write(source, forClass(clazz), mediaType, response, HINTS); StepVerifier.create(result) .verifyComplete(); }