Esempio n. 1
0
 def test_put(self):
     http.put('http://blazedemo.com/?tag=put')
Esempio n. 2
0
#  HTTP Requests
# Apiritif allows to use simple requests-like API for making HTTP requests.

from apiritif import http

response = http.get("http://...")
response.assert_ok()  # will raise AssertionError if request wasn't successful


#  http object provides the following methods:

from apiritif import http

http.get("http://api.example.com/posts")
http.post("http://api.example.com/posts")
http.put("http://api.example.com/posts/1")
http.patch("http://api.example.com/posts/1")
http.delete("http://api.example.com/posts/1")
http.head("http://api.example.com/posts")
# All methods (get, post, put, patch, delete, head) support the following arguments:

def get(address,               # URL for the request
        params=None,           # URL params dict
        headers=None,          # HTTP headers
        cookies=None,          # request cookies
        data=None,             # raw request data
        json=None,             # attach JSON object as request body
        encrypted_cert=None,   # certificate to use with request 
        allow_redirects=True,  # automatically follow HTTP redirects
        timeout=30)            # request timeout, by default it's 30 seconds
Esempio n. 3
0
def api_test_assertions(method, url, logger, payload=''):

    # Gives useful information for debugging and narrowing down the kind of response code error.
    # Returns: N/A : Maybe later return the error message to the user.

    auth_data = ('admin', 'admin')
    header = {'X-Requested-By': 'ambari', 'Content-type': 'application/json'}
    method = method.upper()

    if (method == 'GET'):
        logger.info(
            "Start test asserts on the API GET Method for URL: {}".format(url))
        response = http.get(url, auth=auth_data, header=header)
    elif (method == 'POST'):
        logger.info(
            "Start test asserts on API POST method for URL: {}".format(url))
        response = http.post(url, auth=auth_data, headers=header, data=payload)
    elif (method == 'PUT'):
        logger.info(
            "Start test asserts on API PUT method for URL: {}".format(url))
        if payload != '':
            response = http.put(url,
                                auth=auth_data,
                                headers=header,
                                data=payload)
        else:
            response = http.put(url, auth=auth_data, headers=header)
    elif (method == 'DELETE'):
        logger.info(
            "Start test asserts on API DELETE method for URL: {}".format(url))
        response = http.delete(url, auth=auth_data, headers=header)

    # Another long if-elif, but this is needed to print back precise error and
    # save debugging time, by precise error failure cases.
    try:
        if (response.assert_2xx() or response.assert_status_code(200)):
            logger.info(
                "PASS: The response code was a Success. Expected Result: 2xx")
        elif (response.assert_3xx()):
            logger.error(
                "FAIL: Redirection error occured. Request failed. Actual Result: 3xx"
            )
        elif (response.assert_4xx()):
            logger.error(
                "FAIL: Client error occured. Check request sent. Actual Result: 4xx"
            )
        elif (response.assert_5xx()):
            logger.error(
                "FAIL: Server error occured. Server does not recognise request. Actual Result: 5xx"
            )
    except:
        logger.critical("Fail: Unexpected response code.\n")
        #sys.exit(1)

    # Use the response methods assert_header() like below to extend for all checks in the response header.
    # Test if OK is present in response.
    if (response.assert_ok()):
        logger.info("PASS: Response has OK as expected result.")
    else:
        logger.error("FAIL: Expected OK in the response not found.\n")

    # Validate the response request header contents.
    validate_header(response, logger)

    # Validate the response body with expected results.
    validate_body(response, logger)