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

Skip to content

Commit 47dcc09

Browse files
committed
Python: Add tests for creating HTTP responses in flask
Which is runnable, if you have flask installed locally
1 parent 8aaa36b commit 47dcc09

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import json
2+
3+
from flask import Flask, make_response, jsonify, Response
4+
5+
app = Flask(__name__)
6+
7+
8+
@app.route("/html1")
9+
def html1():
10+
return "<h1>hello</h1>"
11+
12+
13+
@app.route("/html2")
14+
def html2():
15+
# note that response saved in a variable intentionally -- we wan the annotations to
16+
# show that we recognize the response creation, and not the return (hopefully). (and
17+
# do the same in the following of the file)
18+
resp = make_response("<h1>hello</h1>")
19+
return resp
20+
21+
22+
@app.route("/html3")
23+
def html3():
24+
resp = app.make_response("<h1>hello</h1>")
25+
return resp
26+
27+
28+
# TODO: Create test-cases for the many ways that `make_response` can be used
29+
# https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.make_response
30+
31+
32+
@app.route("/html4")
33+
def html4():
34+
resp = Response("<h1>hello</h1>")
35+
return resp
36+
37+
38+
@app.route("/html5")
39+
def html5():
40+
# note: flask.Flask.response_class is set to `flask.Response` by default.
41+
# it can be overridden, but we don't try to handle that right now.
42+
resp = Flask.response_class("<h1>hello</h1>")
43+
return resp
44+
45+
46+
@app.route("/html6")
47+
def html6():
48+
# note: app.response_class (flask.Flask.response_class) is set to `flask.Response` by default.
49+
# it can be overridden, but we don't try to handle that right now.
50+
resp = app.response_class("<h1>hello</h1>")
51+
return resp
52+
53+
54+
@app.route("/jsonify")
55+
def jsonify_route():
56+
data = {"foo": "bar"}
57+
response = jsonify(data)
58+
return response
59+
60+
61+
################################################################################
62+
# Setting content-type manually
63+
################################################################################
64+
65+
66+
@app.route("/content-type/response-modification1")
67+
def response_modification1():
68+
resp = make_response("<h1>hello</h1>")
69+
resp.content_type = "text/plain"
70+
return resp
71+
72+
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"
77+
return resp
78+
79+
80+
# Exploration of mimetype/content_type/headers arguments to `app.response_class` -- things can get tricky
81+
# see https://werkzeug.palletsprojects.com/en/1.0.x/wrappers/#werkzeug.wrappers.Response
82+
83+
84+
@app.route("/content-type/Response1")
85+
def Response1():
86+
resp = Response("<h1>hello</h1>", mimetype="text/plain")
87+
return resp
88+
89+
90+
@app.route("/content-type/Response2")
91+
def Response2():
92+
resp = Response("<h1>hello</h1>", content_type="text/plain; charset=utf-8")
93+
return resp
94+
95+
96+
@app.route("/content-type/Response3")
97+
def Response3():
98+
# 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")
100+
return resp
101+
102+
103+
@app.route("/content-type/Response4")
104+
def Response4():
105+
# note: capitalization of Content-Type does not matter
106+
resp = Response("<h1>hello</h1>", headers={"Content-TYPE": "text/plain"})
107+
return resp
108+
109+
110+
@app.route("/content-type/Response5")
111+
def Response5():
112+
# content_type argument takes priority (and result is text/plain)
113+
# 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")
115+
return resp
116+
117+
118+
@app.route("/content-type/Flask-response-class")
119+
def Flask_response_class():
120+
# note: flask.Flask.response_class is set to `flask.Response` by default.
121+
# 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")
123+
return resp
124+
125+
126+
@app.route("/content-type/app-response-class")
127+
def app_response_class():
128+
# note: app.response_class (flask.Flask.response_class) is set to `flask.Response` by default.
129+
# 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")
131+
return resp
132+
133+
# TODO: add tests for setting status code
134+
# TODO: add test that manually creates a redirect by setting status code and suitable header.
135+
136+
################################################################################
137+
138+
139+
if __name__ == "__main__":
140+
app.run(debug=True)

0 commit comments

Comments
 (0)