-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create functional tests for httpClient
- Loading branch information
Fernando Aureliano da Silva Maia
committed
Sep 28, 2023
1 parent
ad4dd68
commit 910b603
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# vim: set filetype=python ts=4 sw=4 | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
from tokendito.http_client import HTTPClient | ||
|
||
class TestHTTPClientFunctional(unittest.TestCase): | ||
|
||
def setUp(self): | ||
# Initialize the HTTPClient with a test user-agent | ||
self.client = HTTPClient(user_agent="test-agent") | ||
|
||
def test_get_request(self): | ||
# Make a GET request to the /get endpoint of httpbin which reflects the sent request data | ||
response = self.client.get("https://httpbin.org/get") | ||
json_data = response.json() | ||
|
||
# Assert that the request was successful and the returned User-Agent matches the one we set | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(json_data['headers']['User-Agent'], "test-agent") | ||
|
||
def test_post_request(self): | ||
# Make a POST request to the /post endpoint of httpbin with sample data | ||
response = self.client.post("https://httpbin.org/post", json={"key": "value"}) | ||
json_data = response.json() | ||
|
||
# Assert that the request was successful and the returned json data matches the data we sent | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(json_data['json'], {"key": "value"}) | ||
|
||
def test_set_cookies(self): | ||
# Set a test cookie for the client | ||
self.client.set_cookies({"test_cookie": "cookie_value"}) | ||
|
||
# Make a request to the /cookies endpoint of httpbin which returns set cookies | ||
response = self.client.get("https://httpbin.org/cookies") | ||
json_data = response.json() | ||
|
||
# Assert that the cookie we set is correctly returned by the server | ||
self.assertEqual(json_data['cookies'], {"test_cookie": "cookie_value"}) | ||
|
||
def test_custom_header(self): | ||
# Make a GET request with a custom header | ||
response = self.client.get("https://httpbin.org/get", headers={"X-Test-Header": "TestValue"}) | ||
json_data = response.json() | ||
|
||
# Assert that the custom header was correctly sent | ||
self.assertEqual(json_data['headers']['X-Test-Header'], "TestValue") | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |