Skip to content

Commit

Permalink
Add test for sending json and data at the same time (#252)
Browse files Browse the repository at this point in the history
Add test for sending json and data at the same time
  • Loading branch information
michaelboulton authored and bitdivision committed Feb 3, 2019
1 parent a5e4a7b commit 2c51e4a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
22 changes: 12 additions & 10 deletions tavern/_plugins/rest/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,30 @@ def get_request_args(rspec, test_block_config):
logger.debug("Using default GET method")
rspec["method"] = "GET"

content_keys = ["data", "json"]
content_keys = ["data", "json", "files"]

in_request = [c for c in content_keys if c in rspec]
if len(in_request) > 1:
# Explicitly raise an error here
# From requests docs:
# Note, the json parameter is ignored if either data or files is passed.
raise exceptions.BadSchemaError(
"Can only specify one type of request data in HTTP request (tried to send {})".format(
" and ".join(in_request)
)
)

headers = rspec.get("headers", {})
has_content_header = "content-type" in [h.lower() for h in headers.keys()]

if "files" in rspec:
if any(ckey in rspec for ckey in content_keys):
raise exceptions.BadSchemaError(
"Tried to send non-file content alongside a file"
)

if has_content_header:
logger.warning(
"Tried to specify a content-type header while sending a file - this will be ignored"
)
rspec["headers"] = {
i: j for i, j in headers.items() if i.lower() != "content-type"
}
elif headers:
# This should only be hit if we aren't sending a file
if not has_content_header:
rspec["headers"]["content-type"] = "application/json"

fspec = format_keys(rspec, test_block_config["variables"])

Expand Down
20 changes: 20 additions & 0 deletions tests/integration/test_data_key.tavern.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,23 @@ stages:
status_code: 200
body:
status: ok

---

test_name: Test sending JSON and data at the same time fails

_xfail: verify

stages:
- name: Try to send both
request:
url: "{host}/expect_raw_data"
method: POST
data:
a: 123
json:
b: 456
response:
status_code: 200
body:
status: ok
26 changes: 24 additions & 2 deletions tests/unit/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,34 @@ def test_no_override_method(self, req, includes):

assert args["method"] == "POST"

def test_default_content_type(self, req, includes):
@pytest.mark.parametrize("extra", [{}, {"json": [1, 2, 3]}, {"data": {"a": 2}}])
def test_no_default_content_type(self, req, includes, extra):
del req["headers"]["Content-Type"]
req.pop("json", {})
req.pop("data", {})

req.update(**extra)

args = get_request_args(req, includes)

# Requests will automatically set content type headers for json/form encoded data so we don't need to
with pytest.raises(KeyError):
assert args["headers"]["content-type"]

def test_no_set_content_type(self, req, includes):
del req["headers"]["Content-Type"]

args = get_request_args(req, includes)

assert args["headers"]["content-type"] == "application/json"
with pytest.raises(KeyError):
assert args["headers"]["content-type"]

def test_cannot_send_data_and_json(self, req, includes):
req["json"] = [1, 2, 3]
req["data"] = [1, 2, 3]

with pytest.raises(exceptions.BadSchemaError):
get_request_args(req, includes)

def test_no_override_content_type(self, req, includes):
req["headers"]["Content-Type"] = "application/x-www-form-urlencoded"
Expand Down

0 comments on commit 2c51e4a

Please sign in to comment.