Unit test for XMLRPCController

31 views
Skip to first unread message

Bryan

unread,
Mar 25, 2009, 12:44:51 PM3/25/09
to pylons-discuss
I am trying to write some unit tests for an XMLRPCController. I am
trying to use xmlrpclib to do this. I don't know what URI to pass
into ServerProxy.

from xmlrpclib import ServerProxy
server = ServerProxy('http://ipaddress:port/xmlrpc/')
server.system.listMethods()

raises a 'Connection refused' error.

Kumar McMillan

unread,
Mar 25, 2009, 2:41:38 PM3/25/09
to pylons-...@googlegroups.com

Connection refused means you passed it an address correctly but the
connection was, eh, refused.

However, for testing you may want to use a mock object to temporarily
replace the xmlrpclib ServerProxy so that it doesn't go out into the
Internet when you run your tests. I wrote a module named Fudge for
exactly this kind of situation -- http://farmdev.com/projects/fudge/
-- but there are a lot of other similar libraries for Python available
via your favorite search engine.

K

Bryan

unread,
Mar 25, 2009, 3:18:04 PM3/25/09
to pylons-discuss
I could simply make calls to the controller itself, but I want to
include the transport layer in the tests, that way I can catch errors
in badly formed xmlrpc requests and responses.

Let me ask a more general question:
When pylons unit tests run, is the test instance actually bound to an
ip and port?

In my test.ini I have a server host and port defined, are those used
at all when unit tests are run? How can paste.fixture make requests
to my regular controllers, but I can't make an xmlrpc request?

On Mar 25, 11:41 am, Kumar McMillan <kumar.mcmil...@gmail.com> wrote:
> On Wed, Mar 25, 2009 at 11:44 AM, Bryan <bryanv...@gmail.com> wrote:
>
> > I am trying to write some unit tests for an XMLRPCController.  I am
> > trying to use xmlrpclib to do this.  I don't know what URI to pass
> > into ServerProxy.
>
> > from xmlrpclib import ServerProxy
> > server = ServerProxy('http://ipaddress:port/xmlrpc/')
> > server.system.listMethods()
>
> > raises a 'Connection refused' error.
>
> Connection refused means you passed it an address correctly but the
> connection was, eh, refused.
>
> However, for testing you may want to use a mock object to temporarily
> replace the xmlrpclib ServerProxy so that it doesn't go out into the
> Internet when you run your tests.  I wrote a module named Fudge for
> exactly this kind of situation --http://farmdev.com/projects/fudge/

Kumar McMillan

unread,
Mar 25, 2009, 5:09:12 PM3/25/09
to pylons-...@googlegroups.com
On Wed, Mar 25, 2009 at 2:18 PM, Bryan <brya...@gmail.com> wrote:
>
> I could simply make calls to the controller itself, but I want to
> include the transport layer in the tests, that way I can catch errors
> in badly formed xmlrpc requests and responses.
>

oh sorry I see what you mean now. You are connecting to the xmlrpc
server that you implemented using XMLRPCController.

> Let me ask a more general question:
> When pylons unit tests run, is the test instance actually bound to an
> ip and port?

Nope, in the tests a "server" is not running at all. it is not
attached to an ip or port. What you have instead is a WSGI app that
has been loaded into a WebTest instance; all this happens in-process.

You could take another approach and use wsgi_intercept:
http://code.google.com/p/wsgi-intercept/ ... however ... I don't
recall how to intercept something that uses httplib (which is what
drives xmlrpclib). d'oh

My best suggestion is to write a custom Transport :

class WSGIAppTransport(xmlrpclib.Transport):
def make_connection(self, host):
host, extra_headers, x509 = self.get_host_info(host)
from pylons.test import pylonsapp
return WSGILikeHTTP(host, pylonsapp)

# then use it in your test :
from xmlrpclib import ServerProxy
server = ServerProxy('http://ipaddress:port/xmlrpc/',
transport=WSGIAppTransport())

The implementation of WSGILikeHTTP would need to re-route all http
requests to your WSGI pylonsapp which is created in
yourapp/tests/__init__.py . You'd have to implement all the methods
from httplib.HTTP like putrequest() and getreply() etc.

... or maybe there is an easier way? not sure. Good question.

>
> In my test.ini I have a server host and port defined, are those used
> at all when unit tests are run? How can paste.fixture make requests
> to my regular controllers, but I can't make an xmlrpc request?

the reason is because paste.fixture (and WebTest) are designed to
simulate a wsgi server environment not an xmlrpc server environment.

Bryan

unread,
Mar 25, 2009, 5:54:32 PM3/25/09
to pylons-discuss
Thanks for the informative reply. I will have to try it out. I may
just move the xmlrpc out of pylons. The only thing I seem to be
getting from using the XMLRPCController is the debug web page when
something goes wrong.

On Mar 25, 2:09 pm, Kumar McMillan <kumar.mcmil...@gmail.com> wrote:
> On Wed, Mar 25, 2009 at 2:18 PM, Bryan <bryanv...@gmail.com> wrote:
>
> > I could simply make calls to the controller itself, but I want to
> > include the transport layer in the tests, that way I can catch errors
> > in badly formed xmlrpc requests and responses.
>
> oh sorry I see what you mean now.  You are connecting to the xmlrpc
> server that you implemented using XMLRPCController.
>
> > Let me ask a more general question:
> > When pylons unit tests run, is the test instance actually bound to an
> > ip and port?
>
> Nope, in the tests a "server" is not running at all.  it is not
> attached to an ip or port.  What you have instead is a WSGI app that
> has been loaded into a WebTest instance; all this happens in-process.
>
> You could take another approach and use wsgi_intercept:http://code.google.com/p/wsgi-intercept/... however ... I don't
Reply all
Reply to author
Forward
0 new messages