测试 REST API
最后,我们将测试我们的REST API。
首先,一个简单的设置:
private final String ROOT_URI = "http://localhost:8080";
private FormAuthConfig formConfig = new FormAuthConfig("/login", "username", "password");
@Before public void setup() {
RestAssured.config = config().redirect(RedirectConfig.redirectConfig().followRedirects(false)); }
接下来,让我们得到所有books:
@Test
public void whenGetAllBooks_thenSuccess() {
Response response = RestAssured.get(ROOT_URI + "/book-service/books");
Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode());
Assert.assertNotNull(response.getBody());
}
然后,尝试不登录来访问受保护的资源:
@Test
public void whenAccessProtectedResourceWithoutLogin_thenRedirectToLogin() {
Response response = RestAssured.get(ROOT_URI + "/book-service/books/1");
Assert.assertEquals(HttpStatus.FOUND.value(), response.getStatusCode());
Assert.assertEquals("http://localhost:8080/login",response.getHeader("Location"));
}
然后,登录并创建新的book对象:
@Test
public void whenAddNewBook_thenSuccess() {
Book book = new Book("Baeldung", "How to spring cloud");
Response bookResponse = RestAssured.given().auth()
.form("admin", "admin", formConfig).and()
.body(book)
Book result = bookResponse.as(Book.class);
Assert.assertEquals(HttpStatus.OK.value(), bookResponse.getStatusCode());
Assert.assertEquals(book.getAuthor(), result.getAuthor());
}
然后在身份验证后访问受保护的资源:
@Test
public void whenAccessProtectedResourceAfterLogin_thenSuccess() {
Response response = RestAssured.given().auth()
.form("user", "password", formConfig)
.get(ROOT_URI + "/book-service/books/1");
Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode());
}
现在,我们将创建新的Rating对象:
@Test
public void whenAddNewRating_thenSuccess() {
Rating rating = new Rating(1L, 4);
Response ratingResponse = RestAssured.given().auth()
.form("admin", "admin", formConfig).and()
.contentType(ContentType.JSON)
.body(rating)
Rating result = ratingResponse.as(Rating.class);
Assert.assertEquals(HttpStatus.OK.value(), ratingResponse.getStatusCode());
Assert.assertEquals(rating.getBookId(), result.getBookId());
}
现在尝试访问管理员保护的Rating资源:
@Test
public void whenAccessAdminProtectedResource_thenForbidden() {
Response response = RestAssured.given().auth()
.form("user", "password", formConfig)
.get(ROOT_URI + "/rating-service/ratings");
}
通过使用admin登录访问受保护的Rating
@Test
public void whenAdminAccessProtectedResource_thenSuccess() {
Response response = RestAssured.given().auth()
.form("admin", "admin", formConfig)
.get(ROOT_URI + "/rating-service/ratings");
Assert.assertEquals(HttpStatus.OK.value(),
response.getStatusCode());
Assert.assertNotNull(response.getBody());
}
最后,以管理员身份访问discovery资源:
@Test
public void whenAdminAccessDiscoveryResource_thenSuccess() {
Response response = RestAssured.given().auth()
.form("admin", "admin", formConfig)
.get(ROOT_URI + "/discovery");
Assert.assertEquals(HttpStatus.OK.value(),response.getStatusCode()); }
运行
- 首先,在您的HOME目录中创建一个本地Git存储库application-config。
- 添加配置属性文件。
- 确保在运行服务之前提交本地Git存储库中的所有更改。
git add -A
git commit -m "the initial configuration"
- 确保按以下顺序运行模块:
- 在端口8081运行配置服务
- 接着,在端口8082运行发现服务
- 之后,在端口端口8080运行网关服务
- 最后,分别在端口8083和8084运行资源服务
结论
我们已经完成了 - 使用Spring Boot构建的全功能微服务架构Spring Cloud。
GitHub上提供了所有模块的源代码。
END