Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Created May 23, 2016 14:24
Show Gist options
  • Save kgriffs/56f92620a2f7cd4a78dab1f799a4dfb5 to your computer and use it in GitHub Desktop.
Save kgriffs/56f92620a2f7cd4a78dab1f799a4dfb5 to your computer and use it in GitHub Desktop.
Example of using a Falcon middleware component to perform a permanent redirect
import falcon
from falcon import testing
class RedirectorComponent(object):
def process_request(self, req, resp):
raise falcon.HTTPMovedPermanently('http://example.org/foo')
class HelloResource(object):
def on_get(self, req, resp):
resp.body = '{"message": "Hello world!"}'
app = falcon.API(middleware=[RedirectorComponent()])
app.add_route('/', HelloResource())
environ = testing.create_environ('/')
srmock = testing.StartResponseMock()
app(environ, srmock)
print(srmock.status)
print(srmock.headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment