Class: Wants::TestMimeParsing

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
MIMEParse
Defined in:
lib/wants/mimeparse.rb

Instance Method Summary collapse

Methods included from MIMEParse

best_match, fitness_and_quality_parsed, parse_media_range, parse_mime_type, quality, quality_parsed

Instance Method Details

#assert_best_match(expected, header) ⇒ Object



212
213
214
# File 'lib/wants/mimeparse.rb', line 212

def assert_best_match(expected, header)
  assert_equal(expected, best_match(@supported_mime_types, header))
end

#test_best_matchObject



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
# File 'lib/wants/mimeparse.rb', line 178

def test_best_match
  @supported_mime_types = [ "application/xbel+xml", "application/xml" ]

  # direct match
  assert_best_match "application/xbel+xml", "application/xbel+xml"
  # direct match with a q parameter
  assert_best_match "application/xbel+xml", "application/xbel+xml; q=1"
  # direct match of our second choice with a q parameter
  assert_best_match "application/xml", "application/xml; q=1"
  # match using a subtype wildcard
  assert_best_match "application/xml", "application/*; q=1"
  # match using a type wildcard
  assert_best_match "application/xml", "*/*"

  @supported_mime_types = [ "application/xbel+xml", "text/xml" ]
  # match using a type versus a lower weighted subtype
  assert_best_match "text/xml", "text/*;q=0.5,*/*;q=0.1"
  # fail to match anything
  assert_best_match nil, "text/html,application/atom+xml; q=0.9"
  # common AJAX scenario
  @supported_mime_types = [ "application/json", "text/html" ]
  assert_best_match "application/json", "application/json, text/javascript, */*"
  # verify fitness sorting
  assert_best_match "application/json", "application/json, text/html;q=0.9"
end

#test_parse_media_rangeObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/wants/mimeparse.rb', line 146

def test_parse_media_range
  assert_equal [ "application", "xml", { "q" => "1" } ],
                parse_media_range("application/xml;q=1")

  assert_equal [ "application", "xml", { "q" => "1" } ],
                parse_media_range("application/xml")

  assert_equal [ "application", "xml", { "q" => "1" } ],
                parse_media_range("application/xml;q=")

  assert_equal [ "application", "xml", { "q" => "1", "b" => "other" } ],
                parse_media_range("application/xml ; q=1;b=other")

  assert_equal [ "application", "xml", { "q" => "1", "b" => "other" } ],
                parse_media_range("application/xml ; q=2;b=other")

  # Java URLConnection class sends an Accept header that includes a single "*"
  assert_equal [ "*", "*", { "q" => ".2" } ],
                parse_media_range(" *; q=.2")
end

#test_rfc_2616_exampleObject



167
168
169
170
171
172
173
174
175
176
# File 'lib/wants/mimeparse.rb', line 167

def test_rfc_2616_example
  accept = "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5"

  assert_equal 1, quality("text/html;level=1", accept)
  assert_equal 0.7, quality("text/html", accept)
  assert_equal 0.3, quality("text/plain", accept)
  assert_equal 0.5, quality("image/jpeg", accept)
  assert_equal 0.4, quality("text/html;level=2", accept)
  assert_equal 0.7, quality("text/html;level=3", accept)
end

#test_support_wildcardsObject



204
205
206
207
208
209
210
# File 'lib/wants/mimeparse.rb', line 204

def test_support_wildcards
  @supported_mime_types = ['image/*', 'application/xml']
  # match using a type wildcard
  assert_best_match 'image/*', 'image/png'
  # match using a wildcard for both requested and supported
  assert_best_match 'image/*', 'image/*'
end