mirror of
https://github.com/QingdaoU/JudgeServer.git
synced 2024-12-29 14:11:44 +00:00
cbcc9cb313
token获取方式改为环境变量
48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
# coding=utf-8
|
|
from __future__ import unicode_literals
|
|
from os import sys, path
|
|
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
|
|
|
|
import json
|
|
import unittest
|
|
import requests
|
|
|
|
|
|
from client.Python.client import JudgeServerClient, JudgeServerClientError
|
|
|
|
|
|
class JudgeServerClientForTokenHeaderTest(JudgeServerClient):
|
|
def _request(self, url, data=None):
|
|
kwargs = {"headers": {"Content-Type": "application/json"}}
|
|
if data:
|
|
kwargs["data"] = json.dumps(data)
|
|
try:
|
|
return requests.post(url, **kwargs).json()
|
|
except Exception as e:
|
|
raise JudgeServerClientError(e.message)
|
|
|
|
|
|
class JudgeServerTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.token = "YOUR_TOKEN_HERE"
|
|
self.server_base_url = "http://127.0.0.1:12358"
|
|
self.client = JudgeServerClient(token=self.token, server_base_url=self.server_base_url)
|
|
|
|
def test_success(self):
|
|
data = self.client.ping()
|
|
self.assertEqual(data["err"], None)
|
|
self.assertEqual(data["data"]["action"], "pong")
|
|
|
|
def test_invalid_token(self):
|
|
client = JudgeServerClient(token="wrong token", server_base_url=self.server_base_url)
|
|
data = client.ping()
|
|
self.assertEqual(data["err"], "TokenVerificationFailed")
|
|
|
|
def test_no_token_header(self):
|
|
client = JudgeServerClientForTokenHeaderTest(token="wrong token", server_base_url=self.server_base_url)
|
|
data = client.ping()
|
|
self.assertEqual(data["err"], "TokenVerificationFailed")
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|