Exemple #1
0
    def test_validation_image_url_good_url(self):
        try:
            param1 = {
                'url': urlBase + '/test1.png',
                'timeout': 1000,
                'verify_ssl': 0
            }
            expected1 = {
                'url': urlBase + '/test1.png',
                'timeout': 1000,
                'verify_ssl': False
            }
            result1, err = Validation.validate_check_image_url_param(
                param1, None)
            self.assertIsNone(err, 'Good url failed: %s' % (str(err)))
            self.assertIsNotNone(result1)

            self.assertIsInstance(result1, dict)
            self.assertDictEqual(result1, expected1)

        except Exception as ex:
            self.assertTrue(False)
Exemple #2
0
    def check_image_url(self, ctx, param):
        """
        :param param: instance of type "CheckImageURLParams" (Check image
           url) -> structure: parameter "url" of String, parameter "timeout"
           of Long, parameter "verify_ssl" of type "Boolean"
        :returns: multiple set - (1) parameter "result" of type
           "CheckImageURLResult" -> 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_image_url
        param, error = Validation.validate_check_image_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,
                                     verify=param['verify_ssl'],
                                     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('^image/')
            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 image'),
                    'type': 'value',
                    'code': 'error-response',
                    'info': {
                        'exception': str(ex)
                    }
                }
            ]
        #END check_image_url

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