Skip to content

Commit

Permalink
Addresses #32 and stops sending content-type header when the payload …
Browse files Browse the repository at this point in the history
…is None
  • Loading branch information
Sam Giles committed Apr 24, 2017
1 parent 291c649 commit 67988eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion slumber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def _request(self, method, data=None, files=None, params=None):
headers = {"accept": serializer.get_content_type()}

if not files:
headers["content-type"] = serializer.get_content_type()
if data is not None:
headers["content-type"] = serializer.get_content_type()
data = serializer.dumps(data)

resp = self._store["session"].request(method, url, data=data, params=params, files=files, headers=headers)
Expand Down
57 changes: 42 additions & 15 deletions tests/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_get_200_json(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.get()
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_get_200_text(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.get()
Expand Down Expand Up @@ -95,8 +95,7 @@ def test_options_200_json(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(),
"accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.options()
Expand Down Expand Up @@ -128,8 +127,7 @@ def test_head_200_json(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(),
"accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.head()
Expand Down Expand Up @@ -163,7 +161,7 @@ def test_post_201_redirect(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.post(data={'foo': 'bar'})
Expand Down Expand Up @@ -192,7 +190,7 @@ def test_post_decodable_response(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.post(data={'foo': 'bar'})
Expand Down Expand Up @@ -226,7 +224,7 @@ def test_patch_201_redirect(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.patch(data={'foo': 'bar'})
Expand Down Expand Up @@ -255,7 +253,7 @@ def test_patch_decodable_response(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.patch(data={'foo': 'bar'})
Expand Down Expand Up @@ -289,7 +287,7 @@ def test_put_201_redirect(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.put(data={'foo': 'bar'})
Expand Down Expand Up @@ -318,7 +316,7 @@ def test_put_decodable_response(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.put(data={'foo': 'bar'})
Expand Down Expand Up @@ -377,7 +375,7 @@ def test_get_200_subresource_json(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.get()
Expand Down Expand Up @@ -488,12 +486,41 @@ def test_get_200_json_py3(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.get()
self.assertEqual(resp['result'], ['a', 'b', 'c'])

def test_send_content_type_only_if_body_data_exists(self):
apiuri = "http://example/api/v1/"
newuri = "http://example/api/v1/myresource/"
ses = mock.Mock(spec=requests.session())
api = slumber.API(apiuri, session=ses)

# Empty post request
api.myresource.post()
self.assertEqual(ses.request.call_count, 1)

ses.request.assert_called_with('POST', newuri,
headers={
'accept': 'application/json'
},
data=None,
files=None,
params={})

api.myresource.post({'key': 'value'})
self.assertEqual(ses.request.call_count, 2)
ses.request.assert_called_with('POST', newuri,
headers={
'accept': 'application/json',
'content-type': 'application/json'
},
data='{"key": "value"}',
files=None,
params={})

@unittest.expectedFailure
def test_post_201_does_get(self):
getparams = dict(username="luser", api_key="1234")
Expand Down Expand Up @@ -537,7 +564,7 @@ def test_unicode_decodable_response(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"accept": self.base_resource._store["serializer"].get_content_type()}
)

resp = self.base_resource.post(data={'foo': 'bar'})
Expand Down

0 comments on commit 67988eb

Please sign in to comment.