def test_validation_home_url_good_param(self):
        try:
            param = {
                'url': urlBase + '/test1.html',
                'timeout': 1000
            }
            expected = {
                'url': urlBase + '/test1.html',
                'timeout': 1000
            }
            result, err = Validation.validate_check_html_url_param(param, None)
            self.assertIsNone(err, 'Good param failed: %s' % (str(err)))
            self.assertIsNotNone(result)
            
            self.assertIsInstance(result, dict)
            self.assertDictEqual(result, expected)

        except Exception as ex:
            self.assertTrue(False, 'Unexpected exception: %s' % str(ex))
    def test_validation_home_url_missing_url_param(self):
        try:
            param = {
                'timeout': 1000
            }
            expected = { 
                'message': 'the required parameter "url" was not provided', 
                'type': 'input', 
                'code': 'missing', 
                'info': { 
                    'key': 'url' 
                } 
            } 
            result, err = Validation.validate_check_html_url_param(param, None)
            self.assertIsNone(result)
            self.assertIsNotNone(err)
            
            self.assertIsInstance(err, dict)
            self.assertDictEqual(err, expected)

        except Exception as ex:
            self.assertTrue(False, 'Unexpected exception: %s' % str(ex))
Example #3
0
    def check_html_url(self, ctx, param):
        """
        :param param: instance of type "CheckHTMLURLParams" (Check html url)
           -> structure: parameter "url" of String, parameter "timeout" of
           Long
        :returns: multiple set - (1) parameter "result" of type
           "CheckHTMLURLResult" -> structure: parameter "is_valid" of type
           "Boolean", parameter "error" of type "CheckError" -> structure:
           parameter "code" of String, parameter "info" of unspecified
           object, (2) parameter "error" of type "Error" -> structure:
           parameter "message" of String, parameter "type" of String,
           parameter "code" of String, parameter "info" of unspecified object
        """
        # ctx is the context object
        # return variables are: result, error
        #BEGIN check_html_url
        param, error = Validation.validate_check_html_url_param(param, ctx)
        if error:
            return [None, error]

        try:
            header = {'User-Agent': self.USER_AGENT_STRING}
            # header = {
            #     'User-Agent': ''
            # }
            timeout = param['timeout'] / 1000
            response = requests.head(url=param['url'],
                                     allow_redirects=True,
                                     timeout=timeout,
                                     headers=header)
            if response.status_code == 404:
                return [{
                    'is_valid': False,
                    'error': {
                        'code': 'not-found'
                    }
                }, None]

            if response.status_code not in [200, 301, 302]:
                return [{
                    'is_valid': False,
                    'error': {
                        'code': 'unexpected-response-status-code',
                        'info': {
                            'status_code': response.status_code
                        }
                    }
                }, None]

            if 'content-type' not in response.headers:
                return [{
                    'is_valid': False,
                    'error': {
                        'code': 'missing-content-type',
                        'info': {}
                    }
                }, None]

            content_type = response.headers['content-type']
            image_re = re.compile('^text/html')
            if not re.match(image_re, content_type):
                return [{
                    'is_valid': False,
                    'error': {
                        'code': 'invalid-content-type',
                        'info': {
                            'content_type': content_type
                        }
                    }
                }, None]

            return [{
                'is_valid': True,
            }, None]

        except Exception as ex:
            return [
                None, {
                    'message': ('exception requesting html page'),
                    'type': 'value',
                    'code': 'error-response',
                    'info': {
                        'exception': str(ex)
                    }
                }
            ]
        #END check_html_url

        # At some point might do deeper type checking...
        if not isinstance(result, dict):
            raise ValueError('Method check_html_url return value ' +
                             'result is not type dict as required.')
        if not isinstance(error, dict):
            raise ValueError('Method check_html_url return value ' +
                             'error is not type dict as required.')
        # return the results
        return [result, error]