1# (c) 2010 Ch. Zwerschke and contributors 2# This module is part of the Python Paste Project and is released under 3# the MIT License: http://www.opensource.org/licenses/mit-license.php 4 5from paste.util.mimeparse import * 6 7def test_parse_mime_type(): 8 parse = parse_mime_type 9 assert parse('*/*') == ('*', '*', {}) 10 assert parse('text/html') == ('text', 'html', {}) 11 assert parse('audio/*; q=0.2') == ('audio', '*', {'q': '0.2'}) 12 assert parse('text/x-dvi;level=1') == ('text', 'x-dvi', {'level': '1'}) 13 assert parse('image/gif; level=2; q=0.4') == ( 14 'image', 'gif', {'level': '2', 'q': '0.4'}) 15 assert parse('application/xhtml;level=3;q=0.5') == ( 16 'application', 'xhtml', {'level': '3', 'q': '0.5'}) 17 assert parse('application/xml') == ('application', 'xml', {}) 18 assert parse('application/xml;q=1') == ('application', 'xml', {'q': '1'}) 19 assert parse('application/xml ; q=1;b=other') == ( 20 'application', 'xml', {'q': '1', 'b': 'other'}) 21 assert parse('application/xml ; q=2;b=other') == ( 22 'application', 'xml', {'q': '2', 'b': 'other'}) 23 assert parse('application/xhtml;q=0.5') == ( 24 'application', 'xhtml', {'q': '0.5'}) 25 assert parse('application/xhtml;q=0.5;ver=1.2') == ( 26 'application', 'xhtml', {'q': '0.5', 'ver': '1.2'}) 27 28def test_parse_illformed_mime_type(): 29 parse = parse_mime_type 30 assert parse('*') == ('*', '*', {}) 31 assert parse('text') == ('text', '*', {}) 32 assert parse('text/') == ('text', '*', {}) 33 assert parse('/plain') == ('*', 'plain', {}) 34 assert parse('/') == ('*', '*', {}) 35 assert parse('text/plain;') == ('text', 'plain', {}) 36 assert parse(';q=0.5') == ('*', '*', {'q': '0.5'}) 37 assert parse('*; q=.2') == ('*', '*', {'q': '.2'}) 38 assert parse('image; q=.7; level=3') == ( 39 'image', '*', {'q': '.7', 'level': '3'}) 40 assert parse('*;q=1') == ('*', '*', {'q': '1'}) 41 assert parse('*;q=') == ('*', '*', {}) 42 assert parse('*;=0.5') == ('*', '*', {}) 43 assert parse('*;q=foobar') == ('*', '*', {'q': 'foobar'}) 44 assert parse('image/gif; level=2; q=2') == ( 45 'image', 'gif', {'level': '2', 'q': '2'}) 46 assert parse('application/xml;q=') == ('application', 'xml', {}) 47 assert parse('application/xml ;q=') == ('application', 'xml', {}) 48 assert parse(' *; q =;') == ('*', '*', {}) 49 assert parse(' *; q=.2') == ('*', '*', {'q': '.2'}) 50 51def test_parse_media_range(): 52 parse = parse_media_range 53 assert parse('application/*;q=0.5') == ('application', '*', {'q': '0.5'}) 54 assert parse('text/plain') == ('text', 'plain', {'q': '1'}) 55 assert parse('*') == ('*', '*', {'q': '1'}) 56 assert parse(';q=0.5') == ('*', '*', {'q': '0.5'}) 57 assert parse('*;q=0.5') == ('*', '*', {'q': '0.5'}) 58 assert parse('*;q=1') == ('*', '*', {'q': '1'}) 59 assert parse('*;q=') == ('*', '*', {'q': '1'}) 60 assert parse('*;q=-1') == ('*', '*', {'q': '1'}) 61 assert parse('*;q=foobar') == ('*', '*', {'q': '1'}) 62 assert parse('*;q=0.0001') == ('*', '*', {'q': '0.0001'}) 63 assert parse('*;q=1000.0') == ('*', '*', {'q': '1'}) 64 assert parse('*;q=0') == ('*', '*', {'q': '0'}) 65 assert parse('*;q=0.0000') == ('*', '*', {'q': '0.0000'}) 66 assert parse('*;q=1.0001') == ('*', '*', {'q': '1'}) 67 assert parse('*;q=2') == ('*', '*', {'q': '1'}) 68 assert parse('*;q=1e3') == ('*', '*', {'q': '1'}) 69 assert parse('image/gif; level=2') == ( 70 'image', 'gif', {'level': '2', 'q': '1'}) 71 assert parse('image/gif; level=2; q=0.5') == ( 72 'image', 'gif', {'level': '2', 'q': '0.5'}) 73 assert parse('image/gif; level=2; q=2') == ( 74 'image', 'gif', {'level': '2', 'q': '1'}) 75 assert parse('application/xml') == ('application', 'xml', {'q': '1'}) 76 assert parse('application/xml;q=1') == ('application', 'xml', {'q': '1'}) 77 assert parse('application/xml;q=') == ('application', 'xml', {'q': '1'}) 78 assert parse('application/xml ;q=') == ('application', 'xml', {'q': '1'}) 79 assert parse('application/xml ; q=1;b=other') == ( 80 'application', 'xml', {'q': '1', 'b': 'other'}) 81 assert parse('application/xml ; q=2;b=other') == ( 82 'application', 'xml', {'q': '1', 'b': 'other'}) 83 assert parse(' *; q =;') == ('*', '*', {'q': '1'}) 84 assert parse(' *; q=.2') == ('*', '*', {'q': '.2'}) 85 86def test_fitness_and_quality_parsed(): 87 faq = fitness_and_quality_parsed 88 assert faq('*/*;q=0.7', [ 89 ('foo', 'bar', {'q': '0.5'})]) == (0, 0.5) 90 assert faq('foo/*;q=0.7', [ 91 ('foo', 'bar', {'q': '0.5'})]) == (100, 0.5) 92 assert faq('*/bar;q=0.7', [ 93 ('foo', 'bar', {'q': '0.5'})]) == (10, 0.5) 94 assert faq('foo/bar;q=0.7', [ 95 ('foo', 'bar', {'q': '0.5'})]) == (110, 0.5) 96 assert faq('text/html;q=0.7', [ 97 ('foo', 'bar', {'q': '0.5'})]) == (-1, 0) 98 assert faq('text/html;q=0.7', [ 99 ('text', 'bar', {'q': '0.5'})]) == (-1, 0) 100 assert faq('text/html;q=0.7', [ 101 ('foo', 'html', {'q': '0.5'})]) == (-1, 0) 102 assert faq('text/html;q=0.7', [ 103 ('text', '*', {'q': '0.5'})]) == (100, 0.5) 104 assert faq('text/html;q=0.7', [ 105 ('*', 'html', {'q': '0.5'})]) == (10, 0.5) 106 assert faq('text/html;q=0.7', [ 107 ('*', '*', {'q': '0'}), ('text', 'html', {'q': '0.5'})]) == (110, 0.5) 108 assert faq('text/html;q=0.7', [ 109 ('*', '*', {'q': '0.5'}), ('audio', '*', {'q': '0'})]) == (0, 0.5) 110 assert faq('audio/mp3;q=0.7', [ 111 ('*', '*', {'q': '0'}), ('audio', '*', {'q': '0.5'})]) == (100, 0.5) 112 assert faq('*/mp3;q=0.7', [ 113 ('foo', 'mp3', {'q': '0.5'}), ('audio', '*', {'q': '0'})]) == (10, 0.5) 114 assert faq('audio/mp3;q=0.7', [ 115 ('audio', 'ogg', {'q': '0'}), ('*', 'mp3', {'q': '0.5'})]) == (10, 0.5) 116 assert faq('audio/mp3;q=0.7', [ 117 ('*', 'ogg', {'q': '0'}), ('*', 'mp3', {'q': '0.5'})]) == (10, 0.5) 118 assert faq('text/html;q=0.7', [ 119 ('text', 'plain', {'q': '0'}), 120 ('plain', 'html', {'q': '0'}), 121 ('text', 'html', {'q': '0.5'}), 122 ('html', 'text', {'q': '0'})]) == (110, 0.5) 123 assert faq('text/html;q=0.7;level=2', [ 124 ('plain', 'html', {'q': '0', 'level': '2'}), 125 ('text', '*', {'q': '0.5', 'level': '3'}), 126 ('*', 'html', {'q': '0.5', 'level': '2'}), 127 ('image', 'gif', {'q': '0.5', 'level': '2'})]) == (100, 0.5) 128 assert faq('text/html;q=0.7;level=2', [ 129 ('text', 'plain', {'q': '0'}), ('text', 'html', {'q': '0'}), 130 ('text', 'plain', {'q': '0', 'level': '2'}), 131 ('text', 'html', {'q': '0.5', 'level': '2'}), 132 ('*', '*', {'q': '0', 'level': '2'}), 133 ('text', 'html', {'q': '0', 'level': '3'})]) == (111, 0.5) 134 assert faq('text/html;q=0.7;level=2;opt=3', [ 135 ('text', 'html', {'q': '0'}), 136 ('text', 'html', {'q': '0', 'level': '2'}), 137 ('text', 'html', {'q': '0', 'opt': '3'}), 138 ('*', '*', {'q': '0', 'level': '2', 'opt': '3'}), 139 ('text', 'html', {'q': '0', 'level': '3', 'opt': '3'}), 140 ('text', 'html', {'q': '0.5', 'level': '2', 'opt': '3'}), 141 ('*', '*', {'q': '0', 'level': '3', 'opt': '3'})]) == (112, 0.5) 142 143def test_quality_parsed(): 144 qp = quality_parsed 145 assert qp('image/gif;q=0.7', [('image', 'jpg', {'q': '0.5'})]) == 0 146 assert qp('image/gif;q=0.7', [('image', '*', {'q': '0.5'})]) == 0.5 147 assert qp('audio/mp3;q=0.7;quality=100', [ 148 ('*', '*', {'q': '0', 'quality': '100'}), 149 ('audio', '*', {'q': '0', 'quality': '100'}), 150 ('*', 'mp3', {'q': '0', 'quality': '100'}), 151 ('audio', 'mp3', {'q': '0', 'quality': '50'}), 152 ('audio', 'mp3', {'q': '0.5', 'quality': '100'}), 153 ('audio', 'mp3', {'q': '0.5'})]) == 0.5 154 155def test_quality(): 156 assert quality('text/html', 157 'text/*;q=0.3, text/html;q=0.75, text/html;level=1,' 158 ' text/html;level=2;q=0.4, */*;q=0.5') == 0.75 159 assert quality('text/html;level=2', 160 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' 161 ' text/html;level=2;q=0.4, */*;q=0.5') == 0.4 162 assert quality('text/plain', 163 'text/*;q=0.25, text/html;q=0.7, text/html;level=1,' 164 ' text/html;level=2;q=0.4, */*;q=0.5') == 0.25 165 assert quality('plain/text', 166 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' 167 ' text/html;level=2;q=0.4, */*;q=0.5') == 0.5 168 assert quality('text/html;level=1', 169 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' 170 ' text/html;level=2;q=0.4, */*;q=0.5') == 1 171 assert quality('image/jpeg', 172 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' 173 ' text/html;level=2;q=0.4, */*;q=0.5') == 0.5 174 assert quality('text/html;level=2', 175 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' 176 ' text/html;level=2;q=0.375, */*;q=0.5') == 0.375 177 assert quality('text/html;level=3', 178 'text/*;q=0.3, text/html;q=0.75, text/html;level=1,' 179 ' text/html;level=2;q=0.4, */*;q=0.5') == 0.75 180 181def test_best_match(): 182 bm = best_match 183 assert bm([], '*/*') == '' 184 assert bm(['application/xbel+xml', 'text/xml'], 185 'text/*;q=0.5,*/*; q=0.1') == 'text/xml' 186 assert bm(['application/xbel+xml', 'audio/mp3'], 187 'text/*;q=0.5,*/*; q=0.1') == 'application/xbel+xml' 188 assert bm(['application/xbel+xml', 'audio/mp3'], 189 'text/*;q=0.5,*/mp3; q=0.1') == 'audio/mp3' 190 assert bm(['application/xbel+xml', 'text/plain', 'text/html'], 191 'text/*;q=0.5,*/plain; q=0.1') == 'text/plain' 192 assert bm(['application/xbel+xml', 'text/html', 'text/xhtml'], 193 'text/*;q=0.1,*/xhtml; q=0.5') == 'text/html' 194 assert bm(['application/xbel+xml', 'text/html', 'text/xhtml'], 195 '*/html;q=0.1,*/xhtml; q=0.5') == 'text/xhtml' 196 assert bm(['application/xbel+xml', 'application/xml'], 197 'application/xbel+xml') == 'application/xbel+xml' 198 assert bm(['application/xbel+xml', 'application/xml'], 199 'application/xbel+xml; q=1') == 'application/xbel+xml' 200 assert bm(['application/xbel+xml', 'application/xml'], 201 'application/xml; q=1') == 'application/xml' 202 assert bm(['application/xbel+xml', 'application/xml'], 203 'application/*; q=1') == 'application/xbel+xml' 204 assert bm(['application/xbel+xml', 'application/xml'], 205 '*/*, application/xml') == 'application/xml' 206 assert bm(['application/xbel+xml', 'text/xml'], 207 'text/*;q=0.5,*/*; q=0.1') == 'text/xml' 208 assert bm(['application/xbel+xml', 'text/xml'], 209 'text/html,application/atom+xml; q=0.9') == '' 210 assert bm(['application/json', 'text/html'], 211 'application/json, text/javascript, */*') == 'application/json' 212 assert bm(['application/json', 'text/html'], 213 'application/json, text/html;q=0.9') == 'application/json' 214 assert bm(['image/*', 'application/xml'], 'image/png') == 'image/*' 215 assert bm(['image/*', 'application/xml'], 'image/*') == 'image/*' 216 217def test_illformed_best_match(): 218 bm = best_match 219 assert bm(['image/png', 'image/jpeg', 'image/gif', 'text/html'], 220 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'image/jpeg' 221 assert bm(['image/png', 'image/jpg', 'image/tif', 'text/html'], 222 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'text/html' 223 assert bm(['image/png', 'image/jpg', 'image/tif', 'audio/mp3'], 224 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'image/png' 225 226def test_sorted_match(): 227 dm = desired_matches 228 assert dm(['text/html', 'application/xml'], 229 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,' 230 'text/plain;q=0.8,image/png') == ['text/html', 'application/xml'] 231 assert dm(['text/html', 'application/xml'], 232 'application/xml,application/json') == ['application/xml'] 233 assert dm(['text/xhtml', 'text/plain', 'application/xhtml'], 234 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,' 235 'text/plain;q=0.8,image/png') == ['text/plain'] 236