Search code examples
spring-bootgroovyspock

Adding body to Http request with Spock


I'm developing a Spring Boot application and I'm trying to do some authorization/authentication testing using Spock and groovyx.net.http.RESTClient. I'm trying to pass username and password inside body block like this:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class AuthorizationTest extends Specification {

    @Shared
    def client = new RESTClient("http://localhost:8080")

    def "something should happen"() {
        when:
        def response = client.post(
                path: "/login",
                body: [ password : "1234", username : "admin"],
                requestContentType: ContentType.JSON
        )

        then:
        response.status == 200
    } 

Sadly, something's not working, and when I'm debugging I don't see the two parameters (username and password) inside the request. What am I doing wrong?


Solution

  • It turned out I need to use different encoding, requestContentType: ContentType.URLENC, which is of type application/x-www-form-urlencoded.