Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

APITest case should default to JSON #81

Open
frankwiles opened this issue Aug 30, 2018 · 1 comment
Open

APITest case should default to JSON #81

frankwiles opened this issue Aug 30, 2018 · 1 comment
Assignees

Comments

@frankwiles
Copy link
Member

While I'm sure SOME people out there are still using XML for their APIs, the vast majority are JSON and we should just default the format option to all test calls to be JSON to ease the typing on DRF related testing.

@frankwiles frankwiles self-assigned this Aug 30, 2018
@epicserve
Copy link
Contributor

epicserve commented Nov 26, 2022

I might be missing something, but I add this to all of my projects. I would welcome its addition to this project.

import json

from test_plus.test import TestCase


class BaseTest(TestCase):
    def request_json(self, method, url_name, *args, **kwargs):
        data = kwargs.get("data")
        kwargs["data"] = json.dumps(data) if data is not None else None
        kwargs["extra"] = {
            "content_type": "application/json",
            "HTTP_X_REQUESTED_WITH": "XMLHttpRequest",
        }
        return getattr(self, method)(url_name, *args, **kwargs)

    def post_json(self, url_name, *args, **kwargs):
        return self.request_json("post", url_name, *args, **kwargs)

    def get_json(self, url_name, *args, **kwargs):
        return self.request_json("get", url_name, *args, **kwargs)

    def put_json(self, url_name, *args, **kwargs):
        return self.request_json("put", url_name, *args, **kwargs)

    def delete_json(self, url_name, *args, **kwargs):
        return self.request_json("delete", url_name, *args, **kwargs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants