groovy 使用Spock将正文添加到Http请求

hmtdttj4  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(180)

我正在开发一个Sping Boot 应用程序,并尝试使用Spock和groovyx.net.http.RESTClient进行一些授权/身份验证测试。我尝试在body block内部传递用户名和密码,如下所示:

  1. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
  2. class AuthorizationTest extends Specification {
  3. @Shared
  4. def client = new RESTClient("http://localhost:8080")
  5. def "something should happen"() {
  6. when:
  7. def response = client.post(
  8. path: "/login",
  9. body: [ password : "1234", username : "admin"],
  10. requestContentType: ContentType.JSON
  11. )
  12. then:
  13. response.status == 200
  14. }

遗憾的是,有些东西不起作用,当我调试的时候,我没有看到请求中的两个参数(用户名和密码)。

kqqjbcuj

kqqjbcuj1#

结果我需要使用不同的编码,requestContentType: ContentType.URLENC,它的类型是application/x-www-form-urlencoded

相关问题