Beispiel #1
0
 def test_assert_xpath(self):
     response = http.get('http://blazedemo.com/')
     response.assert_ok()
     response.assert_xpath('//head/title',
                           parser_type='html',
                           validate=False)
     response.assert_not_xpath('//yo/my/man',
                               parser_type='html',
                               validate=False)
Beispiel #2
0
 def test_assert_regex(self):
     response = http.get('http://blazedemo.com/')
     response.assert_ok()
     response.assert_status_code(200)
     response.assert_regex_in_body('Welcome to the Simple Travel Agency!')
Beispiel #3
0
 def test_url_params(self):
     http.get('http://blazedemo.com/', params={'foo': 'bar'})
Beispiel #4
0
 def test_cookies(self):
     response = http.get('http://httpbin.org/cookies/set?name=value', cookies={"foo": "bar"})
     response.assert_ok()
Beispiel #5
0
 def test_assert_in_headers(self):
     response = http.get('http://blazedemo.com/')
     response.assert_in_headers("content-type: text/html")
Beispiel #6
0
 def test_assert_has_header(self):
     response = http.get('http://blazedemo.com/')
     response.assert_has_header("Content-Type")
Beispiel #7
0
 def test_assert_regex_in_body(self):
     response = http.get('http://blazedemo.com/')
     response.assert_regex_in_body("Welcome to the Simple .+ Agency")
Beispiel #8
0
 def test_assert_in_body(self):
     response = http.get('http://blazedemo.com/')
     response.assert_in_body("Welcome")
Beispiel #9
0
 def test_assert_regex_in_headers(self):
     response = http.get('http://blazedemo.com/')
     response.assert_regex_in_headers(r"content-type: .+")
Beispiel #10
0
 def test_assert_regex_not_in_headers(self):
     response = http.get('http://blazedemo.com/')
     response.assert_regex_not_in_headers(r"Content-Type: application/.+")
Beispiel #11
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)
Beispiel #12
0
 def test_body_json(self):
     http.get('http://blazedemo.com/', json={'foo': 'bar'})
Beispiel #13
0
 def test_body_string(self):
     http.get('http://blazedemo.com/', data='MY PERFECT BODY')
Beispiel #14
0
 def test_assert_status_code_in(self):
     response = http.get('http://blazedemo.com/')
     response.assert_status_code_in((302, 200))
Beispiel #15
0
 def test_assert_cssselect(self):
     response = http.get('http://blazedemo.com/')
     response.assert_ok()
     response.assert_cssselect('head title')
     response.assert_not_cssselect('yo my man')
Beispiel #16
0
 def test_assert_not_status_code(self):
     response = http.get('http://blazedemo.com/not-found')
     response.assert_not_status_code(200)
Beispiel #17
0
 def test_assert_jsonpath(self):
     response = http.get('https://jsonplaceholder.typicode.com/users')
     response.assert_ok()
     response.assert_jsonpath('$.[0].username', expected_value='Bret')
     response.assert_not_jsonpath("$.foo.bar")
Beispiel #18
0
 def test_assert_not_in_body(self):
     response = http.get('http://blazedemo.com/')
     response.assert_not_in_body("Willcommen!")
Beispiel #19
0
 def test_assert_2xx(self):
     response = http.get('http://blazedemo.com/')
     response.assert_2xx()
Beispiel #20
0
 def test_assert_regex_not_in_body(self):
     response = http.get('http://blazedemo.com/not-found')
     response.assert_regex_not_in_body("Nope")
Beispiel #21
0
 def test_assert_3xx(self):
     response = http.get('https://httpbin.org/status/301',
                         allow_redirects=False)
     response.assert_3xx()
Beispiel #22
0
 def test_assert_header_value(self):
     response = http.get('http://blazedemo.com/not-found')
     response.assert_header_value("Content-Type",
                                  "text/html; charset=UTF-8")
Beispiel #23
0
 def test_assert_4xx(self):
     response = http.get('http://blazedemo.com/not-found')
     response.assert_4xx()
Beispiel #24
0
 def test_get(self):
     http.get('http://blazedemo.com/?tag=get')
Beispiel #25
0
 def test_assert_5xx(self):
     response = http.get('https://httpbin.org/status/500')
     response.assert_5xx()
Beispiel #26
0
#  Apiritif - Python framework for API testing

# PyPi:  https://pypi.org/project/apiritif/
# Github: https://github.com/Blazemeter/apiritif


#  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
Beispiel #27
0
from apiritif import http

response = http.get("http://127.0.0.1:5000/ali")
print(response.text)