|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import inspect |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import urllib |
| 8 | + |
| 9 | +# prefer local copy to the one which is installed |
| 10 | +# hack from http://stackoverflow.com/a/6098238/280539 |
| 11 | +_top_level_path = os.path.realpath(os.path.abspath(os.path.join( |
| 12 | + os.path.split(inspect.getfile(inspect.currentframe()))[0], |
| 13 | + ".." |
| 14 | +))) |
| 15 | +if _top_level_path not in sys.path: |
| 16 | + sys.path.insert(0, _top_level_path) |
| 17 | +# end of hack |
| 18 | + |
| 19 | +from SPARQLWrapper import SPARQLWrapper, XML, GET, SELECT |
| 20 | +import unittest |
| 21 | +from urllib2 import URLError |
| 22 | +import socket |
| 23 | +import errno |
| 24 | + |
| 25 | +endpoint = "http://dbpedia.org/sparql" |
| 26 | +endpoint_ssl = "https://dbpedia.org/sparql" |
| 27 | + |
| 28 | +prefixes = """ |
| 29 | + PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 30 | + PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 31 | +""" |
| 32 | + |
| 33 | +selectQuery = """ |
| 34 | + SELECT ?label |
| 35 | + WHERE { |
| 36 | + <http://dbpedia.org/resource/Asturias> rdfs:label ?label . |
| 37 | + } |
| 38 | +""" |
| 39 | + |
| 40 | +proxiesWorking = { |
| 41 | + "http":"177.22.107.236:8080", |
| 42 | + "https":"177.22.107.236:8080"} # Checked using http://www.proxy-checker.org/ |
| 43 | + |
| 44 | +proxiesNotWorking = {"http":"127.0.0.1:80", |
| 45 | + "https":"127.0.0.1:80"} |
| 46 | + |
| 47 | +class SPARQLWrapperProxyTests(unittest.TestCase): |
| 48 | + |
| 49 | + def __generic(self, endpoint): |
| 50 | + sparql = SPARQLWrapper(endpoint) |
| 51 | + sparql.setQuery(prefixes + selectQuery) |
| 52 | + sparql.setReturnFormat(XML) |
| 53 | + sparql.setMethod(GET) |
| 54 | + return sparql |
| 55 | + |
| 56 | + def testProxyWorking(self): |
| 57 | + sparql = self.__generic(endpoint) |
| 58 | + sparql.setProxies(proxiesWorking) |
| 59 | + self.assertTrue(sparql.proxies) # assert no empty |
| 60 | + |
| 61 | + try: |
| 62 | + result = sparql.query() |
| 63 | + except URLError as error: |
| 64 | + print "The Proxy server is not responding" |
| 65 | + print error # Because we are not sure that the proxy is working |
| 66 | + else: |
| 67 | + result.convert().toprettyxml() |
| 68 | + self.assertTrue(True) |
| 69 | + |
| 70 | + def testProxyWorkingSSL(self): |
| 71 | + sparql = self.__generic(endpoint_ssl) |
| 72 | + self.assertEqual(sparql.endpoint, endpoint_ssl) |
| 73 | + sparql.setProxies(proxiesWorking) |
| 74 | + self.assertTrue(sparql.proxies) # assert no empty |
| 75 | + |
| 76 | + try: |
| 77 | + result = sparql.query() |
| 78 | + except URLError as error: |
| 79 | + print "The Proxy server is not responding" |
| 80 | + print error # Because we are not sure that the proxy is working |
| 81 | + else: |
| 82 | + result.convert().toprettyxml() |
| 83 | + self.assertTrue(True) |
| 84 | + |
| 85 | + def testProxyNotWorking(self): |
| 86 | + sparql = self.__generic(endpoint) |
| 87 | + sparql.setProxies(proxiesNotWorking) |
| 88 | + self.assertTrue(sparql.proxies) # assert no empty |
| 89 | + |
| 90 | + try: |
| 91 | + result = sparql.query() |
| 92 | + except URLError as error: |
| 93 | + self.assertTrue(True) |
| 94 | + else: |
| 95 | + self.assertTrue(False) |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + unittest.main() |
| 99 | + |
| 100 | + |
0 commit comments