Skip to content

Instantly share code, notes, and snippets.

@ianjosephwilson
Created May 21, 2013 04:06
Show Gist options
  • Save ianjosephwilson/5617417 to your computer and use it in GitHub Desktop.
Save ianjosephwilson/5617417 to your computer and use it in GitHub Desktop.
from pyramid.config import Configurator
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.add_route('test-post', '/test-post', request_method='POST')
config.scan()
return config.make_wsgi_app()
~/workspace/testweb/testweb $ nosetests -v
nose.plugins.cover: ERROR: Coverage not available: unable to import coverage module
Test a post request with a str set to the request body using app.post. ... ok
Test a post request with a str set to the request body using app.request. ... ERROR
test_my_view (testweb.tests.ViewTests) ... ok
======================================================================
ERROR: Test a post request with a str set to the request body using app.request.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ian/workspace/testweb/testweb/testweb/test_functional.py", line 19, in test_request_post
res = self.testapp.request('/test-post', method="POST", status=200, body=b'this-is-a-test')
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/webtest/app.py", line 428, in request
expect_errors=expect_errors,
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/webtest/app.py", line 467, in do_request
res = req.get_response(app, catch_exc_info=True)
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/webob/request.py", line 1292, in send
application, catch_exc_info=True)
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/webob/request.py", line 1260, in call_application
app_iter = application(self.environ, start_response)
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/webtest/lint.py", line 198, in lint_app
iterator = application(environ, start_response_wrapper)
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/pyramid/router.py", line 251, in __call__
response = self.invoke_subrequest(request, use_tweens=True)
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/pyramid/router.py", line 227, in invoke_subrequest
response = handle_request(request)
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/pyramid/tweens.py", line 21, in excview_tween
response = handler(request)
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/pyramid/router.py", line 161, in handle_request
response = view_callable(context, request)
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/pyramid/config/views.py", line 347, in rendered_view
result = view(context, request)
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/pyramid/config/views.py", line 493, in _requestonly_view
response = view(request)
File "/home/ian/workspace/testweb/testweb/testweb/views.py", line 11, in test_post
assert request.body == 'this-is-a-test'
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/webob/request.py", line 677, in _body__get
self.make_body_seekable() # we need this to have content_length
File "/home/ian/workspace/testweb/ve/lib/python2.7/site-packages/webob/request.py", line 920, in make_body_seekable
self.body_file_raw.seek(0)
AttributeError: 'InputWrapper' object has no attribute 'seek'
----------------------------------------------------------------------
Ran 3 tests in 0.303s
FAILED (errors=1)
import unittest
class FunctionalTests(unittest.TestCase):
def setUp(self):
from testweb import main
app = main({})
from webtest import TestApp
self.testapp = TestApp(app)
def test_post(self):
""" Test a post request with a str set to the request body using app.post. """
from StringIO import StringIO
res = self.testapp.post('/test-post', 'this-is-a-test', status=200)
self.failUnless('test-response' in res.body)
def test_request_post(self):
""" Test a post request with a str set to the request body using app.request. """
from StringIO import StringIO
res = self.testapp.request('/test-post', method="POST", status=200, body=b'this-is-a-test')
self.failUnless('test-response' in res.body)
from pyramid.view import view_config
@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
return {'project': 'testweb'}
@view_config(route_name='test-post', renderer='string')
def test_post(request):
assert request.body == 'this-is-a-test'
return 'test-response'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment