Exemplo n.º 1
0
    def __init__(self, service):
        Resource.__init__(self)
        self.service = service
        self.resources = {
            '/capoeira/events/similar/artist':
            self.service.capoeiraSimilarByArtistQuery,
            '/capoeira/events/similar/track':
            self.service.capoeiraSimilarByTrackQuery
        }
        self.isLeaf = True

        # default_params is used in lieu of a defined Accept field in the header
        default_params = AcceptParameters(ContentType("text/html"),
                                          Language("en"))
        acceptable = [
            AcceptParameters(ContentType("text/html"), Language("en")),
            AcceptParameters(ContentType("text/json"), Language("en")),
            AcceptParameters(ContentType("application/json"), Language("en"))
        ]
        self.contentNegotiator = ContentNegotiator(default_params, acceptable)
        # function mapping for rendering response
        self.renderFns = {
            'text/html': formatHTMLResponse,
            'text/json': formatJSONResponse,
            'application/json': formatJSONResponse
        }
Exemplo n.º 2
0
 def test_wildcard_by_itself(self):
     """
     * by itself
     """
     accept_language = "*"
     server = [
         AcceptParameters(language=Language("no")),
         AcceptParameters(language=Language("de")),
     ]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert str(accept_parameters.language) == "no"
Exemplo n.º 3
0
 def test_image_wildcard_supported_by_serverwith_other_options_primary_option_supported(self):
     """
     * with other options, primary option supported
     """
     accept_language = "en, *"
     server = [
         AcceptParameters(language=Language("en")),
         AcceptParameters(language=Language("de")),
     ]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert str(accept_parameters.language) == "en"
Exemplo n.º 4
0
 def test_mixed_q_values_most_preferred_available(self):
     """
     en vs no vs de with mixed q values, most preferred available
     """
     accept_language = "en;q=0.6, no;q=0.9, de"
     server = [
         AcceptParameters(language=Language("no")),
         AcceptParameters(language=Language("de")),
     ]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert str(accept_parameters.language) == "de"
Exemplo n.º 5
0
 def test_with_q_values(self):
     """
     fr vs no with q values
     """
     accept_language = "fr;q=0.7, no;q=0.8"
     server = [
         AcceptParameters(language=Language("fr")),
         AcceptParameters(language=Language("no")),
     ]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert str(accept_parameters.language) == "no"
Exemplo n.º 6
0
 def test_without_q_values(self):
     """
     en vs de without q values
     """
     accept_language = "en, de"
     server = [
         AcceptParameters(language=Language("en")),
         AcceptParameters(language=Language("de")),
     ]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert str(accept_parameters.language) == "en"
Exemplo n.º 7
0
 def test_with_mixed_q_values(self):
     """
     en vs de vs fr with mixed q values
     """
     accept_language = "en;q=0.6, de;q=0.9, fr"
     server = [
         AcceptParameters(language=Language("en")),
         AcceptParameters(language=Language("de")),
         AcceptParameters(language=Language("fr")),
     ]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert str(accept_parameters.language) == "fr"
Exemplo n.º 8
0
 def test_two_content_types_and_two_languages_specified(self):
     """
     Two content types and 2 languages specified
     """
     accept = "text/html, text/plain"
     accept_lang = "en, de"
     server = [
         AcceptParameters(ContentType("text/html"), Language("de")),
         AcceptParameters(ContentType("text/plain"), Language("en")),
     ]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(
         accept=accept, accept_language=accept_lang
     )
     assert str(accept_parameters.content_type) == "text/html"
     assert str(accept_parameters.language) == "de"
Exemplo n.º 9
0
 def test_partially_supported_by_server(self):
     """
     en, partially supported by server
     """
     accept_language = "en"
     server = [AcceptParameters(language=Language("en-gb"))]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert str(accept_parameters.language) == "en-gb"
Exemplo n.º 10
0
 def test_unsupported_by_server(self):
     """
     en only, unsupported by server
     """
     accept_language = "en"
     server = [AcceptParameters(language=Language("de"))]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert accept_parameters is None
Exemplo n.º 11
0
 def test_en_only(self):
     """
     en only
     """
     accept_language = "en"
     server = [AcceptParameters(language=Language("en"))]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert str(accept_parameters.language) == "en"
Exemplo n.º 12
0
 def test_supported_by_server_through_language_variants(self):
     """
     en-gb, supported by server through language variants
     """
     accept_language = "en-gb"
     server = [AcceptParameters(language=Language("en"))]
     cn = ContentNegotiator(
         acceptable=server, ignore_language_variants=True
     )
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert str(accept_parameters.language) == "en"
Exemplo n.º 13
0
 def test_unsupported_by_server(self):
     """
     en-gb, unsupported by server
     """
     accept_language = "en-gb"
     server = [AcceptParameters(language=Language("en"))]
     cn = ContentNegotiator(
         acceptable=server, ignore_language_variants=False
     )
     accept_parameters = cn.negotiate(accept_language=accept_language)
     assert accept_parameters is None
Exemplo n.º 14
0
 def test_two_content_types_and_one_language_specified_with_weights(self):
     """
     Two content types and one language specified, with weights
     """
     weights = {
         "content_type": 2.0,
         "language": 1.0,
         "charset": 1.0,
         "encoding": 1.0,
     }
     accept = "text/html, text/plain"
     accept_lang = "en"
     server = [
         AcceptParameters(ContentType("text/html"), Language("de")),
         AcceptParameters(ContentType("text/plain"), Language("en")),
     ]
     cn = ContentNegotiator(acceptable=server, weights=weights)
     accept_parameters = cn.negotiate(
         accept=accept, accept_language=accept_lang
     )
     assert str(accept_parameters.content_type) == "text/plain"
     assert str(accept_parameters.language) == "en"
Exemplo n.º 15
0
 def test_content_type_and_language_specified(self):
     """
     content type and language specified
     """
     accept = "text/html"
     accept_lang = "en"
     server = [AcceptParameters(ContentType("text/html"), Language("en"))]
     cn = ContentNegotiator(acceptable=server)
     accept_parameters = cn.negotiate(
         accept=accept, accept_language=accept_lang
     )
     assert str(accept_parameters.content_type) == "text/html"
     assert str(accept_parameters.language) == "en"