question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make testing resource methods easier by creating arbitrary req/resp

See original GitHub issue

It 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 a Request and Response object.
    • Enable passing json, body, and stream
    • Automatically set Content-Length
    • Do best effort to set Content-Type
    • Enable passing arbitrary WSGI envs
  • Create a method on TestClient which will utilize the global method but fill in some of the environment details and utilize application specific Request and Response.
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:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
kgriffscommented, Aug 28, 2019

As it turns out, I need this for dual-testing ASGI/WSGI so I’m working on it as part of the ASGI branch.

1reaction
vytas7commented, Dec 7, 2020

The remaining work items and documentation improvements were deferred to https://github.com/falconry/falcon/issues/1793.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found