How to create a consumer test to application/x-www-form-urlencoded body

1,429 views
Skip to first unread message

Chuan Chuan Law

unread,
May 12, 2015, 7:36:17 PM5/12/15
to pact-s...@googlegroups.com

Hi,

 

 

I am using the Groovy version is trying to write a consumer test that can POST a application/x-www-form-urlencoded body, for eg ‘number’ with 123456789.

 

Please advice.

 

 

Thank you very much.

 

 

Cheers,

 

Chuan

Ronald Holshausen

unread,
May 12, 2015, 11:36:10 PM5/12/15
to Chuan Chuan Law, pact-s...@googlegroups.com
Hi Chuan,

You need to set the Content-Type header to application/x-www-form-urlencoded for the interactions in your test. Currently, Pact will do a comparison on the textual contents of the body for anything that is not in JSON format (basically string equality on the whole body). You test code might look something like:

            uponReceiving('a POST in application/x-www-form-urlencoded')
            withAttributes(method: 'post', path: '/path',
                headers: ['Accept': 'text/plain', 'Content-Type': '
application/x-www-form-urlencoded'],
                body: 'number=12345678'
            )
            willRespondWith(status: 201, body: 'form posted ok', headers: ['Content-Type': 'text/plain'])

Could you raise an issue against the pact-jvm project to get matching working for x-www-form-urlencoded encoded bodies?


--
You received this message because you are subscribed to the Google Groups "Pact" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pact-support...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ronald Holshausen

DiUS Computing Pty Ltd

Level 10, 99 Queens Street
Melbourne, VIC 3000

Phone: +61 3 9008 5400
Mobile: +61 413 162 439

http://www.diuscomputing.com.au

Chuan Chuan Law

unread,
May 12, 2015, 11:51:41 PM5/12/15
to pact-s...@googlegroups.com, chuan.c...@gmail.com
Thanks Ronald for the solution.

Yep, I've raised an issue on GitHub as you proposed.


Cheers,

Chuan

Chuan Chuan Law

unread,
May 13, 2015, 12:40:55 AM5/13/15
to pact-s...@googlegroups.com, chuan.c...@gmail.com

In the Consumer test as well, I try to post like this:

def alice_response = client.post(path: '/task', requestContentType: 'application/x-www-form-urlencoded', headers:['Content-Type': 'application/x-www-form-urlencoded'], body: 'number=12345678' )

However, I keep getting a fail during "assert result == PactVerified$.MODULE"

If I change request content-type to 'text/html', assert will pass.

Does that mean that I will not be able to get a pass for this scenario although my response header is in text/html?  

Please advice.

Thanks helps.


Chuan


On Wednesday, 13 May 2015 13:36:10 UTC+10, Ronald Holshausen wrote:

Ronald Holshausen

unread,
May 13, 2015, 6:05:11 AM5/13/15
to pact-s...@googlegroups.com
If you can enable debug logging, you should be able to see the reason why it is not matching in the logs.

Regards,

Ronald

Ronald Holshausen

unread,
May 13, 2015, 7:45:51 PM5/13/15
to pact-s...@googlegroups.com
Hi Chuan,

I've worked out what the problem is. When posting a form that is URL encoded, the body gets interpreted as query parameters. Have a look at https://github.com/DiUS/pact-jvm/blob/master/pact-jvm-consumer-groovy/src/test/groovy/au/com/dius/pact/consumer/groovy/ExampleFormPostTest.groovy for a working example.


On Wednesday, 13 May 2015 14:40:55 UTC+10, Chuan Chuan Law wrote:

Chuan Chuan Law

unread,
May 14, 2015, 12:21:31 AM5/14/15
to pact-s...@googlegroups.com

Thank you so much for the example. The consumer test now passed.

However, the generated PACT file is as below:

"interactions" : [ {
"providerState" : "Scenario of Carfacts REST asynchronous web service in LiveMarket for any internal requestor from within Carsales network",
"description" : "a POST request",
"request" : {
"method" : "POST",
"path" : "/task",
"headers" : {
"Content-Type" : "application/x-www-form-urlencoded"
},
"query" : "vin=1GJ2GYDG2A1827206"
},


But the Provider expects is "body" instead of "query" though.


Please advice.


Thanks heaps.



Cheers,

Chuan

Ronald Holshausen

unread,
May 14, 2015, 7:13:03 PM5/14/15
to Chuan Chuan Law, pact-s...@googlegroups.com
Looks like we need to change the provider verification to post the query in the body if it is a URL encoded form submission.

Chuan Chuan Law

unread,
May 14, 2015, 7:44:19 PM5/14/15
to pact-s...@googlegroups.com

Sorry, I don't really understand the suggested solution below:

Do you mind to give an example please?


Thanks.

Ronald Holshausen

unread,
May 14, 2015, 8:26:33 PM5/14/15
to Chuan Chuan Law, pact-s...@googlegroups.com
The gradle/maven plugins need to be fixed to handle URL encoded form posts.

Chuan Chuan Law

unread,
May 14, 2015, 8:38:56 PM5/14/15
to pact-s...@googlegroups.com

Ah I see......thanks.

Let me know if I need to raise an issue on GitHub.

Ronald Holshausen

unread,
May 14, 2015, 8:39:46 PM5/14/15
to Chuan Chuan Law, pact-s...@googlegroups.com
I think I'll just include it in the existing issue you raised.

Chuan Chuan Law

unread,
Jun 18, 2015, 2:25:13 AM6/18/15
to pact-s...@googlegroups.com
Hi Ronald,


Thanks for fixing the pact provider issue #119.

I guess there is 1 issue from this thread that's supposed to be included in issue #119 as well, which is still outstanding.

Basically the issue is that as below:

Consumer test looks like with post:

      given('Scenario of Carfacts REST asynchronous web service in LiveMarket for any internal requestor from within Carsales network')
            uponReceiving('a POST request')
            withAttributes(method: 'post', path: '/task', headers: ['Content-Type': 'application/x-www-form-urlencoded'],
                    query:'vin=1GJ2GYDG2A1827206&redbook=AAAAAAAA&musthave=7200&jde=100&kms=200&price=20000&pricetype=dap&image=www.google.com' +
                            '&rego=abc123&regostate=vic&colour=red&seller=SSE-SELLER-1984811&recorddesc=Current listing&comparison=0'
            )
            willRespondWith(
                    status: 201,
                    headers: ['Content-Type': 'text/html'],
                    body: '1'
            )

The generated pact file looks like:

 "interactions" : [ {
    "providerState" : "Scenario of Carfacts REST asynchronous web service in LiveMarket for any internal requestor from within Carsales network",
    "description" : "a POST request",
    "request" : {
      "method" : "POST",
      "path" : "/task",
      "headers" : {
        "Content-Type" : "application/x-www-form-urlencoded"
      },
      "query" : "vin=1GJ2GYDG2A1827206&redbook=AAAAAAAA&musthave=7200&jde=100&kms=200&price=20000&pricetype=dap&image=www.google.com&rego=abc123&regostate=vic&colour=red&seller=SSE-SELLER-1984811&recorddesc=Current listing&comparison=0"
    },
    "response" : {
      "status" : 201,
      "headers" : {
        "Content-Type" : "text/html"
      },
      "body" : "1"
    }
  } ],


However, we need the json to look like:


 "interactions" : [ {
    "providerState" : "there is some good mallory",
    "description" : "a retrieve Mallory request",
    "request" : {
      "method" : "POST",
      "path" : "/task",
      "headers" : {
        "Content-Type" : "application/x-www-form-urlencoded"
      },
      "body" : "vin=1GJ2GYDG2A1827206&redbook=AAAAAAAA&musthave=7200&jde=100&kms=200&price=20000&pricetype=dap&image=www.google.com&rego=abc123&regostate=vic&colour=red&seller=SSE-SELLER-1984811&recorddesc=Current listing&comparison=0"

    },
    "response" : {
      "status" : 201,
      "headers" : {
        "Content-Type" : "text/html"
      },
      "body" : "1"
    }


Basically, we need "query" to be "body". Please advice how can we generate a consumer test with POST and generates body.

Thanks.

Ronald Holshausen

unread,
Jun 18, 2015, 3:03:37 AM6/18/15
to Chuan Chuan Law, pact-s...@googlegroups.com
Hi Chuan,

Part of the change I implemented will put the query string in the body when the content type is application/x-www-form-urlencoded.

Chuan Chuan Law

unread,
Jun 18, 2015, 3:15:11 AM6/18/15
to pact-s...@googlegroups.com
Hi Ronald,

I've got the latest version but:

   uponReceiving('a POST request')
            withAttributes(method: 'post', path: '/task', headers: ['Content-Type': 'application/x-www-form-urlencoded'],
                    query:'vin=1GJ2GYDG2A1827206&redbook=AAAAAAAA&musthave=7200&jde=100&kms=200&price=20000&pricetype=dap&image=www.google.com' +
                            '&rego=abc123&regostate=vic&colour=red&seller=SSE-SELLER-1984811&recorddesc=Current listing&comparison=0'
            )

Still generates:

  "request" : {
      "method" : "POST",
      "path" : "/task",
      "headers" : {
        "Content-Type" : "application/x-www-form-urlencoded"
      },
      "query" : "vin=1GJ2GYDG2A1827206&redbook=AAAAAAAA&musthave=7200&jde=100&kms=200&price=20000&pricetype=dap&image=www.google.com&rego=abc123&regostate=vic&colour=red&seller=SSE-SELLER-1984811&recorddesc=Current listing&comparison=0"
    },


Instead of:

 "request" : {
      "method" : "POST",
      "path" : "/task",
      "headers" : {
        "Content-Type" : "application/x-www-form-urlencoded"
      },
      "body" : "vin=1GJ2GYDG2A1827206&redbook=AAAAAAAA&musthave=7200&jde=100&kms=200&price=20000&pricetype=dap&image=www.google.com&rego=abc123&regostate=vic&colour=red&seller=SSE-SELLER-1984811&recorddesc=Current listing&comparison=0"

    },


Please advice.

Thank you.

Ronald Holshausen

unread,
Jun 18, 2015, 7:03:17 PM6/18/15
to Chuan Chuan Law, pact-s...@googlegroups.com
It's still saved to the pact file as a query string. This is the correct form:


  "request" : {
      "method" : "POST",
      "path" : "/task",
      "headers" : {
        "Content-Type" : "application/x-www-form-urlencoded"
      },
      "query" : "vin=1GJ2GYDG2A1827206&redbook=AAAAAAAA&musthave=7200&jde=100&kms=200&price=20000&pricetype=dap&image=www.google.com&rego=abc123&regostate=vic&colour=red&seller=SSE-SELLER-1984811&recorddesc=Current listing&comparison=0"
    },

When the provider validates it, it will post the query string in the body because the content type is application/x-www-form-urlencoded.

Chuan Chuan Law

unread,
Jun 19, 2015, 1:24:49 AM6/19/15
to pact-s...@googlegroups.com

Got it Ronald.

It's working perfectly!

Thanks heaps.


Cheers,

Chuan
Reply all
Reply to author
Forward
0 new messages