Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Revert APITestCase defaulting to JSON format. #208

Merged
merged 8 commits into from
Jun 24, 2024

Conversation

KaczuH
Copy link

@KaczuH KaczuH commented Jul 8, 2023

It's an attempt to fix bug reported in #205
It's been introduced in #190

I've looked into it.
Changes i've introduced:

  • added tests for GET requests
  • corrected existing tests for POST requests (format and content_type options were used incorrectly)
  • removed APITestCase.request as it's redundant. In APITest case should default to JSON #81 the solution should be to recommend users to set default format in tests. DRF docs

All changes are explained in commit messages.

All nox tests pass locally.

Kamil Niski added 7 commits July 8, 2023 11:36
It reproduces issue reported in:
revsys#205

`TestAPITestCaseDRFInstalled` only had tests for POST requests but the problem exposes itself on GET
request.
- removed `TestAPITestCaseDRFInstalled.test_post_with_format_and_content_type`

DRF doesn't allow passing both `format` and `content_type`. Removed test was promoting incorrect
usage.

- corrected `TestAPITestCaseDRFInstalled.test_post_with_content_type`
According to DRF docs (https://www.django-rest-framework.org/api-guide/testing/#explicitly-encoding-the-request-body)
using `content_type` requires explicitly encoding request body
`TestAPITestCaseDRFInstalled` tests make GET requests to this view.
DRF uses `multipart` by default for compatibility.
https://www.django-rest-framework.org/api-guide/testing/#using-the-format-argument
but for convenience it can be changed to json.
revsys#190
introduces a bug reported in:
revsys#205

Also given that DRF already can handle serialization of json in post/put/patch/delete/options with
`rest_framework.test.APIRequestFactory._encode_data` it's reasonable to assume `APITestCase.request`
is redundant.
@KaczuH KaczuH changed the title Api test json failing on get Revert APITestCase defaulting to JSON format. Jul 8, 2023
@frankwiles frankwiles merged commit 96f061e into revsys:main Jun 24, 2024
22 of 23 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants