summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Wojcik <wojcikstefan@gmail.com>2016-09-28 18:20:44 -0400
committerStefan Wojcik <wojcikstefan@gmail.com>2016-09-28 18:20:44 -0400
commit475dfb6911c784cb4a7377d203a4998d742d047f (patch)
treec8271e6b54da5300aaedb2ac6efaf58b024e1b99
parentcc55a1ba1479e2897a819916c28fd050f7a9fea2 (diff)
downloadpython-mimeparse-475dfb6911c784cb4a7377d203a4998d742d047f.tar.gz
let acceptable types specify their quality
-rw-r--r--mimeparse.py3
-rw-r--r--testdata.json8
2 files changed, 11 insertions, 0 deletions
diff --git a/mimeparse.py b/mimeparse.py
index 897b2df..a38278a 100644
--- a/mimeparse.py
+++ b/mimeparse.py
@@ -110,6 +110,9 @@ def quality_and_fitness_parsed(mime_type, parsed_ranges):
])
fitness += param_matches
+ # finally, add the target's "q" param (between 0 and 1)
+ fitness += float(target_params.get('q', 1))
+
if fitness > best_fitness:
best_fitness = fitness
best_fit_q = params['q']
diff --git a/testdata.json b/testdata.json
index cd655c8..07ace39 100644
--- a/testdata.json
+++ b/testdata.json
@@ -167,6 +167,14 @@
],
[
[
+ ["application/json;q=1.0", "text/html;q=0.9", "text/plain;q=0.1"],
+ "*/*"
+ ],
+ "application/json;q=1.0",
+ "*/* match should pick an acceptable type with the highest quality"
+ ],
+ [
+ [
["application/json", "text/html"],
"text"
],