Skip to content

Commit

Permalink
Fixed httplib2 mocking (bug 1050091, bug 1050097)
Browse files Browse the repository at this point in the history
- 204 No Content should be mocked with empty response bodies
- Content-Type headers should not be mocked with empty response bodies
- httplib2 would never return None as a response body
- The Identity API never expects a req/resp body with a string value of "null"

Change-Id: Ie22e8e5288573268165ed06049978195955f8ca6
  • Loading branch information
dolph committed Sep 12, 2012
1 parent 0a8c960 commit 5991727
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 39 deletions.
5 changes: 4 additions & 1 deletion keystoneclient/base.py
Expand Up @@ -91,7 +91,10 @@ def _update(self, url, body, response_key=None, method="PUT"):
methods = {"PUT": self.api.put,
"POST": self.api.post}
try:
resp, body = methods[method](url, body=body)
if body is not None:
resp, body = methods[method](url, body=body)
else:
resp, body = methods[method](url)
except KeyError:
raise exceptions.ClientException("Invalid update method: %s"
% method)
Expand Down
2 changes: 1 addition & 1 deletion tests/v2_0/test_ec2.py
Expand Up @@ -139,7 +139,7 @@ def test_delete(self):
user_id = 'usr'
access = 'access'
resp = httplib2.Response({
"status": 200,
"status": 204,
"body": "",
})

Expand Down
2 changes: 1 addition & 1 deletion tests/v2_0/test_endpoints.py
Expand Up @@ -83,7 +83,7 @@ def test_create(self):

def test_delete(self):
resp = httplib2.Response({
"status": 200,
"status": 204,
"body": "",
})
httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
Expand Down
32 changes: 15 additions & 17 deletions tests/v2_0/test_roles.py
Expand Up @@ -66,7 +66,7 @@ def test_create(self):

def test_delete(self):
resp = httplib2.Response({
"status": 200,
"status": 204,
"body": "",
})
httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
Expand Down Expand Up @@ -147,62 +147,60 @@ def test_roles_for_user_tenant(self):

def test_add_user_role(self):
resp = httplib2.Response({
"status": 200,
"body": json.dumps({}),
"status": 204,
"body": '',
})

httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
'v2.0/users/foo/roles/OS-KSADM/barrr'),
'PUT',
body='null',
headers=self.TEST_POST_HEADERS) \
.AndReturn((resp, None))
headers=self.TEST_REQUEST_HEADERS) \
.AndReturn((resp, resp['body']))
self.mox.ReplayAll()

self.client.roles.add_user_role('foo', 'barrr')

def test_add_user_role_tenant(self):
resp = httplib2.Response({
"status": 200,
"body": json.dumps({}),
"status": 204,
"body": '',
})

httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
'PUT',
body='null',
headers=self.TEST_POST_HEADERS) \
.AndReturn((resp, None))
headers=self.TEST_REQUEST_HEADERS) \
.AndReturn((resp, resp['body']))
self.mox.ReplayAll()

self.client.roles.add_user_role('foo', 'barrr', '4')

def test_remove_user_role(self):
resp = httplib2.Response({
"status": 200,
"body": json.dumps({}),
"status": 204,
"body": '',
})

httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
'v2.0/users/foo/roles/OS-KSADM/barrr'),
'DELETE',
headers=self.TEST_REQUEST_HEADERS) \
.AndReturn((resp, None))
.AndReturn((resp, resp['body']))
self.mox.ReplayAll()

self.client.roles.remove_user_role('foo', 'barrr')

def test_remove_user_role_tenant(self):
resp = httplib2.Response({
"status": 200,
"body": json.dumps({}),
"status": 204,
"body": '',
})

httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
'DELETE',
headers=self.TEST_REQUEST_HEADERS) \
.AndReturn((resp, None))
.AndReturn((resp, resp['body']))
self.mox.ReplayAll()

self.client.roles.remove_user_role('foo', 'barrr', '4')
32 changes: 15 additions & 17 deletions tests/v2_0/test_tenants.py
Expand Up @@ -83,7 +83,7 @@ def test_create(self):

def test_delete(self):
resp = httplib2.Response({
"status": 200,
"status": 204,
"body": "",
})
httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
Expand Down Expand Up @@ -220,31 +220,30 @@ def test_update(self):

def test_add_user(self):
resp = httplib2.Response({
"status": 200,
"body": json.dumps({}),
"status": 204,
"body": '',
})

httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
'PUT',
body='null',
headers=self.TEST_POST_HEADERS) \
.AndReturn((resp, None))
headers=self.TEST_REQUEST_HEADERS) \
.AndReturn((resp, resp['body']))
self.mox.ReplayAll()

self.client.tenants.add_user('4', 'foo', 'barrr')

def test_remove_user(self):
resp = httplib2.Response({
"status": 200,
"body": json.dumps({}),
"status": 204,
"body": '',
})

httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
'DELETE',
headers=self.TEST_REQUEST_HEADERS) \
.AndReturn((resp, None))
.AndReturn((resp, resp['body']))
self.mox.ReplayAll()

self.client.tenants.remove_user('4', 'foo', 'barrr')
Expand All @@ -259,16 +258,15 @@ def test_tenant_add_user(self):
},
}
resp = httplib2.Response({
"status": 200,
"body": json.dumps({}),
"status": 204,
"body": '',
})

httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
'PUT',
body='null',
headers=self.TEST_POST_HEADERS) \
.AndReturn((resp, None))
headers=self.TEST_REQUEST_HEADERS) \
.AndReturn((resp, resp['body']))
self.mox.ReplayAll()

# make tenant object with manager
Expand All @@ -287,15 +285,15 @@ def test_tenant_remove_user(self):
},
}
resp = httplib2.Response({
"status": 200,
"body": json.dumps({}),
"status": 204,
"body": '',
})

httplib2.Http.request(urlparse.urljoin(self.TEST_URL,
'v2.0/tenants/4/users/foo/roles/OS-KSADM/barrr'),
'DELETE',
headers=self.TEST_REQUEST_HEADERS) \
.AndReturn((resp, None))
.AndReturn((resp, resp['body']))
self.mox.ReplayAll()

# make tenant object with manager
Expand Down
2 changes: 1 addition & 1 deletion tests/v2_0/test_tokens.py
Expand Up @@ -18,7 +18,7 @@ def setUp(self):

def test_delete(self):
resp = httplib2.Response({
"status": 200,
"status": 204,
"body": ""})

req = httplib2.Http.request(
Expand Down
2 changes: 1 addition & 1 deletion tests/v2_0/test_users.py
Expand Up @@ -82,7 +82,7 @@ def test_create(self):

def test_delete(self):
resp = httplib2.Response({
"status": 200,
"status": 204,
"body": "",
})
httplib2.Http.request(urlparse.urljoin(self.TEST_URL, 'v2.0/users/1'),
Expand Down

0 comments on commit 5991727

Please sign in to comment.