from falcon.testing import StartResponseMock, create_environ
from falcon import HTTP_METHODS
from urllib.parse import urlencode
import json
from hug.run import server
from functools import partial


def call(method, api_module, url, body='', headers=None, **params):
    api = server(api_module)
    response = StartResponseMock()
    result = api(create_environ(path=url, method=method, headers=headers, query_string=urlencode(params)), response)
    if result:
        return json.loads(result[0].decode('utf8'))
    else:
        return response.status


for method in HTTP_METHODS:
    globals()[method.lower()] = partial(call, method)

from falcon.testing import StartResponseMock, create_environ
from falcon import HTTP_METHODS
from urllib.parse import urlencode
import json
from hug.run import server
from functools import partial


def call(method, api_module, url, body='', headers=None, **params):
    api = server(api_module)
    response = StartResponseMock()
    result = api(create_environ(path=url, method=method, headers=headers, query_string=urlencode(params), body=body),
                 response)
    if result:
        response.data = result[0].decode('utf8')
        response.content_type = response.headers_dict['content-type']
        if response.content_type == 'application/json':
            response.data = json.loads(response.data)
    return response


for method in HTTP_METHODS:
    globals()[method.lower()] = partial(call, method)

Update call method to return entire response object, and work with non json data
Update call method to return entire response object, and work with non json data

  from falcon.testing import StartResponseMock, create_environ
  from falcon import HTTP_METHODS
  from urllib.parse import urlencode
  import json
  from hug.run import server
  from functools import partial
  
  
  def call(method, api_module, url, body='', headers=None, **params):
      api = server(api_module)
      response = StartResponseMock()
-     result = api(create_environ(path=url, method=method, headers=headers, query_string=urlencode(params)), response)
+     result = api(create_environ(path=url, method=method, headers=headers, query_string=urlencode(params), body=body),
+                  response)
      if result:
-         return json.loads(result[0].decode('utf8'))
+         response.data = result[0].decode('utf8')
-     else:
+         response.content_type = response.headers_dict['content-type']
+         if response.content_type == 'application/json':
+             response.data = json.loads(response.data)
-         return response.status
+     return response
  
  
  for method in HTTP_METHODS:
      globals()[method.lower()] = partial(call, method)
  
Update call method to return entire response object, and work with non json data
## Code Before:
from falcon.testing import StartResponseMock, create_environ
from falcon import HTTP_METHODS
from urllib.parse import urlencode
import json
from hug.run import server
from functools import partial


def call(method, api_module, url, body='', headers=None, **params):
    api = server(api_module)
    response = StartResponseMock()
    result = api(create_environ(path=url, method=method, headers=headers, query_string=urlencode(params)), response)
    if result:
        return json.loads(result[0].decode('utf8'))
    else:
        return response.status


for method in HTTP_METHODS:
    globals()[method.lower()] = partial(call, method)

## Instruction:
Update call method to return entire response object, and work with non json data
## Code After:
from falcon.testing import StartResponseMock, create_environ
from falcon import HTTP_METHODS
from urllib.parse import urlencode
import json
from hug.run import server
from functools import partial


def call(method, api_module, url, body='', headers=None, **params):
    api = server(api_module)
    response = StartResponseMock()
    result = api(create_environ(path=url, method=method, headers=headers, query_string=urlencode(params), body=body),
                 response)
    if result:
        response.data = result[0].decode('utf8')
        response.content_type = response.headers_dict['content-type']
        if response.content_type == 'application/json':
            response.data = json.loads(response.data)
    return response


for method in HTTP_METHODS:
    globals()[method.lower()] = partial(call, method)
