Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfc29bb

Browse files
committed
Python: Add annotations for flask response tests
The fact that we need to add routeSetup and routeHandler annotations is sort of annoying :|
1 parent 47dcc09 commit bfc29bb

1 file changed

Lines changed: 54 additions & 50 deletions

File tree

python/ql/test/experimental/library-tests/frameworks/flask/response_test.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,56 @@
55
app = Flask(__name__)
66

77

8-
@app.route("/html1")
9-
def html1():
10-
return "<h1>hello</h1>"
8+
@app.route("/html1") # $routeSetup="/html1"
9+
def html1(): # $routeHandler
10+
return "<h1>hello</h1>" # $f-:HttpResponse $f-:contentType=text/html $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
1111

1212

13-
@app.route("/html2")
14-
def html2():
13+
@app.route("/html2") # $routeSetup="/html2"
14+
def html2(): # $routeHandler
1515
# note that response saved in a variable intentionally -- we wan the annotations to
1616
# show that we recognize the response creation, and not the return (hopefully). (and
1717
# do the same in the following of the file)
18-
resp = make_response("<h1>hello</h1>")
18+
resp = make_response("<h1>hello</h1>") # $f-:HttpResponse $f-:contentType=text/html $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
1919
return resp
2020

2121

22-
@app.route("/html3")
23-
def html3():
24-
resp = app.make_response("<h1>hello</h1>")
22+
@app.route("/html3") # $routeSetup="/html3"
23+
def html3(): # $routeHandler
24+
resp = app.make_response("<h1>hello</h1>") # $f-:HttpResponse $f-:contentType=text/html $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
2525
return resp
2626

2727

2828
# TODO: Create test-cases for the many ways that `make_response` can be used
2929
# https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.make_response
3030

3131

32-
@app.route("/html4")
33-
def html4():
34-
resp = Response("<h1>hello</h1>")
32+
@app.route("/html4") # $routeSetup="/html4"
33+
def html4(): # $routeHandler
34+
resp = Response("<h1>hello</h1>") # $f-:HttpResponse $f-:contentType=text/html $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
3535
return resp
3636

3737

38-
@app.route("/html5")
39-
def html5():
38+
@app.route("/html5") # $routeSetup="/html5"
39+
def html5(): # $routeHandler
4040
# note: flask.Flask.response_class is set to `flask.Response` by default.
4141
# it can be overridden, but we don't try to handle that right now.
42-
resp = Flask.response_class("<h1>hello</h1>")
42+
resp = Flask.response_class("<h1>hello</h1>") # $f-:HttpResponse $f-:contentType=text/html $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
4343
return resp
4444

4545

46-
@app.route("/html6")
47-
def html6():
46+
@app.route("/html6") # $routeSetup="/html6"
47+
def html6(): # $routeHandler
4848
# note: app.response_class (flask.Flask.response_class) is set to `flask.Response` by default.
4949
# it can be overridden, but we don't try to handle that right now.
50-
resp = app.response_class("<h1>hello</h1>")
50+
resp = app.response_class("<h1>hello</h1>") # $f-:HttpResponse $f-:contentType=text/html $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
5151
return resp
5252

5353

54-
@app.route("/jsonify")
55-
def jsonify_route():
54+
@app.route("/jsonify") # $routeSetup="/jsonify"
55+
def jsonify_route(): # $routeHandler
5656
data = {"foo": "bar"}
57-
response = jsonify(data)
57+
response = jsonify(data) # $f-:HttpResponse $f-:contentType=application/json $f-:statusCode=200 $f-:responseBody=data
5858
return response
5959

6060

@@ -63,71 +63,75 @@ def jsonify_route():
6363
################################################################################
6464

6565

66-
@app.route("/content-type/response-modification1")
67-
def response_modification1():
68-
resp = make_response("<h1>hello</h1>")
69-
resp.content_type = "text/plain"
66+
@app.route("/content-type/response-modification1") # $routeSetup="/content-type/response-modification1"
67+
def response_modification1(): # $routeHandler
68+
resp = make_response("<h1>hello</h1>") # $f-:HttpResponse $f-:contentType=text/html $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
69+
resp.content_type = "text/plain" # $f-:HttpResponse $f-:contentType=text/plain
7070
return resp
7171

7272

73-
@app.route("/content-type/response-modification2")
74-
def response_modification2():
75-
resp = make_response("<h1>hello</h1>")
76-
resp.headers["content-type"] = "text/plain"
73+
@app.route("/content-type/response-modification2") # $routeSetup="/content-type/response-modification2"
74+
def response_modification2(): # $routeHandler
75+
resp = make_response("<h1>hello</h1>") # $f-:HttpResponse $f-:contentType=text/html $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
76+
resp.headers["content-type"] = "text/plain" # $f-:HttpResponse $f-:contentType=text/plain
7777
return resp
7878

7979

8080
# Exploration of mimetype/content_type/headers arguments to `app.response_class` -- things can get tricky
8181
# see https://werkzeug.palletsprojects.com/en/1.0.x/wrappers/#werkzeug.wrappers.Response
8282

8383

84-
@app.route("/content-type/Response1")
85-
def Response1():
86-
resp = Response("<h1>hello</h1>", mimetype="text/plain")
84+
@app.route("/content-type/Response1") # $routeSetup="/content-type/Response1"
85+
def Response1(): # $routeHandler
86+
resp = Response("<h1>hello</h1>", mimetype="text/plain") # $f-:HttpResponse $f-:contentType=text/plain $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
8787
return resp
8888

8989

90-
@app.route("/content-type/Response2")
91-
def Response2():
92-
resp = Response("<h1>hello</h1>", content_type="text/plain; charset=utf-8")
90+
@app.route("/content-type/Response2") # $routeSetup="/content-type/Response2"
91+
def Response2(): # $routeHandler
92+
resp = Response("<h1>hello</h1>", content_type="text/plain; charset=utf-8") # $f-:HttpResponse $f-:contentType=text/plain $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
9393
return resp
9494

9595

96-
@app.route("/content-type/Response3")
97-
def Response3():
96+
@app.route("/content-type/Response3") # $routeSetup="/content-type/Response3"
97+
def Response3(): # $routeHandler
9898
# content_type argument takes priority (and result is text/plain)
99-
resp = Response("<h1>hello</h1>", content_type="text/plain; charset=utf-8", mimetype="text/html")
99+
resp = Response(
100+
"<h1>hello</h1>", content_type="text/plain; charset=utf-8", mimetype="text/html"
101+
) # $f-:HttpResponse $f-:contentType=text/plain $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
100102
return resp
101103

102104

103-
@app.route("/content-type/Response4")
104-
def Response4():
105+
@app.route("/content-type/Response4") # $routeSetup="/content-type/Response4"
106+
def Response4(): # $routeHandler
105107
# note: capitalization of Content-Type does not matter
106-
resp = Response("<h1>hello</h1>", headers={"Content-TYPE": "text/plain"})
108+
resp = Response("<h1>hello</h1>", headers={"Content-TYPE": "text/plain"}) # $f-:HttpResponse $f-:contentType=text/plain $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
107109
return resp
108110

109111

110-
@app.route("/content-type/Response5")
111-
def Response5():
112+
@app.route("/content-type/Response5") # $routeSetup="/content-type/Response5"
113+
def Response5(): # $routeHandler
112114
# content_type argument takes priority (and result is text/plain)
113115
# note: capitalization of Content-Type does not matter
114-
resp = Response("<h1>hello</h1>", headers={"Content-TYPE": "text/html"}, content_type="text/plain; charset=utf-8")
116+
resp = Response(
117+
"<h1>hello</h1>", headers={"Content-TYPE": "text/html"}, content_type="text/plain; charset=utf-8"
118+
) # $f-:HttpResponse $f-:contentType=text/plain $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
115119
return resp
116120

117121

118-
@app.route("/content-type/Flask-response-class")
119-
def Flask_response_class():
122+
@app.route("/content-type/Flask-response-class") # $routeSetup="/content-type/Flask-response-class"
123+
def Flask_response_class(): # $routeHandler
120124
# note: flask.Flask.response_class is set to `flask.Response` by default.
121125
# it can be overridden, but we don't try to handle that right now.
122-
resp = Flask.response_class("<h1>hello</h1>", mimetype="text/plain")
126+
resp = Flask.response_class("<h1>hello</h1>", mimetype="text/plain") # $f-:HttpResponse $f-:contentType=text/plain $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
123127
return resp
124128

125129

126-
@app.route("/content-type/app-response-class")
127-
def app_response_class():
130+
@app.route("/content-type/app-response-class") # $routeSetup="/content-type/app-response-class"
131+
def app_response_class(): # $routeHandler
128132
# note: app.response_class (flask.Flask.response_class) is set to `flask.Response` by default.
129133
# it can be overridden, but we don't try to handle that right now.
130-
resp = app.response_class("<h1>hello</h1>", mimetype="text/plain")
134+
resp = app.response_class("<h1>hello</h1>", mimetype="text/plain") # $f-:HttpResponse $f-:contentType=text/plain $f-:statusCode=200 $f-:responseBody="<h1>hello</h1>"
131135
return resp
132136

133137
# TODO: add tests for setting status code

0 commit comments

Comments
 (0)