1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Test::Fatal;
BEGIN {
use_ok('Web::Machine::Util::ContentNegotiation', 'choose_media_type');
}
ok(!defined( choose_media_type( [], '*/*' ) ), '... got nothing back');
ok(!defined( choose_media_type( [ "text/html" ], 'application/json' ) ), '... got nothing back');
# Examples from http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
=pod
The example
Accept: audio/*; q=0.2, audio/basic
SHOULD be interpreted as "I prefer audio/basic, but send me any
audio type if it is the best available after an 80% mark-down
in quality."
=cut
is(
choose_media_type(
["audio/basic", "audio/oog"],
"audio/*; q=0.2, audio/basic"
),
'audio/basic',
'... got the right media type back (prefer audio/basic)'
);
is(
choose_media_type(
["audio/mp3", "audio/oog"],
"audio/*; q=0.2, audio/basic"
),
'audio/mp3',
'... got the right media type back (prefer audio/* and choose audio/mp3)'
);
=pod
A more elaborate example is
Accept: text/plain; q=0.5, text/html,
text/x-dvi; q=0.8, text/x-c
Verbally, this would be interpreted as "text/html and text/x-c
are the preferred media types, but if they do not exist, then
send the text/x-dvi entity, and if that does not exist, send
the text/plain entity."
=cut
is(
choose_media_type(
["text/html", "text/plain"],
"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"
),
'text/html',
'... got the right media type back (prefer text/html over lesser quality options)'
);
is(
choose_media_type(
["text/html", "text/x-dvi"],
"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"
),
'text/html',
'... got the right media type back (prefer text/html over lesser quality options)'
);
is(
choose_media_type(
["text/x-c", "text/plain"],
"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"
),
'text/x-c',
'... got the right media type back (prefer text/x-c over lesser quality options)'
);
is(
choose_media_type(
["text/x-c", "text/x-dvi"],
"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"
),
'text/x-c',
'... got the right media type back (prefer text/x-c over lesser quality options)'
);
is(
choose_media_type(
["text/x-c", "text/html"],
"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"
),
'text/html',
'... got the right media type back (prefer text/html over text/x-c)'
);
is(
choose_media_type(
["text/sgml", "text/x-dvi"],
"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"
),
'text/x-dvi',
'... got the right media type back (accept text/x-dvi)'
);
is(
choose_media_type(
["text/sgml", "text/plain", "text/x-dvi"],
"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"
),
'text/x-dvi',
'... got the right media type back (prefer text/x-dvi over text/plain)'
);
is(
choose_media_type(
["text/sgml", "text/plain", ],
"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"
),
'text/plain',
'... got the right media type back (accept text/plain)'
);
=pod
Media ranges can be overridden by more specific media ranges
or specific media types. If more than one media range applies
to a given type, the most specific reference has precedence.
For example,
Accept: text/*, text/html, text/html;level=1, */*
have the following precedence:
1) text/html;level=1
2) text/html
3) text/*
4) */*
=cut
is(
choose_media_type(
["text/html", "text/html;level=1" ],
"text/*, text/html, text/html;level=1, */*"
),
'text/html; level="1"',
'... got the right media type back (prefer text/html;level=1 because it is more specific)'
);
is(
choose_media_type(
["text/plain", "text/html" ],
"text/*, text/html, text/html;level=1, */*"
),
'text/html',
'... got the right media type back (prefer text/html to other less specific options)'
);
# Examples from webmachine-ruby
is(
choose_media_type(
["text/html", "application/xml"],
"application/xml, text/html, */*"
),
'application/xml',
'... got the right media type back (choose application/xml because of header ordering)'
);
is(
choose_media_type(
["text/html", "text/html;charset=iso8859-1" ],
"text/html;charset=iso8859-1, application/xml"
),
'text/html; charset="iso8859-1"',
'... got the right media type back (choose the more specific text/html;charset=iso8859-1)'
);
is(
choose_media_type(
["application/json;v=3;foo=bar", "application/json;v=2"],
"text/html, application/json"
),
'application/json; v="3"; foo="bar"',
'... got the right media type back (choose application/json;v=3;foo=bar because of preference ordering)'
);
is(
choose_media_type(
["text/html", "application/xml"],
"application/xml;q=0.7, text/html, */*"
),
'text/html',
'... got the right media type back (choose text/html because of quality level and preference ordering)'
);
is(
choose_media_type(
["text/html", "application/xml"],
"bah"
),
undef,
'... got no media type back'
);
done_testing;
|