11import json
22
3- from flask import Flask , make_response , jsonify , Response
3+ from flask import Flask , make_response , jsonify , Response , request
44
55app = Flask (__name__ )
66
@@ -16,13 +16,13 @@ def html2(): # $routeHandler
1616 # show that we recognize the response creation, and not the return (hopefully). (and
1717 # do the same in the following of the file)
1818 resp = make_response ("<h1>hello</h1>" ) # $HttpResponse $mimetype=text/html $responseBody="<h1>hello</h1>"
19- return resp
19+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
2020
2121
2222@app .route ("/html3" ) # $routeSetup="/html3"
2323def html3 (): # $routeHandler
2424 resp = app .make_response ("<h1>hello</h1>" ) # $HttpResponse $mimetype=text/html $responseBody="<h1>hello</h1>"
25- return resp
25+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
2626
2727
2828# TODO: Create test-cases for the many ways that `make_response` can be used
@@ -32,30 +32,53 @@ def html3(): # $routeHandler
3232@app .route ("/html4" ) # $routeSetup="/html4"
3333def html4 (): # $routeHandler
3434 resp = Response ("<h1>hello</h1>" ) # $HttpResponse $mimetype=text/html $responseBody="<h1>hello</h1>"
35- return resp
35+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
3636
3737
3838@app .route ("/html5" ) # $routeSetup="/html5"
3939def 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.
4242 resp = Flask .response_class ("<h1>hello</h1>" ) # $HttpResponse $mimetype=text/html $responseBody="<h1>hello</h1>"
43- return resp
43+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
4444
4545
4646@app .route ("/html6" ) # $routeSetup="/html6"
4747def 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.
5050 resp = app .response_class ("<h1>hello</h1>" ) # $HttpResponse $mimetype=text/html $responseBody="<h1>hello</h1>"
51- return resp
51+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
5252
5353
5454@app .route ("/jsonify" ) # $routeSetup="/jsonify"
5555def jsonify_route (): # $routeHandler
5656 data = {"foo" : "bar" }
57- response = jsonify (data ) # $f-:HttpResponse $f-:mimetype=application/json $f-:responseBody=data
58- return response
57+ resp = jsonify (data ) # $f-:HttpResponse $f-:mimetype=application/json $f-:responseBody=data
58+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
59+
60+ ################################################################################
61+ # Tricky return handling
62+ ################################################################################
63+
64+ @app .route ("/tricky-return1" ) # $routeSetup="/tricky-return1"
65+ def tricky_return1 (): # $routeHandler
66+ if "raw" in request .args :
67+ resp = "<h1>hellu</h1>"
68+ else :
69+ resp = make_response ("<h1>hello</h1>" ) # $HttpResponse $mimetype=text/html $responseBody="<h1>hello</h1>"
70+ return resp # $HttpResponse $mimetype=text/html $responseBody=resp
71+
72+ def helper ():
73+ if "raw" in request .args :
74+ return "<h1>hellu</h1>"
75+ else :
76+ return make_response ("<h1>hello</h1>" ) # $HttpResponse $mimetype=text/html $responseBody="<h1>hello</h1>"
77+
78+ @app .route ("/tricky-return2" ) # $routeSetup="/tricky-return2"
79+ def tricky_return2 (): # $routeHandler
80+ resp = helper ()
81+ return resp # $HttpResponse $mimetype=text/html $responseBody=resp
5982
6083
6184################################################################################
@@ -67,14 +90,14 @@ def jsonify_route(): # $routeHandler
6790def response_modification1 (): # $routeHandler
6891 resp = make_response ("<h1>hello</h1>" ) # $HttpResponse $mimetype=text/html $responseBody="<h1>hello</h1>"
6992 resp .content_type = "text/plain" # $f-:HttpResponse $f-:mimetype=text/plain
70- return resp
93+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
7194
7295
7396@app .route ("/content-type/response-modification2" ) # $routeSetup="/content-type/response-modification2"
7497def response_modification2 (): # $routeHandler
7598 resp = make_response ("<h1>hello</h1>" ) # $HttpResponse $mimetype=text/html $responseBody="<h1>hello</h1>"
7699 resp .headers ["content-type" ] = "text/plain" # $f-:HttpResponse $f-:mimetype=text/plain
77- return resp
100+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
78101
79102
80103# Exploration of mimetype/content_type/headers arguments to `app.response_class` -- things can get tricky
@@ -84,59 +107,59 @@ def response_modification2(): # $routeHandler
84107@app .route ("/content-type/Response1" ) # $routeSetup="/content-type/Response1"
85108def Response1 (): # $routeHandler
86109 resp = Response ("<h1>hello</h1>" , mimetype = "text/plain" ) # $HttpResponse $mimetype=text/plain $responseBody="<h1>hello</h1>"
87- return resp
110+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
88111
89112
90113@app .route ("/content-type/Response2" ) # $routeSetup="/content-type/Response2"
91114def Response2 (): # $routeHandler
92115 resp = Response ("<h1>hello</h1>" , content_type = "text/plain; charset=utf-8" ) # $HttpResponse $mimetype=text/plain $responseBody="<h1>hello</h1>"
93- return resp
116+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
94117
95118
96119@app .route ("/content-type/Response3" ) # $routeSetup="/content-type/Response3"
97120def Response3 (): # $routeHandler
98121 # content_type argument takes priority (and result is text/plain)
99122 resp = Response ("<h1>hello</h1>" , content_type = "text/plain; charset=utf-8" , mimetype = "text/html" ) # $HttpResponse $mimetype=text/plain $responseBody="<h1>hello</h1>"
100- return resp
123+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
101124
102125
103126@app .route ("/content-type/Response4" ) # $routeSetup="/content-type/Response4"
104127def Response4 (): # $routeHandler
105128 # note: capitalization of Content-Type does not matter
106129 resp = Response ("<h1>hello</h1>" , headers = {"Content-TYPE" : "text/plain" }) # $HttpResponse $f+:mimetype=text/html $f-:mimetype=text/plain $responseBody="<h1>hello</h1>"
107- return resp
130+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
108131
109132
110133@app .route ("/content-type/Response5" ) # $routeSetup="/content-type/Response5"
111134def Response5 (): # $routeHandler
112135 # content_type argument takes priority (and result is text/plain)
113136 # note: capitalization of Content-Type does not matter
114137 resp = Response ("<h1>hello</h1>" , headers = {"Content-TYPE" : "text/html" }, content_type = "text/plain; charset=utf-8" ) # $HttpResponse $mimetype=text/plain $responseBody="<h1>hello</h1>"
115- return resp
138+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
116139
117140
118141@app .route ("/content-type/Response6" ) # $routeSetup="/content-type/Response6"
119142def Response6 (): # $routeHandler
120143 # mimetype argument takes priority over header (and result is text/plain)
121144 # note: capitalization of Content-Type does not matter
122145 resp = Response ("<h1>hello</h1>" , headers = {"Content-TYPE" : "text/html" }, mimetype = "text/plain" ) # $HttpResponse $mimetype=text/plain $responseBody="<h1>hello</h1>"
123- return resp
146+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
124147
125148
126149@app .route ("/content-type/Flask-response-class" ) # $routeSetup="/content-type/Flask-response-class"
127150def Flask_response_class (): # $routeHandler
128151 # note: flask.Flask.response_class is set to `flask.Response` by default.
129152 # it can be overridden, but we don't try to handle that right now.
130153 resp = Flask .response_class ("<h1>hello</h1>" , mimetype = "text/plain" ) # $HttpResponse $mimetype=text/plain $responseBody="<h1>hello</h1>"
131- return resp
154+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
132155
133156
134157@app .route ("/content-type/app-response-class" ) # $routeSetup="/content-type/app-response-class"
135158def app_response_class (): # $routeHandler
136159 # note: app.response_class (flask.Flask.response_class) is set to `flask.Response` by default.
137160 # it can be overridden, but we don't try to handle that right now.
138161 resp = app .response_class ("<h1>hello</h1>" , mimetype = "text/plain" ) # $HttpResponse $mimetype=text/plain $responseBody="<h1>hello</h1>"
139- return resp
162+ return resp # $f+:HttpResponse $f+:mimetype=text/html $f+:responseBody=resp
140163
141164
142165# TODO: add tests for setting status code
0 commit comments