flask icon indicating copy to clipboard operation
flask copied to clipboard

test_client fails for url with "%2F"

Open cthrax opened this issue 11 years ago • 3 comments

I'm working with a parameterized url such as:

"/api/<string:apiToken>"

When I send this token containing "%2F" the url encoded form of "/" I get a 404 from the test client. However, the running app works fine.

I'm using Flask 0.10.1 and a minimal test example looks like the following:

import unittest
import flask
class TestTestClient(unittest.TestCase):
    def test_url(self):
        app = flask.Flask(__name__)
        @app.route('/<string:parameter>')
        def hello_world(parameter):
            return parameter

        test_client = app.test_client()

        response = test_client.get("/FOO")
        self.assertEqual(response.status_code, 200)  # Passes
        self.assertEqual(response.data, "FOO")  # Passes

        response = test_client.get("/%%2F")
        self.assertEqual(response.status_code, 200) # Fails with 404

cthrax avatar May 06 '15 15:05 cthrax

I've used the route you described and found the following:

  • http://127.0.0.1:5000%2F - does work (no text is output and no 404 is thrown)
  • http://127.0.0.1:5000%2Ftest - doesn't seem to work ("test" is not output and no 404 is thrown)

I don't know if this is expected behaviour but seeing as though the app doesn't work with encoded URLs maybe the test_client isn't supposed to either.

indriesergiu avatar May 10 '15 16:05 indriesergiu

Interesting, I reran some of the things I was doing and I think I was just getting lucky. I would have "http://127.0.0.1:5000/%2Ffoo" which would work fine in. Since my "foo" strings were random, I didn't notice that trend.

Thanks for the help!

cthrax avatar May 11 '15 13:05 cthrax

I may also be facing similar problem. Similar, not exactly same, cause my URL itself doesn't contain the token. The token is part of the header. But considering the "randomness", I think I have same issue. When I use requests library, there are no errors, since it is actually talks to the server. The problem is seen only when using test_client.

For at least one URL, token is not needed, yet I get 404 randomly. If I run the tests back to back multiple times, I get combination of 200/201 (for GET and POST respectively) and 404 for the same URL

mandarvaze avatar Sep 03 '15 08:09 mandarvaze