def test_datetime(): res = datetime.datetime(day=1, month=1, year=2011, hour=1, minute=1, second=1) assert json.dumps(res) == '"2011-01-01T01:01:01.000000Z"'
def get_config(self, current_version=None, keys=None): """ Gets configuration from a remote APM Server :param current_version: version of the current configuration :param keys: a JSON-serializable dict to identify this instance, e.g. { "service": { "name": "foo", "environment": "bar" } } :return: a three-tuple of new version, config dictionary and validity in seconds. Any element of the tuple can be None. """ url = self._config_url data = json_encoder.dumps(keys).encode("utf-8") headers = self._headers.copy() headers[b"Content-Type"] = "application/json" headers.pop(b"Content-Encoding", None) # remove gzip content-encoding header headers.update(self.auth_headers) max_age = 300 if current_version: headers["If-None-Match"] = current_version try: response = self.http.urlopen( "POST", url, body=data, headers=headers, timeout=self._timeout, preload_content=False ) except (urllib3.exceptions.RequestError, urllib3.exceptions.HTTPError) as e: logger.debug("HTTP error while fetching remote config: %s", compat.text_type(e)) return current_version, None, max_age body = response.read() if "Cache-Control" in response.headers: try: max_age = int(next(re.finditer(r"max-age=(\d+)", response.headers["Cache-Control"])).groups()[0]) except StopIteration: logger.debug("Could not parse Cache-Control header: %s", response.headers["Cache-Control"]) if response.status == 304: # config is unchanged, return logger.debug("Configuration unchanged") return current_version, None, max_age elif response.status >= 400: return None, None, max_age if not body: logger.debug("APM Server answered with empty body and status code %s", response.status) return current_version, None, max_age return response.headers.get("Etag"), json_encoder.loads(body.decode("utf-8")), max_age
def test_decimal(): res = decimal.Decimal("1.0") assert json.dumps(res) == "1.0"
def test_bytes(): if compat.PY2: res = bytes("foobar") else: res = bytes("foobar", encoding="ascii") assert json.dumps(res) == '"foobar"'
def test_frozenset(): res = frozenset(["foo", "bar"]) assert json.dumps(res) in ('["foo", "bar"]', '["bar", "foo"]')
def test_uuid(): res = uuid.uuid4() assert json.dumps(res) == '"%s"' % res.hex