From 99d9699e19c77b477ef475f312be4c99d8270620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20W=C3=B3jcik?= Date: Sun, 16 Oct 2016 18:53:21 -0400 Subject: let acceptable types specify their quality (#26) --- mimeparse.py | 3 +++ testdata.json | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/mimeparse.py b/mimeparse.py index 7794341..ae942fa 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 64785c4..2ceee39 100644 --- a/testdata.json +++ b/testdata.json @@ -186,6 +186,22 @@ "text/html", "match should use highest order of supported when there is a tie" ], + [ + [ + ["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" + ], + [ + [ + ["text/html;q=0.9", "application/json", "text/plain;q=0.1"], + "*/*" + ], + "application/json", + "*/* match should pick an acceptable type with the highest quality, even if it's implicit" + ], [ [ ["application/json", "text/html"], -- cgit v1.2.1