def test_crud(self): client = self.client headers = self.headers headers['Content-Type'] = 'text/x-yaml' data = { 'name': 'test-%s' % random.randint(0, 1000000000), 'type': 'NFS' } body = yaml.dump(data) client.request('POST', '/api/datacenters', body=body, headers=headers) response = client.getresponse() assert response.status == http.CREATED location = response.getheader('Location') assert location is not None url = urlparse(location) client.request('GET', '/api/datacenters', headers=headers) response = client.getresponse() assert response.status == http.OK ctype = response.getheader('Content-Type') ctype = http.parse_content_type(ctype) assert ctype[:2] == ('text', 'x-yaml') result = yaml.load(response.read()) for entry in result: if entry['name'] == data['name']: break else: raise AssertionError data['type'] = 'FCP' data['description'] = 'New Description' body = yaml.dump(data) client.request('PUT', url.path, body=body, headers=headers) response = client.getresponse() assert response.status == http.OK client.request('GET', url.path, headers=headers) response = client.getresponse() assert response.status == http.OK ctype = response.getheader('Content-Type') ctype = http.parse_content_type(ctype) assert ctype[:2] == ('text', 'x-yaml') parsed = yaml.load(response.read()) assert parsed['type'] == 2 # XXX assert parsed['description'] == 'New Description' client.request('DELETE', url.path, headers=headers) response = client.getresponse() assert response.status == http.NO_CONTENT client.request('DELETE', url.path, headers=headers) response = client.getresponse() assert response.status == http.NOT_FOUND
def _get_detail(self): for i in range(4): ctypes.append('text/xml; detail=%s' % i) ctypes.append('text/yaml; detail=%s' % i) ctype = request.preferred_content_type(ctypes) if ctype is None: return sub, subtype, params = http.parse_content_type(ctype) try: detail = int(params['detail']) except ValueError: raise HTTPReturn(http.NOT_ACCEPTABLE, reason='Non-integer "detail" in Accept header.') return detail
def parse(self, input): """Parse an entity.""" ctype = request.header('Content-Type') if not ctype: raise HTTPReturn(http.BAD_REQUEST, reason='Missing Content-Type header') try: type, subtype, options = http.parse_content_type(ctype) except ValueError: raise HTTPReturn(http.BAD_REQUEST, reason='Illegal Content-Type header [%s]' % ctype) ctype = '%s/%s' % (type, subtype) encoding = options.get('charset') if not len(input): raise HTTPReturn(http.BAD_REQUEST, reason='No request entity provided') if ctype not in self.parsers: raise HTTPReturn(http.UNSUPPORTED_MEDIA_TYPE, reason='Content-Type not supported [%s]' % ctype) parser = self.parsers[ctype] parsed = parser.parse(input, encoding) return parsed
def test_crud(self): client = self.client headers = self.headers # Create a VM headers['Content-Type'] = 'text/x-yaml' data = { 'name': 'test-%s' % random.randint(0, 1000000), 'template': self.template, 'cluster': self.cluster, 'type': 'server' } body = yaml.dump(data) client.request('POST', '/api/vms', body=body, headers=headers) response = client.getresponse() assert response.status == http.CREATED location = response.getheader('Location') assert location is not None url = urlparse(location) # Check that it is created client.request('GET', '/api/vms', headers=headers) response = client.getresponse() assert response.status == http.OK ctype = response.getheader('Content-Type') ctype = http.parse_content_type(ctype) ctype_requested = http.parse_content_type(self.headers['Accept']) assert ctype[:2] == ctype_requested[:2] result = yaml.load(response.read()) for entry in result: if entry['name'] == data['name']: break else: raise AssertionError # Update it del data['template'] data['memory'] = 512 data['description'] = 'My new virtual machine' body = yaml.dump(data) client.request('PUT', url.path, body=body, headers=headers) response = client.getresponse() assert response.status == http.OK # Check the updates client.request('GET', url.path, headers=headers) response = client.getresponse() assert response.status == http.OK ctype = response.getheader('Content-Type') ctype = http.parse_content_type(ctype) assert ctype[:2] == ctype_requested[:2] data = yaml.load(response.read()) assert data['memory'] == 512 assert data['description'] == 'My new virtual machine' # Try a few searches path = '/api/vms?query=name%3dtest-%2a' client.request('GET', path, headers=headers) response = client.getresponse() assert response.status == http.OK data = yaml.load(response.read()) assert len(data) > 0 # Delete it client.request('DELETE', url.path, headers=headers) response = client.getresponse() assert response.status == http.NO_CONTENT client.request('DELETE', url.path, headers=headers) response = client.getresponse() assert response.status == http.NOT_FOUND