def test_get_expires(self): # No header at all r = httmock.response(200, '{}'.encode('utf-8')) self.assertEqual(self.api._get_expires(r), 0) # Cache-Control header with no-cache r = httmock.response(status_code=200, content='{}'.encode('utf-8'), headers={'Cache-Control': 'no-cache'}) self.assertEqual(self.api._get_expires(r), 0) # Cache-Control header with no-store r = httmock.response(status_code=200, content='{}'.encode('utf-8'), headers={'Cache-Control': 'no-store'}) self.assertEqual(self.api._get_expires(r), 0) # Cache-Control header with wrong content r = httmock.response(status_code=200, content='{}'.encode('utf-8'), headers={'Cache-Control': 'no-way'}) self.assertEqual(self.api._get_expires(r), 0) # Cache-Control header with max-age=300 r = httmock.response(status_code=200, content='{}'.encode('utf-8'), headers={'Cache-Control': 'max-age=300'}) self.assertEqual(self.api._get_expires(r), 300)
def test_response_auto_json(self): r = response(0, self.content) self.assertTrue(isinstance(r.content, binary_type)) self.assertTrue(isinstance(r.text, text_type)) self.assertEqual(r.json(), self.content) r = response(0, self.content_list) self.assertEqual(r.json(), self.content_list)
def mock_remote_authentication_file(url, request): if 'good-file.csv' in url.geturl(): return response(200, ''' Some junk below Email domain,Organization,Email address,Organization,Name codeforamerica.org,Code for America,[email protected],Code for America,Mike Migurski *@codeforamerica.org,Code for America,,, ''') if 'org-file.csv' in url.geturl(): return response(200, ''' Some junk below Email domain,Organization codeforamerica.org,Code for America *@codeforamerica.org,Code for America ''') if 'addr-file.csv' in url.geturl(): return response(200, ''' Some junk below Email address,Organization,Name [email protected],Code for America,Mike Migurski ''') return response(404, '')
def get_defaults(url, request): if request.method != 'GET': return LOG.debug("GET mock for %s", url) if not request.headers.get('X-auth-token', None): return {'status_code': requests.codes.UNAUTHORIZED} if 'vpn-svc/ike/policies/2' in url.path: content = {u'kind': u'object#ike-policy', u'priority-id': u'2', u'version': u'v1', u'local-auth-method': u'pre-share', u'encryption': u'des', u'hash': u'sha', u'dhGroup': 1, u'lifetime': 86400} return httmock.response(requests.codes.OK, content=content) if 'vpn-svc/ipsec/policies/' in url.path: ipsec_policy_id = url.path.split('/')[-1] content = {u'kind': u'object#ipsec-policy', u'mode': u'tunnel', u'policy-id': u'%s' % ipsec_policy_id, u'protection-suite': {}, u'lifetime-sec': 3600, u'pfs': u'Disable', u'anti-replay-window-size': u'None', u'lifetime-kb': 4608000, u'idle-time': None} return httmock.response(requests.codes.OK, content=content)
def response_content(url, request): # Username = Aladdin, Password = OpenSesame if request.headers['authorization'] == 'Basic QWxhZGRpbjpPcGVuU2VzYW1l': return httmock.response(200, content=b'hi', headers={ 'content-type': 'image/jpeg' }) return httmock.response(401)
def server_get(url, request): ctx = checkers.AuthContext() test_ops = [bakery.Op(entity='test-op', action='read')] auth_checker = server_bakery.checker.auth( httpbakery.extract_macaroons(request.headers)) try: auth_checker.allow(ctx, test_ops) resp = response(status_code=200, content='done') except bakery.PermissionDenied: caveats = [ checkers.Caveat(location='http://0.3.2.1', condition='is-ok') ] m = server_bakery.oven.macaroon( version=bakery.LATEST_VERSION, expiry=datetime.utcnow() + timedelta(days=1), caveats=caveats, ops=test_ops) content, headers = httpbakery.discharge_required_response( m, '/', 'test', 'message') resp = response(status_code=401, content=content, headers=headers) return request.hooks['response'][0](resp)
def lists(url, request, content_type='application/json'): parameters = dict(parse_qsl(url.query)) page = try_convert(parameters.get('page'), int) or 1 limit = try_convert(parameters.get('limit'), int) or 10 # Retrieve items from fixture items = get_json(url.netloc, url.path, url.query) if items is None: return httmock.response(404, request=request) # Calculate page count and item offset offset = (page - 1) * limit page_count = int(math.ceil(float(len(items)) / limit)) return httmock.response( 200, json.dumps(items[offset:offset + limit]), { 'Content-Type': content_type, 'X-Pagination-Page': page, 'X-Pagination-Limit': limit, 'X-Pagination-Page-Count': page_count, 'X-Pagination-Item-Count': len(items) }, request=request )
def __call__(self, url, request): # Temporary fix for tests to pass (due to first request for publication date) if request.url == 'http://api.navitia.io/v1/coverage/jdr/status': return response(200, json.dumps({'status': {'publication_date': '20170309T143733.933792'}})) if self.assert_url: eq_(self.assert_url, request.url) return response(self.response_code, json.dumps(self.json))
def response_content(self, url, request): ''' Fake HTTP responses for use with HTTMock in tests. ''' scheme, host, path, _, query, _ = urlparse(url.geturl()) data_dirname = join(dirname(__file__), 'tests', 'data') local_path = None if host == 'fake-s3.local': return response(200, self.s3._read_fake_key(path)) if (host, path) == ('data.acgov.org', '/api/geospatial/8e4s-7f4v'): local_path = join(data_dirname, 'us-ca-alameda_county-excerpt.zip') if (host, path) == ('www.ci.berkeley.ca.us', '/uploadedFiles/IT/GIS/Parcels.zip'): local_path = join(data_dirname, 'us-ca-berkeley-excerpt.zip') if (host, path) == ('www.ci.berkeley.ca.us', '/uploadedFiles/IT/GIS/No-Parcels.zip'): return response(404, 'Nobody here but us coats') if (host, path) == ('data.openoakland.org', '/sites/default/files/OakParcelsGeo2013_0.zip'): local_path = join(data_dirname, 'us-ca-oakland-excerpt.zip') if (host, path) == ('s3.amazonaws.com', '/data.openaddresses.io/cache/pl.zip'): local_path = join(data_dirname, 'pl.zip') if (host, path) == ('s3.amazonaws.com', '/data.openaddresses.io/cache/jp-fukushima.zip'): local_path = join(data_dirname, 'jp-fukushima.zip') if (host, path) == ('data.sfgov.org', '/download/kvej-w5kb/ZIPPED%20SHAPEFILE'): local_path = join(data_dirname, 'us-ca-san_francisco-excerpt.zip') if (host, path) == ('www.carsonproperty.info', '/ArcGIS/rest/services/basemap/MapServer/1/query'): qs = parse_qs(query) body_data = parse_qs(request.body) if request.body else {} if qs.get('returnIdsOnly') == ['true']: local_path = join(data_dirname, 'us-ca-carson-ids-only.json') elif body_data.get('outSR') == ['4326']: local_path = join(data_dirname, 'us-ca-carson-0.json') if (host, path) == ('www.carsonproperty.info', '/ArcGIS/rest/services/basemap/MapServer/1'): qs = parse_qs(query) if qs.get('f') == ['json']: local_path = join(data_dirname, 'us-ca-carson-metadata.json') if (host, path) == ('data.openaddresses.io', '/20000101/us-ca-carson-cached.json'): local_path = join(data_dirname, 'us-ca-carson-cache.geojson') if scheme == 'file': local_path = path if local_path: type, _ = guess_type(local_path) with open(local_path, 'rb') as file: return response(200, file.read(), headers={'Content-Type': type}) raise NotImplementedError(url.geturl())
def resource_get(url, request): file_path = '%s/%s%s' % (API_PATH, url.netloc, url.path) try: content = Resource(file_path).get() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(200, content, HEADERS, None, 5, request)
def handler(url, request): if ev.is_set(): return httmock.response(201, request=request) # Set event ev.set() # Return error return httmock.response(504, request=request)
def resourcesListByBoundingBox_get(url, request): file_path = url.netloc + url.path + 'resourceList-bounding-box' try: content = Resource(file_path).get() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(200, content, HEADERS, None, 5, request)
def resourceSetFileType_post(url, request): file_path = url.netloc + url.path + '/set-file-type-response' try: content = Resource(file_path).get() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(202, content, HEADERS, None, 5, request)
def resourceZipFolder_post(url, request): file_path = url.netloc + url.path + 'zip-folder-response' try: content = Resource(file_path).get() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(200, content, HEADERS, None, 5, request)
def githubmock_request(url, request): users = { "user6": {"login": "******", "password": "******", "email": "*****@*****.**", "name": "Demo user6", "ssh_keys": "", "code": "user6_code", "token": "user6_token", "id": 666} } headers = {'content-type': 'application/json'} # Handle a token request if request.method == 'POST': token = None code = urlparse.parse_qs(request.body)['code'][0] for user in users: if users[user]['code'] == code: token = users[user]['token'] break if token: content = {"access_token": token} else: return httmock.response(401, {'Error': 'Not Found'}) # Handle informations request else: u = None for user in users: auth_header = request.headers['Authorization'] _token = users[user]['token'] # handle oauth if _token in auth_header: u = user break # handle API key auth elif base64.b64encode(_token + ':x-oauth-basic') in auth_header: u = user break if not u: # user not found, do not authorize error_content = {u'message': u'Bad credentials'} return httmock.response(401, error_content) if 'keys' in url.path: content = {'key': users[u]['ssh_keys']} elif '/user/emails' in url.path: content = json.dumps([ {'email': users[u]['email'], 'primary': True}]) else: content = {'login': u, 'email': users[u]['email'], 'name': users[u]['name'], 'id': users[u]['id']} return httmock.response(200, content, headers, None, 5, request)
def userInfo_get(url, request): file_path = url.netloc + url.path + 'userInfo-1' try: content = Resource(file_path).get() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(200, content, HEADERS, None, 5, request)
def scimeta_json_put(url, request): file_path = url.netloc + url.path + '/scimeta-update-response' try: content = Resource(file_path).get() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(202, content, HEADERS, None, 5, request)
def mock_response(url, request): if url.netloc == 'webmention.rocks': key = '%s%s' % (url.netloc, url.path) if key in test_data.keys(): d = test_data[key] return response(200, d['html'], d['headers']) elif url.netloc == 'bear.im' and url.path == '/bearlog/2013/325/indiewebify-and-the-new-site.html': return response(200, post_html) else: return response(500)
def bsve_mock(url, request): if url.path.split("/")[-1] == "request": return httmock.response(200, "12345") else: pluginTestDir = os.path.dirname(os.path.realpath(__file__)) filepath = os.path.join(pluginTestDir, "data", "bsve_search.json") with open(filepath) as bsve_search_file: content = {"status": 1, "results": json.load(bsve_search_file)} headers = {"content-length": len(content), "content-type": "application/json"} return httmock.response(200, content, headers, request=request)
def response_content(url, request): response_headers = {'Content-Type': 'text/html; charset=utf-8'} if (url.netloc, url.path) == ('mapzen.com', '/site-fragments/navbar.html'): return response(200, '<div>fake navbar HTML</div>', headers=response_headers) if (url.netloc, url.path) == ('mapzen.com', '/site-fragments/footer.html'): return response(200, '<div>fake footer HTML</div>', headers=response_headers) raise Exception(url)
def resource_get(url, request): file_path = url.netloc + url.path try: with open(file_path, 'r') as f: content = f.read() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(200, content, HEADERS, None, 5, request)
def resourceFlags_post(url, request): body = parse_qs(request.body) file_path = url.netloc + url.path + body.get('t')[0] try: content = Resource(file_path).get() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(202, content, HEADERS, None, 5, request)
def resourceTypes_get(url, request): file_path = url.netloc + url.path # Remove trailing slash so that we can open the file file_path = file_path.strip('/') try: content = Resource(file_path).get() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(200, content, HEADERS, None, 5, request)
def datasets_mock(url, request): """ Args: url (str): request (PreparedRequest): requests request """ if not check_auth(request): return unauthorized_mock() if request.original.params.get("dataset") == "ENPR_PSEDUC": return response(200, load_mock_json("dataset_filter"), {"Content-Type": "application/json"}) return response(200, load_mock_json("datasets"), {"Content-Type": "application/json"})
def test_response_auto_json(self): r = response(0, self.content) if sys.version_info[0] == 2: self.assertTrue(isinstance(r.content, str)) elif sys.version_info[0] == 3: self.assertTrue(isinstance(r.content, bytes)) else: assert False, 'Could not determine Python version' self.assertEqual(r.json(), self.content) r = response(0, self.content_list) self.assertEqual(r.json(), self.content_list)
def get_fixture(netloc, path, query=None, request=None): content = get_content(netloc, path, query) if content is None: return httmock.response(404, request=request) return httmock.response( 200, content, { 'Content-Type': 'application/json' }, request=request )
def response_patch_notok(url, request): headers = {'content-type': 'application/json'} if request.method == 'PATCH': if request.headers['If-Match'] == '27f88f9749259b53ccaf48331074fa54d092e1cc': content = '{"_updated": "Wed, 19 Aug 2015 07:59:51 GMT", "_links": {"self": {"href": "livehost/55d113976376e9835e1b2feb", "title": "Livehost"}}, "_created": "Sun, 16 Aug 2015 22:49:59 GMT", "_status": "OK", "_id": "55d113976376e9835e1b2feb", "_etag": "fff582e398e47bce29e7317f25eb5068aaac3c4a"}' return response(200, content, headers, None, 5, request) else: content = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>412 Precondition Failed</title>\n<h1>Precondition Failed</h1>\n<p>Client and server etags don\'t match</p>\n' return response(412, content, headers, None, 5, request) elif request.method == 'GET': content = '{"_updated": "Wed, 19 Aug 2015 07:59:51 GMT", "last_state_change": 1439818531, "acknowledged": false, "last_check": 1439789562, "long_output": null, "state": "UP", "_links": {"self": {"href": "livehost/55d113976376e9835e1b2feb", "title": "Livehost"}, "collection": {"href": "livehost", "title": "livehost"}, "parent": {"href": "/", "title": "home"}}, "host_name": "55d113586376e9835e1b2fe6", "_created": "Sun, 16 Aug 2015 22:49:59 GMT", "services": null, "output": "[Errno 2] No such file or directory", "_id": "55d113976376e9835e1b2feb", "_etag": "27f88f9749259b53ccaf48331074fa54d092e1ce"}' return response(200, content, headers, None, 5, request)
def resourceListFilterCreator_get(url, request): if url.query == 'creator=bmiles': file_path = url.netloc + url.path + 'resourceList-bmiles' else: file_path = '' try: content = Resource(file_path).get() except EnvironmentError: # catch any environment errors (i.e. file does not exist) and return a # 404. return response(404, {}, HEADERS, None, 5, request) return response(200, content, HEADERS, None, 5, request)
def response_content(url, request): print url headers = {'Content-Type': 'application/json'} if url.netloc == 'portal' and url.path.startswith('/api/action/'): return response(200, json.dumps(portal_resp), headers) elif url.netloc == 'uic' and url.path == '/team/users': return response(200, json.dumps(uic_resp), headers) elif url.netloc == 'api.nexmo.com': return response(200, '{}', headers) elif url.netloc == 'yunpian.com': return response(200, '{"code": 0}', headers) else: raise Exception('Meh!')
def bsve_mock(url, request): r = url.path.split("/")[-1].lower() if r == "soda": # the initial search request return httmock.response(200, '{"requestId": "12345", "status": 0}') elif r == "12345": pluginTestDir = os.path.dirname(os.path.realpath(__file__)) filepath = os.path.join(pluginTestDir, "data", "soda_dump.json") with open(filepath) as soda_dump_file: content = json.load(soda_dump_file) headers = {"content-length": len(content), "content-type": "application/json"} return httmock.response(200, content, headers, request=request) else: self.fail('Unexpected BSVE request "%s"' % url.path)
def resp_mark_all_as_done(url, request): headers = {"content-type": "application/json"} return response(204, {}, headers, None, 5, request)
def all_instances_payload(url, request): return httmock.response(200, { "resourceList": [1, 2], }, request=request)
def create_instance_payload(url, request): return httmock.response(201, {"id": "test"}, request=request)
def resp_get_environment(url, request): headers = {"content-type": "application/json"} content = '{"name": "environment_name", "id": 1, "last_deployment": "sometime"}'.encode( "utf-8" ) return response(200, content, headers, None, 5, request)
def resp_get_todo(url, request): headers = {"content-type": "application/json"} return response(200, encoded_content, headers, None, 5, request)
def error_modoffset(url, request): assert url.path == "/v1/projects/TEST/subscriptions/subscription1:modifyOffset" return response(400, '{"error": {"message": "Offset out of bounds", \ "code": 400, \ "status": "INVALID_ARGUMENT"}}')
def error_unauth(url, request): assert url.path == "/v1/projects/TEST/topics/topic1" return response(401, '{"error": {"code": 401, \ "message": "Unauthorized", \ "status": "UNAUTHORIZED"}}')
def error_json(url, request): assert url.path == "/v1/projects/TEST/topics/topic1" return response(500, '{"error": {"code": 500,\ "message": "Cannot get topic",\ "status": "INTERNAL_SERVER_ERROR"\ }}', None, None, 5, request)
def response_content(url, request): headers = {'Set-Cookie': 'foo=bar;'} content = {'success': True} return response(200, content, headers, None, 5, request)
def resp_get_project(url, request): headers = {"content-type": "application/json"} content = '{"name": "name", "id": 1}'.encode("utf-8") return response(200, content, headers, None, 5, request)
def resp_deactivate(url, request): headers = {"content-type": "application/json"} return response(201, {}, headers, None, 5, request)
def resp_get_group(url, request): headers = {"content-type": "application/json"} content = '{"name": "name", "id": 1, "path": "path"}' content = content.encode("utf-8") return response(200, content, headers, None, 5, request)
def resp_get_environment(url, request): headers = {"content-type": "application/json"} content = """{"statistics": {"counts": {"all": 20, "closed": 5, "opened": 15}}}""".encode( "utf-8" ) return response(200, content, headers, None, 5, request)
def test_response_headers(self): r = response(200, None, {'Content-Type': 'application/json'}) self.assertEqual(r.headers['content-type'], 'application/json')
def resp_get_environment(url, request): headers = {"content-type": "application/json"} content = """{"fetches": {"total": 50, "days": [{"count": 10, "date": "2018-01-10"}]}}""".encode( "utf-8" ) return response(200, content, headers, None, 5, request)
def resp_get_user_status(url, request): headers = {"content-type": "application/json"} content = '{"message": "test", "message_html": "<h1>Message</h1>", "emoji": "thumbsup"}' content = content.encode("utf-8") return response(200, content, headers, None, 5, request)
def response_content(url, request): content = get_file("consul_api_health_mock.json") return response(200, content, CONSUL_HEADERS, None, 5, request)
def resp_cont(url, request): headers = {"content-type": "application/json"} content = '{{"id": {0:d}, "username": "******"}}'.format(id_, name).encode( "utf-8" ) return response(200, content, headers, None, 5, request)
def mocked_response(url, content): data = next(internal) if prep: data = prep(data) return response(200, data, {'content-type': 'application/json'}, None, 5, content)
def resp_get_issue(url, request): headers = {"content-type": "application/json"} content = '[{"name": "name", "id": 1}, ' '{"name": "other_name", "id": 2}]' content = content.encode("utf-8") return response(200, content, headers, None, 5, request)
def response_content(url, request): return response(200, 'Foo', {'Set-Cookie': 'foo=bar;'}, request=request)
def resp_cont(url, request): headers = {"content-type": "application/json"} content = '["name": "project1"]' return response(200, content, headers, None, 5, request)
def instances_of_payload(url, request): return httmock.response(200, json.dumps(payload), request=request)
def resp_cont(url, request): headers = {"content-type": "application/octet-stream"} content = "content" return response(200, content, headers, None, 5, request)
def version_payload(url, request): return httmock.response(200, json.dumps(pyscaleio.client.__api_version__), request=request)
def resp_mark_as_done(url, request): headers = {"content-type": "application/json"} single_todo = json.dumps(json_content[0]) content = single_todo.encode("utf-8") return response(200, content, headers, None, 5, request)
def resp_cont(url, request): headers = {"content-type": "application/json"} content = "true" return response(200, content, headers, None, 5, request)
def resp_cont(url, request): content = {"Here is wh it failed"} return response(404, content, {}, None, 5, request)
def resp_application_create(url, request): headers = {"content-type": "application/json"} return response(200, json_content, headers, None, 5, request)
def test_response_status_code(self): r = response(200) self.assertEqual(r.status_code, 200)