svanoort / pyresttest

Python Rest Testing
Apache License 2.0
1.15k stars 325 forks source link

[Bug?] POST using x-www-form-urlencoded causes issue with templating #147

Closed nitrocode closed 8 years ago

nitrocode commented 8 years ago

When I use POST, I saw that these 2 lines had different results. Is this a bug?

# this works
    - body: 'username=qa@testing.com&password=test'
# this does not
    - body: '{"username": "qa@testing.com", "password": "test"}'

I also tried templating with the working line but it did not have the correct results.

- config:
    - variable_binds: {'user': 'qa@testing.com', 'pass': 'test'}
- test
    ...
    - body: {template: 'username=$user&password=$pass'}

Tried using the following flags but wasn't able to do any further debugging:

--print-bodies=true --print-headers=true --ssl-insecure --verbose --log=debug

The only example using x-www-form-urlencoded doesn't use templating. Am I templating poorly?

nitrocode commented 8 years ago

@svanoort could you clarify this?

svanoort commented 8 years ago

@nitrocode Yes, it's not a bug, it has to do with how form data is formatted on a POST request. PyRestTest passes request body data directly, so the data must be correctly formatted for your endpoint.

The first example is correctly formatted form data (see: https://en.wikipedia.org/wiki/POST_(HTTP)#Use_for_submitting_web_forms). The second example is JSON content that expresses the same data but is not form encoded, so it is not valid.

As far as the templating issue: which version of PyRestTest and version of Python are you using -- the last stable pip release (1.6.0) or the current GitHub master branch (many unreleased changes so far)?

I really need to see the request sent, because that syntax looks correct to me at a glance (worth noting that you need to perform URL encoding on your variable data if you want to include it on the request, but I think this is correct). Try setting interactive mode when running as well, which should show the request body there.

We've got an enhancement request on the future roadmap for a more flexible and powerful system for supplying request bodies (at the very least, doing encoding of form data): https://github.com/svanoort/pyresttest/issues/115

svanoort commented 8 years ago

@nitrocode Were you able to verify what the request body was coming from the templated result & what version you are using?

nitrocode commented 8 years ago

@svanoort the interactive mode doesn't give me much help.

Interactive mode for the working hard coded body string.

===================================
Login
-----------------------------------
REQUEST:
POST https://workbench-rc.netprospex.com/api/v1/users/process-login
HEADERS:
{'Content-Type': 'application/x-www-form-urlencoded'}

username=qa@testing.com&password=test

Interactive mode for the broken hard coded body json.

===================================
Login
-----------------------------------
REQUEST:
POST https://workbench-rc.netprospex.com/api/v1/users/process-login
HEADERS:
{'Content-Type': 'application/x-www-form-urlencoded'}

{"username": "username=qa@testing.com", "password": "test"}

I'd have to step through the code to figure this out.

svanoort commented 8 years ago

@nitrocode I was asking about the broken templated version. The second version does not work because it is supplying the wrong kind of data (JSON).

This is valid form data:

username=qa@testing.com&password=test

This is JSON, it is not compatible. even if your endpoint supports both, it needs to be content-type "application/json"

{"username": "username=qa@testing.com", "password": "test"}

What about the templating with the valid form data?

nitrocode commented 8 years ago

@svanoort I'd like to keep contributing but for some reason when I connect to our internal https site, it says that pycurl keeps timing out. This could be due to the security ticket. Using curl -k I was able to access the page. Will check again when I get home

svanoort commented 8 years ago

@nitrocode Were you able to reproduce the issue with the templated form data (in the correct format)?

svanoort commented 8 years ago

@nitrocode I am going to go ahead and close this issue, since it is not clear if it is a bug, and after nearly a month it is still waiting on information to be able to reproduce. Feel free to reply back with the information and I will be happy to re-open it.