Esempio n. 1
0
    def test_urinfo_timeout_get(self):
        """Test that a timeout error during urinfo returns False"""
        uri = 'http://255.255.255.255/'

        # test error during GET
        responses.add(responses.GET, uri, body=requests.Timeout())
        result = urinfo(uri)
        self.assertEqual(result, False)
Esempio n. 2
0
    def test_urinfo_http_error_head(self):
        """Test that a http error during urinfo returns False"""
        uri = 'http://255.255.255.255/'

        # test error during HEAD
        responses.add(responses.HEAD, uri, body=requests.HTTPError())
        result = urinfo(uri)
        self.assertEqual(result, False)
Esempio n. 3
0
    def test_urinfo_connection_error_get(self):
        """Test that a connection error during urinfo returns False"""
        uri = 'http://255.255.255.255/'

        # test error during GET
        responses.add(responses.GET, uri, body=requests.ConnectionError())
        result = urinfo(uri)
        self.assertEqual(result, False)
Esempio n. 4
0
def fetch():
    uri = request.args.get('uri')

    # abort with 404 if uri missing from query params
    if uri == None:
        abort(404)

    # abort with 404 if uri is not a HTTP(s) uri
    if not uri.lower().startswith("http"):
        abort(404)

    # get urinfo
    info = urinfo(uri)

    # if info is False or None, abort with 404
    if info == None or info == False:
        abort(404)

    return jsonify(**info)
Esempio n. 5
0
    def test_urinfo_success(self):
        """Test fetching a url on the happy path"""

        uri = 'http://255.255.255.255/'

        def head_callback(req):
            body = 'something'
            headers = {'content-type': 'text/html'}
            return (200, headers, body)

        def get_callback(req):
            body = '<!doctype html><html><head><title>Test Title</title></head></html>'
            headers = {'content-type': 'text/html'}
            return (200, headers, body)

        responses.add_callback(responses.HEAD, uri, callback=head_callback, content_type='text/html')
        responses.add_callback(responses.GET,  uri, callback=get_callback,  content_type='text/html')

        result = urinfo(uri)
        self.assertEqual(result['uri'], uri)
        self.assertEqual(result['title'], 'Test Title')