|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2019 the original author or authors. |
|
|
|
* Copyright 2002-2022 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
@ -34,7 +34,6 @@ import org.springframework.web.bind.annotation.RestController; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
import static org.hamcrest.Matchers.containsString; |
|
|
|
import static org.hamcrest.Matchers.containsString; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Samples of tests using {@link WebTestClient} with serialized JSON content. |
|
|
|
* Samples of tests using {@link WebTestClient} with serialized JSON content. |
|
|
|
* |
|
|
|
* |
|
|
@ -48,31 +47,41 @@ public class JsonContentTests { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void jsonContent() { |
|
|
|
public void jsonContentWithDefaultLenientMode() { |
|
|
|
this.client.get().uri("/persons/extended") |
|
|
|
this.client.get().uri("/persons") |
|
|
|
.accept(MediaType.APPLICATION_JSON) |
|
|
|
.accept(MediaType.APPLICATION_JSON) |
|
|
|
.exchange() |
|
|
|
.exchange() |
|
|
|
.expectStatus().isOk() |
|
|
|
.expectStatus().isOk() |
|
|
|
.expectBody().json("[{\"name\":\"Jane\"},{\"name\":\"Jason\"},{\"name\":\"John\"}]"); |
|
|
|
.expectBody().json( |
|
|
|
|
|
|
|
"[{\"firstName\":\"Jane\"}," + |
|
|
|
|
|
|
|
"{\"firstName\":\"Jason\"}," + |
|
|
|
|
|
|
|
"{\"firstName\":\"John\"}]"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void jsonContentStrictFail() { |
|
|
|
public void jsonContentWithStrictMode() { |
|
|
|
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.client.get().uri("/persons/extended") |
|
|
|
this.client.get().uri("/persons") |
|
|
|
.accept(MediaType.APPLICATION_JSON) |
|
|
|
.accept(MediaType.APPLICATION_JSON) |
|
|
|
.exchange() |
|
|
|
.exchange() |
|
|
|
.expectStatus().isOk() |
|
|
|
.expectStatus().isOk() |
|
|
|
.expectBody().json("[{\"name\":\"Jane\"},{\"name\":\"Jason\"},{\"name\":\"John\"}]", true) |
|
|
|
.expectBody().json( |
|
|
|
); |
|
|
|
"[{\"firstName\":\"Jane\",\"lastName\":\"Williams\"}," + |
|
|
|
|
|
|
|
"{\"firstName\":\"Jason\",\"lastName\":\"Johnson\"}," + |
|
|
|
|
|
|
|
"{\"firstName\":\"John\",\"lastName\":\"Smith\"}]", |
|
|
|
|
|
|
|
true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void jsonContentStrict() { |
|
|
|
public void jsonContentWithStrictModeAndMissingAttributes() { |
|
|
|
this.client.get().uri("/persons/extended") |
|
|
|
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.client.get().uri("/persons") |
|
|
|
.accept(MediaType.APPLICATION_JSON) |
|
|
|
.accept(MediaType.APPLICATION_JSON) |
|
|
|
.exchange() |
|
|
|
.exchange() |
|
|
|
.expectStatus().isOk() |
|
|
|
.expectBody().json( |
|
|
|
.expectBody().json("[{\"name\":\"Jane\",\"surname\":\"Williams\"},{\"name\":\"Jason\",\"surname\":\"Johnson\"},{\"name\":\"John\",\"surname\":\"Smith\"}]", true); |
|
|
|
"[{\"firstName\":\"Jane\"}," + |
|
|
|
|
|
|
|
"{\"firstName\":\"Jason\"}," + |
|
|
|
|
|
|
|
"{\"firstName\":\"John\"}]", |
|
|
|
|
|
|
|
true) |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
@ -82,26 +91,26 @@ public class JsonContentTests { |
|
|
|
.exchange() |
|
|
|
.exchange() |
|
|
|
.expectStatus().isOk() |
|
|
|
.expectStatus().isOk() |
|
|
|
.expectBody() |
|
|
|
.expectBody() |
|
|
|
.jsonPath("$[0].name").isEqualTo("Jane") |
|
|
|
.jsonPath("$[0].firstName").isEqualTo("Jane") |
|
|
|
.jsonPath("$[1].name").isEqualTo("Jason") |
|
|
|
.jsonPath("$[1].firstName").isEqualTo("Jason") |
|
|
|
.jsonPath("$[2].name").isEqualTo("John"); |
|
|
|
.jsonPath("$[2].firstName").isEqualTo("John"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void jsonPathMatches() { |
|
|
|
public void jsonPathMatches() { |
|
|
|
this.client.get().uri("/persons/John") |
|
|
|
this.client.get().uri("/persons/John/Smith") |
|
|
|
.accept(MediaType.APPLICATION_JSON) |
|
|
|
.accept(MediaType.APPLICATION_JSON) |
|
|
|
.exchange() |
|
|
|
.exchange() |
|
|
|
.expectStatus().isOk() |
|
|
|
.expectStatus().isOk() |
|
|
|
.expectBody() |
|
|
|
.expectBody() |
|
|
|
.jsonPath("$.name").value(containsString("oh")); |
|
|
|
.jsonPath("$.firstName").value(containsString("oh")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void postJsonContent() { |
|
|
|
public void postJsonContent() { |
|
|
|
this.client.post().uri("/persons") |
|
|
|
this.client.post().uri("/persons") |
|
|
|
.contentType(MediaType.APPLICATION_JSON) |
|
|
|
.contentType(MediaType.APPLICATION_JSON) |
|
|
|
.bodyValue("{\"name\":\"John\"}") |
|
|
|
.bodyValue("{\"firstName\":\"John\",\"lastName\":\"Smith\"}") |
|
|
|
.exchange() |
|
|
|
.exchange() |
|
|
|
.expectStatus().isCreated() |
|
|
|
.expectStatus().isCreated() |
|
|
|
.expectBody().isEmpty(); |
|
|
|
.expectBody().isEmpty(); |
|
|
@ -114,40 +123,38 @@ public class JsonContentTests { |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping |
|
|
|
@GetMapping |
|
|
|
Flux<Person> getPersons() { |
|
|
|
Flux<Person> getPersons() { |
|
|
|
return Flux.just(new Person("Jane"), new Person("Jason"), new Person("John")); |
|
|
|
return Flux.just(new Person("Jane", "Williams"), new Person("Jason", "Johnson"), new Person("John", "Smith")); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/extended") |
|
|
|
|
|
|
|
Flux<ExtendedPerson> getExtendedPersons() { |
|
|
|
|
|
|
|
return Flux.just(new ExtendedPerson("Jane", "Williams"), new ExtendedPerson("Jason", "Johnson"), new ExtendedPerson("John", "Smith")); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/{name}") |
|
|
|
@GetMapping("/{firstName}/{lastName}") |
|
|
|
Person getPerson(@PathVariable String name) { |
|
|
|
Person getPerson(@PathVariable String firstName, @PathVariable String lastName) { |
|
|
|
return new Person(name); |
|
|
|
return new Person(firstName, lastName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PostMapping |
|
|
|
@PostMapping |
|
|
|
ResponseEntity<String> savePerson(@RequestBody Person person) { |
|
|
|
ResponseEntity<String> savePerson(@RequestBody Person person) { |
|
|
|
return ResponseEntity.created(URI.create("/persons/" + person.getName())).build(); |
|
|
|
return ResponseEntity.created(URI.create(String.format("/persons/%s/%s", person.getFirstName(), person.getLastName()))).build(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static class ExtendedPerson { |
|
|
|
static class Person { |
|
|
|
private String name; |
|
|
|
private String firstName; |
|
|
|
private String surname; |
|
|
|
private String lastName; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Person() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public ExtendedPerson(String name, String surname) { |
|
|
|
public Person(String firstName, String lastName) { |
|
|
|
this.name = name; |
|
|
|
this.firstName = firstName; |
|
|
|
this.surname = surname; |
|
|
|
this.lastName = lastName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getName() { |
|
|
|
public String getFirstName() { |
|
|
|
return name; |
|
|
|
return this.firstName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getSurname() { |
|
|
|
public String getLastName() { |
|
|
|
return surname; |
|
|
|
return this.lastName; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|