Skip to content

Commit

Permalink
Fix #11, print info if there are problems with the request file
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Slabchenko committed Dec 8, 2023
1 parent d9820e2 commit 639a367
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
3 changes: 3 additions & 0 deletions pyapitester/httpresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class HttpResponse:
Exception: Optional[str]
"""Exception during request processing, None if there is no exception"""

ExceptionDetails: Optional[str]
"""Exception details, None if there is no exception"""

Json: Optional[Dict]

Result: bool
Expand Down
62 changes: 33 additions & 29 deletions pyapitester/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pyapitester.httpresponse import HttpResponse
from pyapitester.helpers import AppLogger, Environment, AppState
import os
import sys
import json


Expand All @@ -32,44 +33,44 @@ def run(self):

for req in self.requests:

req.prepare(self.env.env_vars)
res = HttpResponse()

AppLogger.buffering_start()
try:
req.prepare(self.env.env_vars)

folder = os.path.dirname(req.Path)
AppLogger.buffering_start()

# Put our user-agent if missing
if "User-Agent" not in req.Headers:
req.Headers["User-Agent"] = "PyApiTester/0.1"
folder = os.path.dirname(req.Path)

if len(req.PreRequestScript) > 0:
AppLogger.log('Executing a pre-request script')
# Put our user-agent if missing
if "User-Agent" not in req.Headers:
req.Headers["User-Agent"] = "PyApiTester/0.1"

exec(req.PreRequestScript, {
"req": req,
"EnvVars": self.env.env_vars,
"AppLogger": AppLogger,
"AppState": AppState
}, None)
if len(req.PreRequestScript) > 0:
AppLogger.log('Executing a pre-request script')

# If session is needed
if req.Session:
# If session doesn't exist - initialize a new one
if session is None:
session = requests.Session()
rq = session
else:
# Session is not needed, close if there is one
if session is not None:
session.close()
session = None
rq = requests.Session()
exec(req.PreRequestScript, {
"req": req,
"EnvVars": self.env.env_vars,
"AppLogger": AppLogger,
"AppState": AppState
}, None)

res = HttpResponse()
# If session is needed
if req.Session:
# If session doesn't exist - initialize a new one
if session is None:
session = requests.Session()
rq = session
else:
# Session is not needed, close if there is one
if session is not None:
session.close()
session = None
rq = requests.Session()

rq.max_redirects = req.MaxRedirects
rq.max_redirects = req.MaxRedirects

try:
if req.Body.Type == HttpRequest.BodyType.MULTIPART:
multipart_fields = {}
for entry in req.Body.Multipart:
Expand Down Expand Up @@ -112,6 +113,7 @@ def run(self):

except Exception as ex:
res.Exception = type(ex).__name__
res.ExceptionDetails = str(sys.exc_info()[1])

# Go through expected statuses to check if response is OK
# TODO: By default the response is considered to be OK
Expand All @@ -136,6 +138,8 @@ def run(self):
res.ResultValue = res.Status

AppLogger.log_header(res.Result, f'Processing {req.Path}', str(res.ResultValue))
if res.ExceptionDetails is not None:
AppLogger.log(message=res.ExceptionDetails,level=logging.ERROR)

AppState.add_request_result(res.Result)

Expand Down

0 comments on commit 639a367

Please sign in to comment.