Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from ESKYoung/bugfix_github_image_caching
Browse files Browse the repository at this point in the history
Bugfix image caching on GitHub
  • Loading branch information
ESKYoung authored Oct 17, 2020
2 parents 30f5775 + 7b8ac1c commit d52b050
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
10 changes: 5 additions & 5 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": null,
"lines": null
},
"generated_at": "2020-10-17T09:53:15Z",
"generated_at": "2020-10-17T11:10:15Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -63,28 +63,28 @@
"hashed_secret": "65b5658c038dcc7b3f9ee80669c0f26ac1441d05",
"is_secret": true,
"is_verified": false,
"line_number": 54,
"line_number": 55,
"type": "Hex High Entropy String"
},
{
"hashed_secret": "5e18621e95bc4b9b75d7d5b097929ecfc9494078",
"is_secret": true,
"is_verified": false,
"line_number": 55,
"line_number": 56,
"type": "Hex High Entropy String"
},
{
"hashed_secret": "2b2718d1211a6b3c6ae4ef13c63469cb1eafa6c7",
"is_secret": true,
"is_verified": false,
"line_number": 56,
"line_number": 57,
"type": "Hex High Entropy String"
},
{
"hashed_secret": "186df42ce44b6dc5a79c097baf114cf67eb9a13b",
"is_secret": true,
"is_verified": false,
"line_number": 57,
"line_number": 58,
"type": "Hex High Entropy String"
}
]
Expand Down
8 changes: 7 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timedelta
from flask import Flask, Response, redirect, request
from typing import Dict, Optional, Tuple, Union
from urllib.parse import SplitResult, urlsplit, urlunsplit
Expand Down Expand Up @@ -165,8 +166,13 @@ def get_shields_io_badge() -> Union[Response, Tuple[str, int]]:
# Get the Shields.IO badge
svg = requests.get(compile_shields_io_url(message=message, **request_arguments))

# Set the expiry time, and create a response header
expiry_time = datetime.utcnow() - timedelta(minutes=10)
headers = {"Cache-Control": "no-cache,max-age=0,no-store,s-maxage=0,proxy-revalidate",
"Expires": expiry_time.strftime("%a, %d %b %Y %H:%M:%S GMT")}

# Return the badge to the user
return Response(response=svg, content_type="image/svg+xml")
return Response(response=svg, content_type="image/svg+xml", headers=headers)


if __name__ == '__main__':
Expand Down
12 changes: 10 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import timedelta
from hypothesis import example, given, settings
from hypothesis.strategies import characters, dictionaries, one_of, text
from flask import request
Expand Down Expand Up @@ -419,11 +420,18 @@ def test_flask_response_is_called_correctly(self, mocker, test_input_page: str,
# Patch the get_page_count, and requests.get functions, and the flask.Response class
_ = mocker.patch("main.get_page_count")
patch_requests_get = mocker.patch("requests.get")
patch_datetime = mocker.patch("main.datetime")
patch_flask_response = mocker.patch("main.Response")

# Get the /badge page of the app
_ = app.test_client().get("/badge", query_string={"page": test_input_page, **test_input_query})

# Define the mock expiry time
mock_expiry_time = patch_datetime.utcnow.return_value - timedelta(minutes=10)

# Assert flask.Response is called with the correct arguments
patch_flask_response.assert_called_once_with(response=patch_requests_get.return_value,
content_type="image/svg+xml")
patch_flask_response.assert_called_once_with(
response=patch_requests_get.return_value, content_type="image/svg+xml",
headers={"Cache-Control": "no-cache,max-age=0,no-store,s-maxage=0,proxy-revalidate",
"Expires": mock_expiry_time.strftime("%a, %d %b %Y %H:%M:%S GMT")}
)

0 comments on commit d52b050

Please sign in to comment.