def test_from_string(self): assert MediaType.from_string('*/*') == MediaType('*/*') assert MediaType.from_string('*/*; q=0.9') == MediaType('*/*', 0.9) assert MediaType.from_string('*/*;q=0.9') == MediaType('*/*', 0.9) with pytest.raises(ValueError) as e: MediaType.from_string('*/*;0.9') assert 'ValueError: "*/*;0.9" is not a valid media type' in str(e)
def get(self, request, *args, **kwargs): status_code = 500 if self.errors else 200 accept_header = request.META.get('HTTP_ACCEPT', '*/*') for media in MediaType.parse_header(accept_header): if media.mime_type in ('text/html', 'application/xhtml+xml', 'text/*', '*/*'): return HttpResponse('OK' if status_code == 200 else 'ERROR', status=status_code) return HttpResponse( 'Not Acceptable: Supported content types: text/html, application/json', status=406, content_type='text/plain')
def test_parse_header(self): assert list(MediaType.parse_header()) == [ MediaType('*/*'), ] assert list(MediaType.parse_header('text/html; q=0.1, application/xhtml+xml; q=0.1 ,application/json')) == [ MediaType('application/json'), MediaType('text/html', 0.1), MediaType('application/xhtml+xml', 0.1), ]
def test_from_invalid_strings(self, type_arg): with pytest.raises(ValueError) as e: MediaType.from_string(type_arg) expected_error = '"%s" is not a valid media type' % type_arg assert expected_error in str(e.value)
def test_from_valid_strings(self, type_arg, expected): assert MediaType.from_string(type_arg) == expected
def test_eq(self): assert MediaType('*/*') == MediaType('*/*') assert MediaType('*/*', 0.9) != MediaType('*/*')
def test_repr(self): assert repr(MediaType('*/*')) == "MediaType: */*; q=1.0"
def test_str(self): assert str(MediaType('*/*')) == "*/*; q=1.0" assert str(MediaType('image/*', 0.6)) == "image/*; q=0.6"
def test_lt(self): assert not MediaType('*/*') < MediaType('*/*') assert not MediaType('*/*') < MediaType('*/*', 0.9) assert MediaType('*/*', 0.9) < MediaType('*/*')
class TestMediaType: def test_lt(self): assert not MediaType('*/*') < MediaType('*/*') assert not MediaType('*/*') < MediaType('*/*', 0.9) assert MediaType('*/*', 0.9) < MediaType('*/*') def test_str(self): assert str(MediaType('*/*')) == "*/*; q=1.0" assert str(MediaType('image/*', 0.6)) == "image/*; q=0.6" def test_repr(self): assert repr(MediaType('*/*')) == "MediaType: */*; q=1.0" def test_eq(self): assert MediaType('*/*') == MediaType('*/*') assert MediaType('*/*', 0.9) != MediaType('*/*') valid_strings = [ ('*/*', MediaType('*/*')), ('*/*; q=0.9', MediaType('*/*', 0.9)), ('*/*; q=0', MediaType('*/*', 0.0)), ('*/*; q=0.0', MediaType('*/*', 0.0)), ('*/*; q=0.1', MediaType('*/*', 0.1)), ('*/*; q=0.12', MediaType('*/*', 0.12)), ('*/*; q=0.123', MediaType('*/*', 0.123)), ('*/*; q=1.000', MediaType('*/*', 1.0)), ('*/*; q=1', MediaType('*/*', 1.0)), ('*/*;q=0.9', MediaType('*/*', 0.9)), ('*/* ;q=0.9', MediaType('*/*', 0.9)), ('*/* ; q=0.9', MediaType('*/*', 0.9)), ('*/* ; q=0.9', MediaType('*/*', 0.9)), ('*/*;v=b3', MediaType('*/*')), ('*/*; q=0.5; v=b3', MediaType('*/*', 0.5)), ] @pytest.mark.parametrize("type_arg, expected", valid_strings) def test_from_valid_strings(self, type_arg, expected): assert MediaType.from_string(type_arg) == expected invalid_strings = [ '*/*;0.9', 'text/html;z=""', 'text/html; xxx', 'text/html; =a', ] @pytest.mark.parametrize("type_arg", invalid_strings) def test_from_invalid_strings(self, type_arg): with pytest.raises(ValueError) as e: MediaType.from_string(type_arg) expected_error = '"%s" is not a valid media type' % type_arg assert expected_error in str(e.value) def test_parse_header(self): assert list(MediaType.parse_header()) == [ MediaType('*/*'), ] assert list( MediaType.parse_header( 'text/html; q=0.1, application/xhtml+xml; q=0.1 ,application/json' )) == [ MediaType('application/json'), MediaType('text/html', 0.1), MediaType('application/xhtml+xml', 0.1), ]