예제 #1
0
파일: exc.py 프로젝트: Hunsu/Rester
    def _execute_test_step(self, test_step):
        http_client = HttpClient(**self.case.request_opts)
        failures = Failure([], None)
        try:
            method = getattr(test_step, 'method', 'get')
            data = getattr(test_step, 'data', None)
            is_raw = getattr(test_step, 'raw', False)
            self.logger.info('\n=======> Executing TestStep : %s, method : %s', test_step.name, method)

            # process and set up headers
            headers = {}
            if hasattr(test_step, 'headers') and test_step.headers is not None:
                self.logger.debug('Found Headers')
                for key, value in test_step.headers.items().items():
                    headers[key] = self.case.variables.expand(value)

            # process and set up params
            params = self._build_param_dict(test_step)

            url = self.case.variables.expand(test_step.apiUrl)
            self.logger.debug('Evaluated URL : %s', url)
            response_wrapper = http_client.request(url, method, headers, params, data, is_raw)
            
            if hasattr(test_step, 'log'):
                self._log(test_step, test_step.log, response_wrapper)

            if hasattr(test_step, "asserts"):
                asserts = test_step.asserts
                if hasattr(asserts, 'status'):
                    expected_status = getattr(asserts, status)
                    if response_wrapper.status != expected_status:
                        failures.errors.append("status(%s) != expected status(%s)" % (response_wrapper.status, expected_status))
                if hasattr(asserts, "headers"):
                    self._assert_element_list('Header', failures, test_step, response_wrapper.headers, test_step.asserts.headers.items().items())

                if hasattr(asserts, "payload"):
                    self.logger.debug('Evaluating Response Payload')
                    self._assert_element_list('Payload', failures, test_step, response_wrapper.body, test_step.asserts.payload.items().items())
            else:
                self.logger.warn('\n=======> No "asserts" element found in TestStep %s', test_step.name)

        except Exception as inst:
            failures.errors.append(traceback.format_exc())
            self.logger.error('ERROR !!! TestStep %s Failed to execute !!!  %s \
                        \n !!! Will ignore all assignment statements as part of TestStep', test_step.name, inst)
            self.logger.exception('Exception')

        if failures.errors:
            return failures
        
        # execute all the assignment statements
        if hasattr(test_step, 'postAsserts') and test_step.postAsserts is not None:          
            for key, value in test_step.postAsserts.items().items():
                self._process_post_asserts(response_wrapper.body, key, value)

        return None
예제 #2
0
class TestHttpClient(object):
    """
    Fixture allowing HttpClient Unit test with HttpBin
    """
    def setup(self):
        self.client = HttpClient()
        pass

    def teardown(self):
        pass

    def test_get_request(self):
        url = "http://httpbin.org"
        response = self.client.request(url, "get", {}, {}, False)
        assert_equal(response.status, 200)
예제 #3
0
class TestHttpClient(object):
    """
    Fixture allowing HttpClient Unit test with HttpBin
    """
    def setup(self):
        self.client = HttpClient()
        pass

    def teardown(self):
        pass

    def test_get_request(self):
        url = "http://httpbin.org"
        response = self.client.request(url, "get", {}, {}, False)
        assert_equal(response.status, 200)
예제 #4
0
파일: exc.py 프로젝트: hsiang0107/python
    def _execute_test_step(self, test_step):
        http_client = HttpClient(**self.case.request_opts)
        failures = Failure([], None)
        try:
            method = getattr(test_step, 'method', 'get')
            is_raw = getattr(test_step, 'raw', False)
            self.logger.info('\n=======> Executing TestStep : %s, method : %s',
                             test_step.name, method)

            # process and set up headers
            headers = {}
            if hasattr(test_step, 'headers') and test_step.headers is not None:
                self.logger.debug('Found Headers')
                for key, value in test_step.headers.items().items():
                    headers[key] = self.case.variables.expand(value)

            # process and set up params
            params = self._build_param_dict(test_step)

            url = self.case.variables.expand(test_step.apiUrl)
            self.logger.debug('Evaluated URL : %s', url)
            response_wrapper = http_client.request(url, method, headers,
                                                   params, is_raw)

            # expected_status = getattr(getattr(test_step, 'asserts'), 'status', 200)
            # if response_wrapper.status != expected_status:
            #     failures.errors.append("status(%s) != expected status(%s)" % (response_wrapper.status, expected_status))

            if hasattr(test_step, "asserts"):
                asserts = test_step.asserts
                if hasattr(asserts, "headers"):
                    self._assert_element_list(
                        'Header', failures, test_step,
                        response_wrapper.headers,
                        test_step.asserts.headers.items().items())

                if hasattr(asserts, "payload"):
                    self.logger.debug('Evaluating Response Payload')
                    self._assert_element_list(
                        'Payload', failures, test_step, response_wrapper.body,
                        test_step.asserts.payload.items().items())
            else:
                self.logger.warn(
                    '\n=======> No "asserts" element found in TestStep %s',
                    test_step.name)

        except Exception as inst:
            failures.errors.append(traceback.format_exc())
            self.logger.error(
                'ERROR !!! TestStep %s Failed to execute !!!  %s \
                        \n !!! Will ignore all assignment statements as part of TestStep',
                test_step.name, inst)
            self.logger.exception('Exception')

        if failures.errors:
            return failures

        # execute all the assignment statements
        if hasattr(test_step,
                   'postAsserts') and test_step.postAsserts is not None:
            for key, value in test_step.postAsserts.items().items():
                self._process_post_asserts(response_wrapper.body, key, value)

        return None