def check_response(self, response, status): self.assertEqual(response.content_type, 'application/json') self.assertEqual(response.charset, 'UTF-8') if status == 'ok': self.assertEqual(response.json, self.ip_response) elif status == 'invalid_key': self.assertEqual(response.json, InvalidAPIKey.json_body()) elif status == 'not_found': self.assertEqual(response.json, self.not_found.json_body()) elif status == 'parse_error': self.assertEqual(response.json, ParseError.json_body()) elif status == 'limit_exceeded': self.assertEqual(response.json, DailyLimitExceeded.json_body())
def check_response(self, response, status): self.assertEqual(response.content_type, 'application/json') self.assertEqual(response.charset, 'UTF-8') self.assertEqual(response.headers['Access-Control-Allow-Origin'], '*') self.assertEqual(response.headers['Access-Control-Max-Age'], '2592000') if status == 'ok': self.assertEqual(response.json, self.ip_response) elif status == 'invalid_key': self.assertEqual(response.json, InvalidAPIKey.json_body()) elif status == 'not_found': self.assertEqual(response.json, self.not_found.json_body()) elif status == 'parse_error': self.assertEqual(response.json, ParseError.json_body()) elif status == 'limit_exceeded': self.assertEqual(response.json, DailyLimitExceeded.json_body())
def check_response(self, response, status): self.assertEqual(response.content_type, "application/json") self.assertEqual(response.charset, "UTF-8") self.assertEqual(response.headers["Access-Control-Allow-Origin"], "*") self.assertEqual(response.headers["Access-Control-Max-Age"], "2592000") if status == "ok": self.assertEqual(response.json, self.ip_response) elif status == "invalid_key": self.assertEqual(response.json, InvalidAPIKey.json_body()) elif status == "not_found": self.assertEqual(response.json, self.not_found.json_body()) elif status == "parse_error": self.assertEqual(response.json, ParseError.json_body()) elif status == "limit_exceeded": self.assertEqual(response.json, DailyLimitExceeded.json_body())
def check_response(self, response, status): self.assertEqual(response.content_type, 'application/json') self.assertEqual(response.charset, 'UTF-8') self.assertEqual(response.headers['Access-Control-Allow-Origin'], '*') self.assertEqual(response.headers['Access-Control-Max-Age'], '2592000') if status == 'ok': self.assertEqual(response.json, self.ip_response) elif status == 'invalid_key': self.assertEqual(response.json, InvalidAPIKey.json_body()) elif status == 'not_found': self.assertEqual(response.json, self.not_found.json_body()) elif status == 'parse_error': self.assertEqual(response.json, ParseError.json_body()) elif status == 'limit_exceeded': self.assertEqual(response.json, DailyLimitExceeded.json_body())
def check_response(self, data_queues, response, status): assert response.content_type == 'application/json' assert response.headers['Access-Control-Allow-Origin'] == '*' assert response.headers['Access-Control-Max-Age'] == '2592000' if status == 'ok': assert response.json == self.ip_response elif status == 'invalid_key': assert response.json == InvalidAPIKey.json_body() elif status == 'not_found': assert response.json == self.not_found.json_body() elif status == 'parse_error': assert response.json == ParseError.json_body() elif status == 'limit_exceeded': assert response.json == DailyLimitExceeded.json_body() if status != 'ok': self.check_queue(data_queues, 0)
def test_api_key_limit(self): london = self.geoip_data['London'] api_key = uuid1().hex self.session.add(ApiKey(valid_key=api_key, maxreq=5, shortname='dis')) self.session.flush() # exhaust today's limit dstamp = util.utcnow().strftime('%Y%m%d') key = 'apilimit:%s:%s' % (api_key, dstamp) self.redis_client.incr(key, 10) res = self.app.post_json( '%s?key=%s' % (self.url, api_key), {}, extra_environ={'HTTP_X_FORWARDED_FOR': london['ip']}, status=403) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, DailyLimitExceeded.json_body())
def check_response(self, data_queues, response, status, fallback=None): assert response.content_type == 'application/json' assert response.headers['Access-Control-Allow-Origin'] == '*' assert response.headers['Access-Control-Max-Age'] == '2592000' if status == 'ok': body = dict(response.json) if fallback: assert body['fallback'] == fallback del body['fallback'] assert body == self.ip_response elif status == 'invalid_key': assert response.json == InvalidAPIKey.json_body() elif status == 'not_found': assert response.json == self.not_found.json_body() elif status == 'parse_error': assert response.json == ParseError.json_body() elif status == 'limit_exceeded': assert response.json == DailyLimitExceeded.json_body() if status != 'ok': self.check_queue(data_queues, 0)
def check_response(self, data_queues, response, status, fallback=None): assert response.content_type == "application/json" assert response.headers["Access-Control-Allow-Origin"] == "*" assert response.headers["Access-Control-Max-Age"] == "2592000" if status == "ok": body = dict(response.json) if fallback: assert body["fallback"] == fallback del body["fallback"] assert body == self.ip_response elif status == "invalid_key": assert response.json == InvalidAPIKey.json_body() elif status == "not_found": assert response.json == self.not_found.json_body() elif status == "parse_error": assert response.json == ParseError.json_body() elif status == "limit_exceeded": assert response.json == DailyLimitExceeded.json_body() if status != "ok": self.check_queue(data_queues, 0)