Skip to content

Commit 9bbb781

Browse files
committed
support etag for static resources
1 parent 935592c commit 9bbb781

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

api.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ def format_diff_response(fmt, diff, opts):
5555

5656
@app.route('/')
5757
def index():
58-
return render_template('index.html')
58+
resp = Response(
59+
render_template('index.html'),
60+
mimetype='text/html',
61+
status=200,
62+
)
63+
resp.add_etag()
64+
resp.make_conditional(request)
65+
66+
return resp
5967

6068

6169
@app.route('/ping')
@@ -124,16 +132,24 @@ def patch():
124132

125133
@app.route('/api/v1/nested_diff.css')
126134
def nested_diff_css():
127-
return Response(HtmlFormatter.get_css(), mimetype='text/css', status=200)
135+
resp = Response(HtmlFormatter.get_css(), mimetype='text/css', status=200)
136+
resp.add_etag()
137+
resp.make_conditional(request)
138+
139+
return resp
128140

129141

130142
@app.route('/api/v1/nested_diff.js')
131143
def nested_diff_script():
132-
return Response(
144+
resp = Response(
133145
HtmlFormatter().get_script(),
134146
mimetype='text/javascript',
135147
status=200,
136148
)
149+
resp.add_etag()
150+
resp.make_conditional(request)
151+
152+
return resp
137153

138154

139155
if __name__ == '__main__':

tests/test_api_misc.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ def test_index(client):
33

44
assert rv.content_type.startswith('text/html')
55
assert rv.status_code == 200
6+
assert 'ETag' in rv.headers
7+
8+
9+
def test_css(client):
10+
rv = client.get('/api/v1/nested_diff.css')
11+
12+
assert rv.content_type.startswith('text/css')
13+
assert rv.status_code == 200
14+
assert 'ETag' in rv.headers
15+
16+
17+
def test_js(client):
18+
rv = client.get('/api/v1/nested_diff.js')
19+
20+
assert rv.content_type.startswith('text/javascript')
21+
assert rv.status_code == 200
22+
assert 'ETag' in rv.headers
623

724

825
def test_health_check(client):

0 commit comments

Comments
 (0)