コード例 #1
0
    def test_cache_expiration(self):
        with self.settings(RESTCLIENTS_DAO_CACHE_CLASS=CACHE):
            cache = BridgeAccountCache()
            ok_response = MockHTTP()
            ok_response.status = 200
            ok_response.data = "xx"
            url = '/group/uw_member/effective_member'

            response = cache.getCache('gws', url, {})
            self.assertEquals(response, None)

            cache.processResponse("gws", url, ok_response)
            response = cache.getCache("gws", url, {})
            self.assertEquals(response["response"].data, 'xx')

            cache_entry = CacheEntryTimed.objects.get(service='gws', url=url)
            orig_time_saved = cache_entry.time_saved
            cache_entry.time_saved = (orig_time_saved -
                                      timedelta(minutes=(60) - 2))
            cache_entry.save()
            response = cache.getCache("gws", url, {})
            self.assertNotEquals(response, None)

            cache_entry.time_saved = (orig_time_saved -
                                      timedelta(minutes=(60) + 1))
            cache_entry.save()
            response = cache.getCache("gws", url, {})
            self.assertEquals(response, None)
コード例 #2
0
    def get_response(obj, service, url):
        problems = PerformanceDegradation.get_problems()
        if not problems:
            return

        delay = problems.get_load_time(service)
        if delay:
            time.sleep(float(delay))

        status = problems.get_status(service)
        content = problems.get_content(service)

        if content and not status:
            status = 200

        if status:
            response = MockHTTP()
            response.status = int(status)

            if content:
                response.data = content

            return response

        return None
コード例 #3
0
 def test_edit_mock_response(self):
     response = MockHTTP()
     response.status = 404
     TrumbaSea_DAO()._edit_mock_response(
         'POST', "/service/calendars.asmx/GetPermissions",
         {"Content-Type": "application/json"}, '{"CalendarID": 1}',
         response)
     self.assertEqual(response.status, 200)
コード例 #4
0
    def load(self, method, url, headers, body):
        service = self._service_name

        cache_key = "%s-%s" % (service, url)
        value = get_cache_value(cache_key)
        if value:
            return value

        for path in self._get_mock_paths():
            response = load_resource_from_path(path, service, "file", url,
                                               headers)

            if response:
                set_cache_value(cache_key, response)
                return response

        response = MockHTTP()
        response.status = 404
        response.reason = "Not Found"

        set_cache_value(cache_key, response)
        return response
コード例 #5
0
def load_resource_from_path(resource_dir, service_name, implementation_name,
                            url, headers):

    RESOURCE_ROOT = os.path.join(resource_dir, service_name,
                                 implementation_name)

    if url == "///":
        # Just a placeholder to put everything else in an else.
        # If there are things that need dynamic work, they'd go here
        pass
    else:
        orig_file_path = RESOURCE_ROOT + url
        handle = open_file(orig_file_path)
        header_handle = open_file(orig_file_path + ".http-headers")

        # attempt to open query permutations even on success
        # so that if there are multiple files we throw an exception
        if "?" in url:
            handle = attempt_open_query_permutations(url, orig_file_path,
                                                     False)

        if "?" in url:
            header_handle = attempt_open_query_permutations(
                url, orig_file_path, True)

        if handle is None and header_handle is None:
            return None

        if handle is not None:
            data = handle.read()

        response = MockHTTP()
        response.status = 200
        if handle is not None:
            response.data = data
        response.headers = {
            "X-Data-Source": service_name + " file mock data",
        }

        if header_handle is not None:
            try:
                data = header_handle.read()
                file_values = json.loads(data)

                if "headers" in file_values:
                    response.headers.update(file_values['headers'])

                    if 'status' in file_values:
                        response.status = file_values['status']

                else:
                    response.headers.update(file_values)
            except UnicodeDecodeError:
                pass
            except IOError:
                pass

        return response
コード例 #6
0
 def process_response(self, headers, data):
     response = MockHTTP()
     response.status = int(headers['status'])
     response.data = data
     response.headers = headers
     return response
コード例 #7
0
 def load(self, method, url, headers, body):
     response = MockHTTP()
     response.status = 200
     response.data = "ok - %s" % method
     return response
コード例 #8
0
 def test_edit_mock_response(self):
     response = MockHTTP()
     response.status = 404
     Bridge_DAO()._edit_mock_response('DELETE', '/api/admin/users/195', {},
                                      None, response)
     self.assertEqual(response.status, 204)