djblets.webapi.testing.testcases¶
Test case support for full API testing.
This provides a useful mixin, WebAPITestCaseMixin
, that can be
mixed into a base test case for an API testing suite. This mixin provides
a number of useful helpers for calling APIs and validating results, along
with generating consistent docstrings.
-
class
WebAPITestCaseMixin
(methodName=’runTest’)[source]¶ Bases:
djblets.testing.testcases.TestCase
Mixin for test cases for thoroughly testing APIs.
This helps with the testing of APIs by providing convenient methods for performing HTTP requests and checking their response payloads, redirects, mimetypes, and more.
Any test methods in a subclass that have the
is_test_template
flag set toTrue
can include<URL>
in their docstring. When the test is run, the<URL>
will be replaced by the contents of the subclass’ssample_api_url
.WebAPITestCaseMixin is meant to be mixed into a new base class that all API test suites would inherit from. Subclasses must provide values for
sample_api_url
anderror_mimetype
.-
shortDescription
()[source]¶ Return a description for a particular test.
If the test has the
is_test_template
attribute set, and contains<URL>
in the docstring, the URL will be replaced by the contents ofsample_api_url
.Returns: The description of the test. Return type: unicode
-
api_get
(path, query={}, follow_redirects=False, expected_status=200, expected_redirects=[], expected_headers={}, expected_mimetype=None, expected_json=True)[source]¶ Perform and check a HTTP GET request to an API resource.
This will perform the request to the resource and validate that all the results are what the caller expects.
Parameters: - path (unicode) – The path to the resource to request.
- query (dict) – The query string.
- expected_status (int) – The expected HTTP status.
- follow_redirects (bool) – Whether to expect and follow redirects to another URL.
- expected_redirects (list of unicode) – The list of expected redirects performed by the resource(s), in order.
- expected_headers (dict) – Expected HTTP headers and their values from the response.
- expected_mimetype (unicode) – The expected mimetype for the response payload.
- expected_json (bool) – Whether the response is expected to be in JSON format.
Returns: The parsed payload data as a dictionary, if
expected_json
isTrue
and the response isn’t a HTTP 302. Otherwise, the raw payload contents.
-
api_post_with_response
(path, query={}, expected_status=201, expected_mimetype=None)[source]¶ Perform an HTTP POST to an API resource, returning additional data.
This works like
api_post()
, but returns the resulting payload and the originalHttpResponse
.This will perform the request to the resource and validate that all the results are what the caller expects.
Parameters: Returns: A 2-item tuple containing the parsed response and the original
HttpResponse
.Return type:
-
api_post
(*args, **kwargs)[source]¶ Perform and check an HTTP POST to an API resource.
This will perform the request to the resource and validate that all the results are what the caller expects.
Parameters: Returns: The parsed payload data.
-
api_put
(path, query={}, expected_status=200, follow_redirects=False, expected_redirects=[], expected_mimetype=None)[source]¶ Perform and check an HTTP PUT to an API resource.
This will perform the request to the resource and validate that all the results are what the caller expects.
Parameters: - path (unicode) – The path to the resource to request.
- query (dict) – The form data to post.
- expected_status (int) – The expected HTTP status.
- follow_redirects (bool) – Whether to expect and follow redirects to another URL.
- expected_redirects (list of unicode) – The list of expected redirects performed by the resource(s), in order.
- expected_mimetype (unicode) – The expected mimetype for the response payload.
Returns: The parsed payload data.
-
api_delete
(path, expected_status=204)[source]¶ Perform and check an HTTP DELETE to an API resource.
This will perform the request to the resource and validate that all the results are what the caller expects.
Parameters: Returns: The HTTP respons of the payload. This won’t have any content if the delete was successful.
Return type: HttpResponse
-