Java Code Examples for org.springframework.web.server.ServerWebInputException

The following examples show how to use org.springframework.web.server.ServerWebInputException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: DefaultServerRequestTests.java    License: MIT License 6 votes vote down vote up
@Test
public void bodyToMonoDecodingException() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("{\"invalid\":\"json\" ".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.setContentType(MediaType.APPLICATION_JSON);
	MockServerHttpRequest mockRequest = MockServerHttpRequest
			.method(HttpMethod.POST, "https://example.com/invalid")
			.headers(httpHeaders)
			.body(body);
	DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), messageReaders);

	Mono<Map<String, String>> resultMono = request.bodyToMono(
			new ParameterizedTypeReference<Map<String, String>>() {});
	StepVerifier.create(resultMono)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 2
@Test
public void emptyBodyWithSingle() {
	MethodParameter param = this.testMethod.annot(requestBody()).arg(Single.class, String.class);
	Single<String> single = resolveValueWithEmptyBody(param);
	StepVerifier.create(RxReactiveStreams.toPublisher(single))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Single.class, String.class);
	single = resolveValueWithEmptyBody(param);
	StepVerifier.create(RxReactiveStreams.toPublisher(single))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 3
@Test
public void emptyBodyWithMaybe() {
	MethodParameter param = this.testMethod.annot(requestBody()).arg(Maybe.class, String.class);
	Maybe<String> maybe = resolveValueWithEmptyBody(param);
	StepVerifier.create(maybe.toFlowable())
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Maybe.class, String.class);
	maybe = resolveValueWithEmptyBody(param);
	StepVerifier.create(maybe.toFlowable())
			.expectNextCount(0)
			.expectComplete()
			.verify();
}
 
Example 4
@Test
public void emptyBodyWithObservable() {
	MethodParameter param = this.testMethod.annot(requestBody()).arg(Observable.class, String.class);
	Observable<String> observable = resolveValueWithEmptyBody(param);
	StepVerifier.create(RxReactiveStreams.toPublisher(observable))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Observable.class, String.class);
	observable = resolveValueWithEmptyBody(param);
	StepVerifier.create(RxReactiveStreams.toPublisher(observable))
			.expectNextCount(0)
			.expectComplete()
			.verify();
}
 
Example 5
Source Project: java-technology-stack   Source File: DefaultServerRequestTests.java    License: MIT License 6 votes vote down vote up
@Test
public void bodyToMonoDecodingException() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("{\"invalid\":\"json\" ".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.setContentType(MediaType.APPLICATION_JSON);
	MockServerHttpRequest mockRequest = MockServerHttpRequest
			.method(HttpMethod.POST, "http://example.com/invalid")
			.headers(httpHeaders)
			.body(body);
	DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), messageReaders);

	Mono<Map<String, String>> resultMono = request.bodyToMono(
			new ParameterizedTypeReference<Map<String, String>>() {});
	StepVerifier.create(resultMono)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 6
@Test
@SuppressWarnings("unchecked")
public void emptyBodyWithMono() throws Exception {

	MethodParameter param = this.testMethod.annot(requestBody()).arg(Mono.class, String.class);
	StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Mono.class, String.class);
	StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param))
			.expectNextCount(0)
			.expectComplete()
			.verify();
}
 
Example 7
@Test
@SuppressWarnings("unchecked")
public void emptyBodyWithFlux() throws Exception {

	MethodParameter param = this.testMethod.annot(requestBody()).arg(Flux.class, String.class);
	StepVerifier.create((Flux<Void>) resolveValueWithEmptyBody(param))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Flux.class, String.class);
	StepVerifier.create((Flux<Void>) resolveValueWithEmptyBody(param))
			.expectNextCount(0)
			.expectComplete()
			.verify();
}
 
Example 8
@Test
public void emptyBodyWithSingle() throws Exception {

	MethodParameter param = this.testMethod.annot(requestBody()).arg(Single.class, String.class);
	Single<String> single = resolveValueWithEmptyBody(param);
	StepVerifier.create(RxReactiveStreams.toPublisher(single))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Single.class, String.class);
	single = resolveValueWithEmptyBody(param);
	StepVerifier.create(RxReactiveStreams.toPublisher(single))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 9
@Test
public void emptyBodyWithMaybe() throws Exception {

	MethodParameter param = this.testMethod.annot(requestBody()).arg(Maybe.class, String.class);
	Maybe<String> maybe = resolveValueWithEmptyBody(param);
	StepVerifier.create(maybe.toFlowable())
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Maybe.class, String.class);
	maybe = resolveValueWithEmptyBody(param);
	StepVerifier.create(maybe.toFlowable())
			.expectNextCount(0)
			.expectComplete()
			.verify();
}
 
Example 10
@Test
public void emptyBodyWithObservable() throws Exception {

	MethodParameter param = this.testMethod.annot(requestBody()).arg(Observable.class, String.class);
	Observable<String> observable = resolveValueWithEmptyBody(param);
	StepVerifier.create(RxReactiveStreams.toPublisher(observable))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Observable.class, String.class);
	observable = resolveValueWithEmptyBody(param);
	StepVerifier.create(RxReactiveStreams.toPublisher(observable))
			.expectNextCount(0)
			.expectComplete()
			.verify();
}
 
Example 11
@Test @SuppressWarnings("unchecked") // SPR-9942
public void emptyBody() throws Exception {
	MockServerHttpRequest request = post("/path").contentType(MediaType.APPLICATION_JSON).build();
	ServerWebExchange exchange = MockServerWebExchange.from(request);
	ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
	MethodParameter param = this.testMethod.arg(type);
	Mono<TestBean> result = (Mono<TestBean>) this.resolver.readBody(
			param, true, this.bindingContext, exchange).block();

	StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
 
Example 12
@Test
@SuppressWarnings("unchecked")
public void validateMonoTestBean() throws Exception {
	String body = "{\"bar\":\"b1\"}";
	ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
	MethodParameter param = this.testMethod.arg(type);
	Mono<TestBean> mono = resolveValue(param, body);

	StepVerifier.create(mono).expectNextCount(0).expectError(ServerWebInputException.class).verify();
}
 
Example 13
@Test
@SuppressWarnings("unchecked")
public void validateFluxTestBean() throws Exception {
	String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\"}]";
	ResolvableType type = forClassWithGenerics(Flux.class, TestBean.class);
	MethodParameter param = this.testMethod.arg(type);
	Flux<TestBean> flux = resolveValue(param, body);

	StepVerifier.create(flux)
			.expectNext(new TestBean("f1", "b1"))
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 14
@Test
public void emptyBodyWithSingle() {
	ResolvableType type = httpEntityType(Single.class, String.class);
	HttpEntity<Single<String>> entity = resolveValueWithEmptyBody(type);

	StepVerifier.create(RxReactiveStreams.toPublisher(entity.getBody()))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 15
@Test
public void emptyBodyWithRxJava2Single() {
	ResolvableType type = httpEntityType(io.reactivex.Single.class, String.class);
	HttpEntity<io.reactivex.Single<String>> entity = resolveValueWithEmptyBody(type);

	StepVerifier.create(entity.getBody().toFlowable())
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 16
@Test
public void resolve() {
	MethodParameter param = this.testMethod.annot(requestAttribute().noName()).arg(Foo.class);
	Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
	StepVerifier.create(mono)
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	Foo foo = new Foo();
	this.exchange.getAttributes().put("foo", foo);
	mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
	assertSame(foo, mono.block());
}
 
Example 17
@Test
public void resolve() {
	MethodParameter param = initMethodParameter(0);
	Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
	StepVerifier.create(mono).expectError(ServerWebInputException.class).verify();

	Foo foo = new Foo();
	given(this.session.getAttribute("foo")).willReturn(foo);
	mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
	assertSame(foo, mono.block());
}
 
Example 18
@Test
@SuppressWarnings("unchecked")
public void emptyBodyWithMono() {
	MethodParameter param = this.testMethod.annot(requestBody()).arg(Mono.class, String.class);
	StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Mono.class, String.class);
	StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param))
			.expectNextCount(0)
			.expectComplete()
			.verify();
}
 
Example 19
@Test
@SuppressWarnings("unchecked")
public void emptyBodyWithFlux() {
	MethodParameter param = this.testMethod.annot(requestBody()).arg(Flux.class, String.class);
	StepVerifier.create((Flux<Void>) resolveValueWithEmptyBody(param))
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	param = this.testMethod.annot(requestBody().notRequired()).arg(Flux.class, String.class);
	StepVerifier.create((Flux<Void>) resolveValueWithEmptyBody(param))
			.expectNextCount(0)
			.expectComplete()
			.verify();
}
 
Example 20
@Test
public void missingRequestParam() {

	MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"));
	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
	Mono<Object> mono = this.resolver.resolveArgument(param, this.bindContext, exchange);

	StepVerifier.create(mono)
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 21
@Test
public void personRequired() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Person.class);
	ServerWebExchange exchange = createExchange(new MultipartBodyBuilder());
	Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);

	StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
 
Example 22
@Test
public void partRequired() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Part.class);
	ServerWebExchange exchange = createExchange(new MultipartBodyBuilder());
	Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);

	StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
 
Example 23
@Test
public void notFound() {
	MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"));
	Mono<Object> mono = resolver.resolveArgument(this.cookieParameter, this.bindingContext, exchange);
	StepVerifier.create(mono)
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 24
@Test
public void notFound() {
	Mono<Object> mono = resolver.resolveArgument(
			this.paramNamedValueStringArray, this.bindingContext,
			MockServerWebExchange.from(MockServerHttpRequest.get("/")));

	StepVerifier.create(mono)
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 25
@Test  // SPR-12854
public void getHandlerTestRequestParamMismatch() {
	ServerWebExchange exchange = MockServerWebExchange.from(get("/params"));
	Mono<Object> mono = this.handlerMapping.getHandler(exchange);
	assertError(mono, ServerWebInputException.class, ex -> {
		assertThat(ex.getReason(), containsString("[foo=bar]"));
		assertThat(ex.getReason(), containsString("[bar=baz]"));
	});
}
 
Example 26
Source Project: staccato   Source File: ServerWebInputExceptionHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected String getMessage(ServerWebInputException ex) {

    Throwable t = ex.getCause();
    if (t != null && t.getCause() != null && t.getCause() instanceof UnrecognizedPropertyException) {
        String propertyName = ((UnrecognizedPropertyException) t.getCause()).getPropertyName();
        Collection knownFields = ((UnrecognizedPropertyException) t.getCause()).getKnownPropertyIds();
        return InvalidParameterException.buildMessage(propertyName, knownFields);
    }

    return ex.getMessage();
}
 
Example 27
@Test @SuppressWarnings("unchecked") // SPR-9942
public void emptyBody() throws Exception {
	MockServerHttpRequest request = post("/path").contentType(MediaType.APPLICATION_JSON).build();
	ServerWebExchange exchange = MockServerWebExchange.from(request);
	ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
	MethodParameter param = this.testMethod.arg(type);
	Mono<TestBean> result = (Mono<TestBean>) this.resolver.readBody(
			param, true, this.bindingContext, exchange).block();

	StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
 
Example 28
@Test
@SuppressWarnings("unchecked")
public void validateMonoTestBean() throws Exception {
	String body = "{\"bar\":\"b1\"}";
	ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
	MethodParameter param = this.testMethod.arg(type);
	Mono<TestBean> mono = resolveValue(param, body);

	StepVerifier.create(mono).expectNextCount(0).expectError(ServerWebInputException.class).verify();
}
 
Example 29
@Test
@SuppressWarnings("unchecked")
public void validateFluxTestBean() throws Exception {
	String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\"}]";
	ResolvableType type = forClassWithGenerics(Flux.class, TestBean.class);
	MethodParameter param = this.testMethod.arg(type);
	Flux<TestBean> flux = resolveValue(param, body);

	StepVerifier.create(flux)
			.expectNext(new TestBean("f1", "b1"))
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example 30
@Test
public void resolve() throws Exception {
	MethodParameter param = this.testMethod.annot(requestAttribute().noName()).arg(Foo.class);
	Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
	StepVerifier.create(mono)
			.expectNextCount(0)
			.expectError(ServerWebInputException.class)
			.verify();

	Foo foo = new Foo();
	this.exchange.getAttributes().put("foo", foo);
	mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
	assertSame(foo, mono.block());
}