Exemple #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
Exemple #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
Exemple #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
Exemple #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"))
Exemple #5
0
 def test_service_id_for_wildcard_type_is_none(self):
     register_service("GET", "application/json")
     self.assertEqual(None, lookup_service_id("GET", "*/*"))
Exemple #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/*"))
Exemple #7
0
 def test_service_id_for_invalid_media_type_is_none(self):
     self.assertEqual(None, lookup_service_id("GET", "application-json"))
Exemple #8
0
 def test_service_id_for_not_registered_media_type_is_none(self):
     self.assertEqual(None, lookup_service_id("PUT", "text/html"))
Exemple #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"))
Exemple #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"))
Exemple #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'))
Exemple #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'))
Exemple #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/*'))
Exemple #14
0
 def test_service_id_for_wildcard_type_is_none(self):
     register_service('GET', 'application/json')
     self.assertEqual(None, lookup_service_id('GET', '*/*'))
Exemple #15
0
 def test_service_id_for_not_registered_media_type_is_none(self):
     self.assertEqual(None, lookup_service_id('PUT', 'text/html'))
Exemple #16
0
 def test_service_id_for_invalid_media_type_is_none(self):
     self.assertEqual(None, lookup_service_id('GET', 'application-json'))
Exemple #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'))
Exemple #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"))
Exemple #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'))