def perform_test(testfile, output_format, dryrun):
    try:
        with open(testfile, 'r') as stream:
            test_data = yaml.safe_load(stream)
    except:
        exit_with_errror('ERROR: cannot open or parse file')
    if (dryrun == 'yes'):
        output((testfile + ' successfully loaded.'))
    majime_base = test_data['Base']
    majime_host = majime_base.split('//')[(- 1)].split('/')[0].split('?')[0]
    tests_run = 0
    tests_successful = 0
    tests_failed = 0
    json_out = {}
    json_out['tests'] = []
    json_out['output'] = {}
    for majime_test in test_data['Tests']:
        majime_url = (majime_base + majime_test['path'])
        majime_queryparams = dict(parse.parse_qsl(parse.urlsplit(majime_url).query))
        majime_baseurl = majime_url.split('?')[0].split('#')[0]
        majime_method = majime_test['method']
        try:
            majime_ctype = majime_test['content-type']
        except:
            majime_ctype = ''
        try:
            majime_payload1 = majime_test['body']
            majime_payload = json.dumps(majime_payload1)
        except:
            majime_payload = ''
        majime_expect_response = majime_test['expect-response']
        headers = {'User-Agent': ('majime-%s' % version)}
        if (majime_ctype != ''):
            headers['Content-Type'] = majime_ctype
        try:
            response = requests.request(majime_method, majime_baseurl, data=majime_payload, params=majime_queryparams, headers=headers)
        except:
            print('Fatal error when running test. Is the endpoint reachable?')
            sys.exit(1)
        code = response.status_code
        tests_run += 1
        if (str(majime_expect_response) == str(code)):
            tests_successful += 1
            json_out['tests'].append(dict(method=majime_method, url=majime_url, http_response=str(code), result='OK'))
        else:
            tests_failed += 1
            json_out['tests'].append(dict(method=majime_method, url=majime_url, http_response=str(code), http_expected_response=majime_expect_response, result='FAIL'))
    if (tests_failed == 0):
        json_out['output']['overall_result'] = 'OK'
    else:
        json_out['output']['overall_result'] = 'FAIL'
    json_out['output']['successful-tests'] = str(tests_successful)
    json_out['output']['failed-tests'] = str(tests_failed)
    if (output_format == 'json'):
        print(str(json_out).replace("'", '"'))
    else:
        for t in json_out['tests']:
            print(('%s %s' % (t['method'], t['url'])))
            if (t['result'] == 'OK'):
                print(colored(('HTTP ' + str(t['http_response'])), 'green'))
            else:
                print(colored(((('HTTP ' + str(t['http_response'])) + ' but expected ') + str(t['http_expected_response'])), 'red'))
        print(('%s tests, %s successful and %s failed' % (tests_run, tests_successful, tests_failed)))
    if (tests_failed == 0):
        sys.exit(0)
    else:
        sys.exit(1)
def perform_test(testfile, output_format, dryrun):
    try:
        with open(testfile, 'r') as stream:
            test_data = yaml.safe_load(stream)
    except:
        exit_with_errror('ERROR: cannot open or parse file')
    if (dryrun == 'yes'):
        output((testfile + ' successfully loaded.'))
    majime_base = test_data['Base']
    majime_host = majime_base.split('//')[(- 1)].split('/')[0].split('?')[0]
    tests_run = 0
    tests_successful = 0
    tests_failed = 0
    json_out = {}
    json_out['tests'] = []
    json_out['output'] = {}
    for majime_test in test_data['Tests']:
        majime_url = (majime_base + majime_test['path'])
        majime_queryparams = dict(parse.parse_qsl(parse.urlsplit(majime_url).query))
        majime_baseurl = majime_url.split('?')[0].split('#')[0]
        majime_method = majime_test['method']
        try:
            majime_ctype = majime_test['content-type']
        except:
            majime_ctype = 
        try:
            majime_payload1 = majime_test['body']
            majime_payload = json.dumps(majime_payload1)
        except:
            majime_payload = 
        majime_expect_response = majime_test['expect-response']
        headers = {'User-Agent': ('majime-%s' % version)}
        if (majime_ctype != ):
            headers['Content-Type'] = majime_ctype
        try:
            response = requests.request(majime_method, majime_baseurl, data=majime_payload, params=majime_queryparams, headers=headers)
        except:
            print('Fatal error when running test. Is the endpoint reachable?')
            sys.exit(1)
        code = response.status_code
        tests_run += 1
        if (str(majime_expect_response) == str(code)):
            tests_successful += 1
            json_out['tests'].append(dict(method=majime_method, url=majime_url, http_response=str(code), result='OK'))
        else:
            tests_failed += 1
            json_out['tests'].append(dict(method=majime_method, url=majime_url, http_response=str(code), http_expected_response=majime_expect_response, result='FAIL'))
    if (tests_failed == 0):
        json_out['output']['overall_result'] = 'OK'
    else:
        json_out['output']['overall_result'] = 'FAIL'
    json_out['output']['successful-tests'] = str(tests_successful)
    json_out['output']['failed-tests'] = str(tests_failed)
    if (output_format == 'json'):
        print(str(json_out).replace("'", '"'))
    else:
        for t in json_out['tests']:
            print(('%s %s' % (t['method'], t['url'])))
            if (t['result'] == 'OK'):
                print(colored(('HTTP ' + str(t['http_response'])), 'green'))
            else:
                print(colored(((('HTTP ' + str(t['http_response'])) + ' but expected ') + str(t['http_expected_response'])), 'red'))
        print(('%s tests, %s successful and %s failed' % (tests_run, tests_successful, tests_failed)))
    if (tests_failed == 0):
        sys.exit(0)
    else:
        sys.exit(1)