simplify locust file

Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
hrj 2021-04-13 09:02:11 +05:30
parent 68dcfb1e49
commit c0ac570746

View File

@ -24,43 +24,41 @@ class QuickStartUser(SequentialTaskSet):
@task @task
def captcha(self): def captcha(self):
# TODO: Iterate over parameters for a more comprehensive test
captcha_params = {"level":"debug","media":"image/png","input_type":"text"} captcha_params = {"level":"debug","media":"image/png","input_type":"text"}
resp = self.client.post(path="/v1/captcha", json=captcha_params, name="/captcha") with self.client.post(path="/v1/captcha", json=captcha_params, name="/captcha", catch_response = True) as resp:
if resp.status_code != 200: if resp.status_code != 200:
print("\nError on /captcha endpoint: ") resp.failure("Status was not 200: " + resp.text)
print(resp) captchaJson = resp.json()
print(resp.text) uuid = captchaJson.get("id")
print("----------------END.CAPTCHA-------------------\n\n") if not uuid:
resp.failure("uuid not returned on /captcha endpoint: " + resp.text)
uuid = resp.json().get("id") with self.client.get(path="/v1/media?id=%s" % uuid, name="/media", stream=True, catch_response = True) as resp:
if resp.status_code != 200:
resp.failure("Status was not 200: " + resp.text)
resp = self.client.get(path="/v1/media?id=%s" % uuid, name="/media", stream=True) media = resp.content
if resp.status_code != 200:
print("\nError on /media endpoint: ")
print(resp)
print(resp.text)
print("----------------END.MEDIA-------------------\n\n")
media = resp.content ocrAnswer = self.solve(uuid, media)
mediaFileName = "tests/test-%s.png" % uuid
with open(mediaFileName, "wb") as f:
f.write(media)
ocrResult = subprocess.Popen("gocr %s" % mediaFileName, shell=True, stdout=subprocess.PIPE)
ocrAnswer = ocrResult.stdout.readlines()[0].strip().decode()
answerBody = {"answer": ocrAnswer,"id": uuid} answerBody = {"answer": ocrAnswer,"id": uuid}
with self.client.post(path='/v1/answer', json=answerBody, name="/answer", catch_response=True) as resp: with self.client.post(path='/v1/answer', json=answerBody, name="/answer", catch_response=True) as resp:
if resp.status_code != 200: if resp.status_code != 200:
print("\nError on /answer endpoint: ") resp.failure("Status was not 200: " + resp.text)
print(resp)
print(resp.text)
print("----------------END.ANSWER-------------------\n\n")
else: else:
if resp.json().get("result") != "True": if resp.json().get("result") != "True":
resp.failure("Answer was not accepted") resp.failure("Answer was not accepted")
def solve(self, uuid, media):
mediaFileName = "tests/test-%s.png" % uuid
with open(mediaFileName, "wb") as f:
f.write(media)
ocrResult = subprocess.Popen("gocr %s" % mediaFileName, shell=True, stdout=subprocess.PIPE)
ocrAnswer = ocrResult.stdout.readlines()[0].strip().decode()
return ocrAnswer
class User(FastHttpUser): class User(FastHttpUser):
wait_time = between(0.1,0.2) wait_time = between(0.1,0.2)