3

I am using Ian Bicking's WebOb to very great effect in writing Python web application tests. I call webob.Request.blank('/path...'), and then use the resulting request object's get_response(app) method to invoke my web application. The response object that is returned lets me check the HTTP response's status code, content type, body, and so forth. Building a POST request is also quite easy:

Request.blank('/path/under/test/', POST={'query': 'some text'})

But now I have run across a bit of a puzzle: I need to test a view in my web application that expects a file upload, and I cannot quite figure out how WebOb represents that particular kind of POST. Does anyone know how to build a WebOb request with one or more file-upload fields inside?

Brandon Rhodes
  • 83,755
  • 16
  • 106
  • 147

2 Answers2

5

As of a couple days ago you can do:

req = Request.blank('/path/under/test', 
                    POST={'query': 'some text', 'upload': ('filename', 'content')})

This was brought in in this commit, and has not yet been released.

Ian Bicking
  • 9,762
  • 6
  • 33
  • 32
  • A perfect-but-unreleased answer that would make the operation simple beyond my wildest imaginings! And also a new situation for me: do I dangle the Big Green Checkmark as incentive for you to release? Or go ahead and accept the answer and grumpily run WebOb from trunk on this project? :) – Brandon Rhodes Aug 05 '11 at 16:58
1

You can use WebTest for that, see this TestApp.post arguments here.

andreypopp
  • 6,887
  • 5
  • 26
  • 26