Esempio n. 1
0
def mark_as_api_request(request, accept):
    """Mark a request as IAPIRequest if there's a service registered for the
    actual request method and Accept header.
    """
    method = request.get("REQUEST_METHOD", "GET")
    if method == "OPTIONS" and request.getHeader("Origin", False):
        preflighted_method = request.getHeader("Access-Control-Request-Method",
                                               None)
        service_id = lookup_preflight_service_id(preflighted_method)
        request._rest_cors_preflight = True
    else:
        service_id = lookup_service_id(method, accept)
        request._rest_cors_preflight = False

    if service_id is not None:
        alsoProvides(request, IAPIRequest)
        request._rest_service_id = service_id

        # Flag as non-WebDAV request in order to avoid special treatment
        # in ZPublisher.BaseRequest.traverse().
        request.maybe_webdav_client = 0
Esempio n. 2
0
def mark_as_api_request(event):
    """Mark a request as IAPIRequest if there's a service registered for the
       actual request method and Accept header.
    """
    request = event.request
    method = request.get('REQUEST_METHOD', 'GET')
    if method == 'OPTIONS' and request.getHeader('Origin', False):
        preflighted_method = request.getHeader('Access-Control-Request-Method',
                                               None)
        service_id = lookup_preflight_service_id(preflighted_method)
        request._rest_cors_preflight = True
    else:
        accept = request.getHeader('Accept', 'text/html')
        service_id = lookup_service_id(method, accept)
        request._rest_cors_preflight = False

    if service_id is not None:
        alsoProvides(request, IAPIRequest)
        request._rest_service_id = service_id

        # Flag as non-WebDAV request in order to avoid special treatment
        # in ZPublisher.BaseRequest.traverse().
        request.maybe_webdav_client = 0
Esempio n. 3
0
def mark_as_api_request(event):
    """Mark a request as IAPIRequest if there's a service registered for the
       actual request method and Accept header.
    """
    request = event.request
    method = request.get('REQUEST_METHOD', 'GET')
    if method == 'OPTIONS' and request.getHeader('Origin', False):
        preflighted_method = request.getHeader(
            'Access-Control-Request-Method', None)
        service_id = lookup_preflight_service_id(preflighted_method)
        request._rest_cors_preflight = True
    else:
        accept = request.getHeader('Accept', 'text/html')
        service_id = lookup_service_id(method, accept)
        request._rest_cors_preflight = False

    if service_id is not None:
        alsoProvides(request, IAPIRequest)
        request._rest_service_id = service_id

        # Flag as non-WebDAV request in order to avoid special treatment
        # in ZPublisher.BaseRequest.traverse().
        request.maybe_webdav_client = 0
Esempio n. 4
0
 def test_register_media_type(self):
     self.assertEqual(u"GET_application_json_",
                      register_service("GET", ("application", "json")))
     self.assertEqual(u"GET_application_json_",
                      lookup_service_id("GET", "application/json"))
Esempio n. 5
0
 def test_service_id_for_wildcard_type_is_none(self):
     register_service("GET", "application/json")
     self.assertEqual(None, lookup_service_id("GET", "*/*"))
Esempio n. 6
0
 def test_service_id_for_wildcard_subtype_is_none(self):
     register_service("GET", "text/xml")
     self.assertEqual(None, lookup_service_id("GET", "text/*"))
Esempio n. 7
0
 def test_service_id_for_invalid_media_type_is_none(self):
     self.assertEqual(None, lookup_service_id("GET", "application-json"))
Esempio n. 8
0
 def test_service_id_for_not_registered_media_type_is_none(self):
     self.assertEqual(None, lookup_service_id("PUT", "text/html"))
Esempio n. 9
0
 def test_register_wilcard_type(self):
     self.assertEqual(u"PATCH_*_*_", register_service("PATCH", ("*", "*")))
     self.assertEqual(u"PATCH_*_*_", lookup_service_id("PATCH", "foo/bar"))
Esempio n. 10
0
 def test_service_id_for_multiple_media_types_is_none(self):
     register_service("GET", "application/json")
     self.assertEqual(
         None,
         lookup_service_id("GET", "application/json,application/javascipt"))
Esempio n. 11
0
 def test_register_wilcard_type(self):
     self.assertEqual(u'PATCH_*_*_', register_service('PATCH', ('*', '*')))
     self.assertEqual(u'PATCH_*_*_', lookup_service_id('PATCH', 'foo/bar'))
Esempio n. 12
0
 def test_register_wildcard_subtype(self):
     self.assertEqual(u'PATCH_text_*_',
                      register_service('PATCH', ('text', '*')))
     self.assertEqual(u'PATCH_text_*_',
                      lookup_service_id('PATCH', 'text/xml'))
Esempio n. 13
0
 def test_service_id_for_wildcard_subtype_is_none(self):
     register_service('GET', 'text/xml')
     self.assertEqual(None, lookup_service_id('GET', 'text/*'))
Esempio n. 14
0
 def test_service_id_for_wildcard_type_is_none(self):
     register_service('GET', 'application/json')
     self.assertEqual(None, lookup_service_id('GET', '*/*'))
Esempio n. 15
0
 def test_service_id_for_not_registered_media_type_is_none(self):
     self.assertEqual(None, lookup_service_id('PUT', 'text/html'))
Esempio n. 16
0
 def test_service_id_for_invalid_media_type_is_none(self):
     self.assertEqual(None, lookup_service_id('GET', 'application-json'))
Esempio n. 17
0
 def test_service_id_for_multiple_media_types_is_none(self):
     register_service('GET', 'application/json')
     self.assertEqual(None, lookup_service_id(
         'GET', 'application/json,application/javascipt'))
Esempio n. 18
0
 def test_register_wildcard_subtype(self):
     self.assertEqual(u"PATCH_text_*_",
                      register_service("PATCH", ("text", "*")))
     self.assertEqual(u"PATCH_text_*_",
                      lookup_service_id("PATCH", "text/xml"))
Esempio n. 19
0
 def test_register_media_type(self):
     self.assertEqual(u'GET_application_json_',
                      register_service('GET', ('application', 'json')))
     self.assertEqual(u'GET_application_json_',
                      lookup_service_id('GET', 'application/json'))