twisted / twisted/klein

Route "/" below branch route doesn't work (returns 404)

Open
#237 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
838
Forks
123
Avg merge
7h 58m
Merged PRs (30d)
12

Description

Klein version 17.10.0

In the following example program (adapted from an example from another issue) I'm trying to get the route /users/ to return a list of all users.

Retrieving http://localhost:8123/users/123 works as expected, returning A User Named "123"..

Retrieving http://localhost:8123/users redirects to http://localhost:8124/users/, which returns a 404 Not Found error. I would expect it to return All users!.

Is this expected behavior?

from klein import route, run, Klein

class UsersApp(object):
    routes = Klein()

    # This route doesn't seem to work: results in a 404 Not found error
    @routes.route("/")
    def all_users(self, request):
        request.setHeader('content-type', 'text/plain')
        return 'All users!'

    @routes.route("/<string:username>")
    def an_user(self, request, username):
        request.setHeader('content-type', 'text/plain')
        return 'A User Named "{username}".'.format(username=username)

class MathApp(object):
    mathroutes = Klein()
    @mathroutes.route("/add/<int:a>/<int:b>")
    def add(self, request, a, b):
        return str(a + b)


@route("/users", branch=True)
def users_app(request):
    return UsersApp().routes.resource()

@route("/math", branch=True)
def math_app(request):
    return MathApp().mathroutes.resource()

@route("/")
def hi(request):
    return "Hi"

run("localhost", 8124)

Decorating all_users with @routes.route("") results in an error ('urls must start with a leading slash').

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Klein 17.10.0 example and trace how the branch route at /users delegates to UsersApp().routes.resource(). Check the route handling for / and /string:username; done means /users/ returns "All users!" while /users/123 continues to return the named-user response without a 404.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.