Search code examples
pythonajaxpyramid

Testing AJAX post in TestApp and Pyramid


I'm trying to write a test for an ajax view...The view is configured like so:

@view_config(name='new', context='resource.Events', renderer='json',
             request_method='POST', xhr=True)
def event_view(self):
    # ...

In my test, I want to create an ajax post so I try:

extra_environ = {'X_REQUESTED_WITH' : 'XmlHttpRequest'}
# also tried setting HTTP_X_REQUESTED_WITH

self.testapp.post('/events/new', params=post_params, extra_environ=extra_environ)

But the post never gets routed to my view. The request.is_xhr param never gets set to True.

Should be a simple answer somewhere, but I could not find it in any of the docs, or elsewhere. Can anyone recommend how to do this?

Thanks!


Solution

  • Looks like I wasn't casing 'xmlhttprequest' correctly. Needs to be:

    extra_environ = {'HTTP_X_REQUESTED_WITH' : 'XMLHttpRequest'}
    

    Found this in the code at:

    webob/request.py line 472

    Unfortunate to need to dig around in the code for this. Also surprised case matters.