Skip to content

Instantly share code, notes, and snippets.

@AndreLouisCaron
Created October 1, 2012 11:22
Show Gist options
  • Save AndreLouisCaron/3811052 to your computer and use it in GitHub Desktop.
Save AndreLouisCaron/3811052 to your computer and use it in GitHub Desktop.
Cornice/Pyramid application with 201 response.
# -*- coding: utf-8 -*-
from pyramid.config import Configurator
from cornice.resource import resource, view
from webtest import TestApp
@resource(path='/hello')
class Hello(object):
def __init__(self, request):
self.request = request
@view(renderer='json')
def get(self):
self.request.response.status = 201
return {'message': "Hello, world!"}
if __name__ == '__main__':
# Prepare the Pyramid/Cornice application.
application = Configurator()
application.include('cornice')
application.scan(__name__)
application = TestApp(application.make_wsgi_app())
# Run a request on our service.
response = application.get('/hello', expect_errors=False)
# Verify the HTTP status.
if response.status_code != 201:
print 'HTTP status "%s" is wrong, expecting "201".'
# Verify the content type.
content_type = response.headers['content-type']
if content_type:
content_type = content_type.split(';', 1)[0]
if content_type == 'application/json':
print response.json
else:
print 'Content type "%s" is wrong, expecting "application/json".'
print response.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment