Make testing resource methods easier by creating arbitrary req/resp
See original GitHub issueIt can be useful when you want to test a resource method in isolation to manually create a Request
and Response
object and pass them directly to the resource method.
def test_my_resource():
resource = MyResource()
req, resp = Request(helpers.create_environ()), Response()
resource.on_get(req, resp)
There are some gotchas with manually creating the environ and it could be much easier.
- Provide a global method in
testing.helpers
which creates aRequest
andResponse
object.- Enable passing
json
,body
, andstream
- Automatically set
Content-Length
- Do best effort to set
Content-Type
- Enable passing arbitrary WSGI envs
- Enable passing
- Create a method on
TestClient
which will utilize the global method but fill in some of theenvironment
details and utilize application specificRequest
andResponse
.
def create_test_request_response(json: dict = None, body: string = None, context: dict = None, **env):
env['body'] = (
(body if body is not None else None)
or (json.dumps(json) if json else '')
)
env = falcon.testing.create_environ(**env,)
req = falcon.Request(env)
req.context.update(context or {})
return (req, falcon.Response())
def test_my_resource():
resource = MyResource()
req, resp = create_test_request_response(json={'hello': 'world'})
resource.on_get(req, resp)
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Add Resources and Methods to Your REST Tests - SoapUI
Adding REST Services, Resources and Methods. 2. Generated WADLs ... Start by creating a new REST Service in your project. Right-click your project...
Read more >Routing — Falcon 3.1.1 documentation
Falcon uses resource-based routing to encourage a RESTful architectural style. Each resource is represented by a class that is responsible for handling all ......
Read more >7 HTTP methods every web developer should know and how ...
GET is often the default method in HTTP clients, so creating tests for these resources should be simple with any tool you choose....
Read more >Alternate routes to the same resource #584 - falconry/falcon
A frequent question from new community members is how to use a single resource class to handle requests for a single item as...
Read more >Falcon Documentation - Read the Docs
Falcon lives on GitHub, making the code easy to browse, download, fork, etc. ... The image resource above defines a single method, on_get()....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
As it turns out, I need this for dual-testing ASGI/WSGI so I’m working on it as part of the ASGI branch.
The remaining work items and documentation improvements were deferred to https://github.com/falconry/falcon/issues/1793.