Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer exact match without parameters to one with extra parameters #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions mimeparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,23 @@ def quality_and_fitness_parsed(mime_type, parsed_ranges):
# if they do, assess the "fitness" of this mime_type
if type_match and subtype_match:

# 100 points if the type matches w/o a wildcard
fitness = type == target_type and 100 or 0
# 1000 points if the type matches w/o a wildcard
fitness = type == target_type and 1000 or 0

# 10 points if the subtype matches w/o a wildcard
fitness += subtype == target_subtype and 10 or 0
# 100 points if the subtype matches w/o a wildcard
fitness += subtype == target_subtype and 100 or 0

# 1 bonus point for each matching param besides "q"
# 10 bonus points for each matching param besides "q"
param_matches = sum([
1 for (key, value) in target_params.items()
10 for (key, value) in target_params.items()
if key != 'q' and key in params and value == params[key]
])
fitness += param_matches

# 1 bonus point if neither has any parameters
if (set(target_params) | set(params)) - {'q'} == set():
fitness += 1

# finally, add the target's "q" param (between 0 and 1)
fitness += float(target_params.get('q', 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as currently implemented the q has basically no say

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, I fixed this + added a test case for that


Expand Down
8 changes: 8 additions & 0 deletions testdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@
"text/html",
"match should use highest order of supported when there is a tie"
],
[
[
["application/json", "application/json;boundary=NL"],
"application/json"
],
"application/json",
"exact match without parameters should be preferred over similar one with extra parameters"
],
[
[
["application/json;q=1.0", "text/html;q=0.9", "text/plain;q=0.1"],
Expand Down