Created
November 30, 2011 00:41
-
-
Save njoyce/1407388 to your computer and use it in GitHub Desktop.
PyAMF test client using WebTest integrating with webapp2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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