def my_get(url, **kwargs): urlspec = spec[url] if isinstance(urlspec, str): urlspec = {'file': urlspec} if 'charset' not in urlspec: urlspec['charset'] = 'utf-8' url_location = os.path.join(os.path.dirname(specfile), urlspec['file']) res = Mock() # load the .content property with open(url_location, "rb") as fp: res.content = fp.read() # but only load .text if a charset is present (note # default value of 'utf-8' above -- set 'charset': null in # the json file for binary files if urlspec['charset']: with codecs.open(url_location, "r", encoding=urlspec['charset']) as fp: res.text = fp.read() # FIXME: Using a defaultdict ensures that we'll never trip # over the non-existance of certain headers. WE should # specify only the most basic headers to make sure calling # code doesn't rely on eg. the etag header always being # there, because it won't res.headers = collections.defaultdict(lambda: None) res.headers['X-These-Headers-Are'] = 'Faked' res.status_code = 200 return res
def _myget(self, url, **kwargs): res = Mock() res.headers = collections.defaultdict(lambda: None) res.headers['Content-type'] = "text/html" res.status_code = 200 res.encoding = 'utf-8' if url == "http://example.org/": res.content = b"<p><a href='doc/a_.html'>ID: a</a></p>" elif url == "http://example.org/doc/a_.html": res.content = b"<p>This is doc A</p>" else: raise ValueError("Unknown url %s" % url) res.text = res.content.decode() return res
def _myget(self, url, **kwargs): res = Mock() res.headers = collections.defaultdict(lambda:None) res.headers['Content-type'] = "text/html" res.status_code = 200 res.encoding = 'utf-8' if url == "http://example.org/": res.content = b"<p><a href='doc/a_.html'>ID: a</a></p>" elif url == "http://example.org/doc/a_.html": res.content = b"<p>This is doc A</p>" else: raise ValueError("Unknown url %s" % url) res.text = res.content.decode() return res
def makeresponse(*args, **kwargs): if len(returned) > len(responses): raise IndexError("Ran out of canned responses after %s calls" % len(returned)) resp = Mock() resp.status_code = responses[len(returned)][0] responsefile = responses[len(returned)][1] if responsefile: responsefile = "test/files/triplestore/" + responsefile resp.content = util.readfile(responsefile, "rb") resp.text = util.readfile(responsefile) if responsefile.endswith(".json"): data = json.loads(util.readfile(responsefile)) resp.json = Mock(return_value=data) returned.append(True) return resp