Skip to content

Instantly share code, notes, and snippets.

@njoyce
Created November 30, 2011 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njoyce/1407388 to your computer and use it in GitHub Desktop.
Save njoyce/1407388 to your computer and use it in GitHub Desktop.
PyAMF test client using WebTest integrating with webapp2
import unittest
from pyamf.remoting.client import RemotingService
import webtest
class ResponseProxy(object):
"""
Mimics a urllib2 response from a WebOb response
"""
def __init__(self, response):
self.response = response
def info(self):
return self
def getheader(self, name):
return self.response.headers.get(name)
def read(self, length):
return self.response.body[:length]
def makeAMFClient(app, url):
"""
Makes a test PyAMF client.
"""
client = webtest.TestApp(app)
def myopener(request):
response = client.post(url, params=request.data, headers=request.headers)
return ResponseProxy(response)
return RemotingService('http://localhost/' + url.lstrip('/'), opener=myopener)
def makeApp(services):
"""
Return a webapp WSGI application with the PyAMF gateway at '/'
"""
from google.appengine.ext import webapp
from pyamf.remoting.gateway.google import WebAppGateway
gateway = WebAppGateway(services)
application_paths = [('/', gateway)]
return webapp.WSGIApplication(application_paths)
class ServiceTestCase(unittest.TestCase):
def test_something(self):
def echo(data):
return data
services = {
'myservice.echo': echo,
}
app = makeApp(services)
client = makeAMFClient(app, '/')
s = client.getService('myservice')
self.assertEqual(s.echo('foobar'), 'foobar')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment