def _test_expansions(expansions): variables = { "count": ("one", "two", "three"), "dom": ("example", "com"), "dub": "me/too", "hello": "Hello World!", "half": "50%", "var": "value", "who": "fred", "base": "http://example.com/home/", "path": "/foo/bar", "list": ("red", "green", "blue"), "keys": OrderedDict([("semi", ";"), ("dot", "."), ("comma", ",")]), "v": "6", "x": "1024", "y": "768", "empty": "", "empty_keys": dict([]), "undef": None, } for template, expansion in expansions.items(): print(template, "->", expansion) uri_template = URITemplate(template) uri = uri_template.expand(**variables) assert uri == expansion
def parseResourceGroups(resourceGroups, environment_vals, only_collection): out = [] for resourceGroup in resourceGroups: collection = dict() collection['id'] = str(uuid4()) collection['folders'] = [] collection['requests'] = [] collection['name'] = resourceGroup['name'] collection['description'] = resourceGroup['description'] collection['timestamp'] = int(time()) collection['synced'] = False collection['remote_id'] = 0 collection['order'] = [] for resource in resourceGroup['resources']: folder = dict() folder['id'] = str(uuid4()) folder['name'] = resource['name'] folder['description'] = resource['description'] folder['order'] = [] folder['collection_id'] = collection['id'] folder['collection_name'] = collection['name'] sub_url = URITemplate(resource['uriTemplate']) for action in resource['actions']: request = dict() request['id'] = str(uuid4()) request['folder'] = folder['id'] request['version'] = 2 request['name'] = action['name'] request['description'] = action['description'] request['descriptionFormat'] = 'html' request['method'] = action['method'] params = { p['name']: p['example'] for p in action['parameters'] } sub_url_str = urllib.unquote( sub_url.expand(**params).string).encode('utf8') request['url'] = "{{HOST}}" + sub_url_str if only_collection: for value in environment_vals: if value['name'] == 'HOST': request['url'] = value['value'] + sub_url_str request['dataMode'] = 'params' request['data'] = [] # Unsupported data request['pathVariables'] = dict() request['tests'] = '' request['time'] = int(time()) request['responses'] = [] request['synced'] = False headers = {} for example in action['examples']: # Add Headers for request_ex in example['requests']: headers.update({ h['name']: h['value'] for h in request_ex['headers'] }) if len(request_ex['body']) > 0: request['dataMode'] = 'raw' request['data'] = request_ex['body'] # Add Accept header to request based on response model (hack?) # EQD: This is not strictly correct since only 1 Accept header will appear in headers for response in example['responses']: content_types = [ r['value'] for r in response['headers'] if r['name'].lower() == 'content-type' ] if len(content_types) > 0 and 'Accept' not in headers: headers['Accept'] = content_types[0] request['headers'] = '\n'.join( ['%s: %s' % (k, v) for k, v in headers.iteritems()]) # Add reference to collection to this request # The collectionId field refers to the parent collection, not the folder request['collectionId'] = collection['id'] # Add reference to the request to the current folder folder['order'].append(request['id']) # Add request json to the collection collection['requests'].append(request) # Add folder json to collection collection['folders'].append(folder) out.append(collection) return out
def test_expansion_with_no_variables(): uri_template = URITemplate("{}") uri = uri_template.expand() assert uri == URI("")
def test_can_parse_none_uri_template(): template = URITemplate(None) assert template.string is None uri = template.expand() assert uri.string is None
def parseResourceGroups(resourceGroups, environment_vals, only_collection): out = [] for resourceGroup in resourceGroups: collection = dict() collection['id'] = str(uuid4()); collection['folders'] = [] collection['requests'] = [] collection['name'] = resourceGroup['name'] collection['description'] = resourceGroup['description'] collection['timestamp'] = int(time()) collection['synced'] = False collection['remote_id'] = 0 collection['order'] = [] for resource in resourceGroup['resources']: folder = dict() folder['id'] = str(uuid4()) folder['name'] = resource['name'] folder['description'] = resource['description'] folder['order'] = [] folder['collection_id'] = collection['id'] folder['collection_name'] = collection['name'] sub_url = URITemplate(resource['uriTemplate']) for action in resource['actions']: request = dict() request['id'] = str(uuid4()) request['folder'] = folder['id'] request['version'] = 2 request['name'] = action['name'] request['description'] = action['description'] request['descriptionFormat'] = 'html' request['method'] = action['method'] params = {p['name']: p['example'] for p in action['parameters']} sub_url_str = urllib.unquote(sub_url.expand(**params).string).encode('utf8') request['url'] = "{{HOST}}"+sub_url_str if only_collection: for value in environment_vals: if value['name'] == 'HOST': request['url'] = value['value'] + sub_url_str request['dataMode'] = 'params' request['data'] = [] # Unsupported data request['pathVariables'] = dict() request['tests'] = '' request['time'] = int(time()) request['responses'] = [] request['synced'] = False headers = {} for example in action['examples']: # Add Headers for request_ex in example['requests']: headers.update({h['name']: h['value'] for h in request_ex['headers']}) if len(request_ex['body']) > 0: request['dataMode'] = 'raw' request['data'] = request_ex['body'] # Add Accept header to request based on response model (hack?) # EQD: This is not strictly correct since only 1 Accept header will appear in headers for response in example['responses']: content_types = [r['value'] for r in response['headers'] if r['name'].lower() == 'content-type'] if len(content_types) > 0 and 'Accept' not in headers: headers['Accept'] = content_types[0] request['headers'] = '\n'.join(['%s: %s' % (k, v) for k,v in headers.iteritems()]) # Add reference to collection to this request # The collectionId field refers to the parent collection, not the folder request['collectionId'] = collection['id'] # Add reference to the request to the current folder folder['order'].append( request['id'] ) # Add request json to the collection collection['requests'].append(request) # Add folder json to collection collection['folders'].append( folder ) out.append(collection) return out