-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests for put, delete and post
- Loading branch information
Amazia Gur
authored and
Amazia Gur
committed
Nov 12, 2024
1 parent
b53e4d5
commit 21b976a
Showing
4 changed files
with
258 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
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,40 @@ | ||
from hamcrest import assert_that, is_ | ||
|
||
from mockingbird.mockingbird import delete | ||
from tests.support.client import Client | ||
|
||
|
||
def test_delete_404(mockingbird_server): | ||
mockingbird_server.routes( | ||
delete("/remove") | ||
) | ||
resp = Client().delete('/not-found') | ||
assert_that(resp.status_code, is_(404)) | ||
|
||
|
||
def test_delete_default_200_status_code(mockingbird_server): | ||
mockingbird_server.routes( | ||
delete("/remove") | ||
) | ||
resp = Client().delete('/remove') | ||
assert_that(resp.status_code, is_(200)) | ||
|
||
|
||
def test_delete_picked_status_code(mockingbird_server): | ||
mockingbird_server.routes( | ||
delete("/remove"). | ||
status(204) | ||
) | ||
resp = Client().delete('/remove') | ||
assert_that(resp.status_code, is_(204)) | ||
|
||
|
||
def test_delete_empty_response(mockingbird_server): | ||
mockingbird_server.routes( | ||
delete("/remove"). | ||
body(None). | ||
status(204) | ||
) | ||
resp = Client().delete('/remove') | ||
assert_that(resp.status_code, is_(204)) | ||
assert_that(resp.text, is_("")) |
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,102 @@ | ||
from hamcrest import assert_that, is_, has_entry, equal_to | ||
|
||
from mockingbird.mockingbird import post | ||
from tests.support.client import Client | ||
|
||
|
||
def test_post_404(mockingbird_server): | ||
mockingbird_server.routes( | ||
post("/submit") | ||
) | ||
resp = Client().post_as_json('/not-found') | ||
assert_that(resp.status_code, is_(404)) | ||
|
||
|
||
def test_post_default_200_status_code(mockingbird_server): | ||
mockingbird_server.routes( | ||
post("/submit") | ||
) | ||
resp = Client().post_as_json('/submit') | ||
assert_that(resp.status_code, is_(200)) | ||
|
||
|
||
def test_post_picked_status_code(mockingbird_server): | ||
mockingbird_server.routes( | ||
post("/submit"). | ||
status(201) | ||
) | ||
resp = Client().post_as_json('/submit') | ||
assert_that(resp.status_code, is_(201)) | ||
|
||
|
||
def test_post_body_as_text(mockingbird_server): | ||
mockingbird_server.routes( | ||
post('/submit'). | ||
body("submission successful") | ||
) | ||
resp = Client().post_as_text('/submit', body="submission successful") | ||
assert_that(resp.text, is_("submission successful")) | ||
|
||
|
||
def test_post_body_as_json(mockingbird_server): | ||
body = {"result": "created"} | ||
mockingbird_server.routes( | ||
post("/submit"). | ||
body(body). | ||
status(201) | ||
) | ||
resp = Client().post_as_json('/submit', body=body) | ||
assert_that(resp.headers, has_entry("Content-Type", "application/json")) | ||
assert_that(resp.json(), equal_to(body)) | ||
|
||
|
||
def test_post_empty_response(mockingbird_server): | ||
mockingbird_server.routes( | ||
post("/clear"). | ||
body(None). | ||
status(204) | ||
) | ||
resp = Client().post_as_json('/clear') | ||
assert_that(resp.status_code, is_(204)) | ||
assert_that(resp.text, is_("")) | ||
|
||
|
||
def test_post_body_with_text_content(mockingbird_server): | ||
body = "This is a plain text submission." | ||
mockingbird_server.routes( | ||
post("/submit-text"). | ||
body(body). | ||
status(200) | ||
) | ||
resp = Client().post_as_text('/submit-text', body=body) | ||
assert_that(resp.status_code, is_(200)) | ||
assert_that(resp.text, is_(body)) | ||
|
||
|
||
def test_post_body_with_json_content(mockingbird_server): | ||
body = {"message": "Data submitted"} | ||
mockingbird_server.routes( | ||
post("/submit-json"). | ||
body(body). | ||
status(201) | ||
) | ||
resp = Client().post_as_json('/submit-json', body=body) | ||
assert_that(resp.status_code, is_(201)) | ||
assert_that(resp.json(), equal_to(body)) | ||
|
||
|
||
def test_post_file_upload(mockingbird_server): | ||
file_content = b"Test file content" | ||
mockingbird_server.routes( | ||
post("/upload"). | ||
body("File uploaded successfully"). | ||
status(200) | ||
) | ||
with open('/tmp/test_file.txt', 'wb') as f: | ||
f.write(file_content) | ||
|
||
with open('/tmp/test_file.txt', 'rb') as file: | ||
resp = Client().post_as_file('/upload', body={'file': file}) | ||
|
||
assert_that(resp.status_code, is_(200)) | ||
assert_that(resp.text, is_("File uploaded successfully")) |
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,85 @@ | ||
from hamcrest import assert_that, is_, has_entry, equal_to | ||
|
||
from mockingbird.mockingbird import put | ||
from tests.support.client import Client | ||
|
||
|
||
def test_put_404(mockingbird_server): | ||
mockingbird_server.routes( | ||
put("/update") | ||
) | ||
resp = Client().put('/not-found') | ||
assert_that(resp.status_code, is_(404)) | ||
|
||
|
||
def test_put_default_200_status_code(mockingbird_server): | ||
mockingbird_server.routes( | ||
put("/update") | ||
) | ||
resp = Client().put('/update') | ||
assert_that(resp.status_code, is_(200)) | ||
|
||
|
||
def test_put_picked_status_code(mockingbird_server): | ||
mockingbird_server.routes( | ||
put("/update"). | ||
status(202) | ||
) | ||
resp = Client().put('/update') | ||
assert_that(resp.status_code, is_(202)) | ||
|
||
|
||
def test_put_body_as_text(mockingbird_server): | ||
mockingbird_server.routes( | ||
put('/update'). | ||
body("update successful") | ||
) | ||
resp = Client().put_as_text('/update', body="update successful") | ||
assert_that(resp.text, is_("update successful")) | ||
|
||
|
||
def test_put_body_as_json(mockingbird_server): | ||
body = {"result": "updated"} | ||
mockingbird_server.routes( | ||
put("/update"). | ||
body(body). | ||
status(200) | ||
) | ||
resp = Client().put_as_json('/update', body=body) | ||
assert_that(resp.headers, has_entry("Content-Type", "application/json")) | ||
assert_that(resp.json(), equal_to(body)) | ||
|
||
|
||
def test_put_empty_response(mockingbird_server): | ||
mockingbird_server.routes( | ||
put("/clear"). | ||
body(None). | ||
status(204) | ||
) | ||
resp = Client().put('/clear') | ||
assert_that(resp.status_code, is_(204)) | ||
assert_that(resp.text, is_("")) | ||
|
||
|
||
def test_put_text_content(mockingbird_server): | ||
body = "This is an update with plain text." | ||
mockingbird_server.routes( | ||
put("/update-text"). | ||
body(body). | ||
status(200) | ||
) | ||
resp = Client().put_as_text('/update-text', body=body) | ||
assert_that(resp.status_code, is_(200)) | ||
assert_that(resp.text, is_(body)) | ||
|
||
|
||
def test_put_json_content(mockingbird_server): | ||
body = {"message": "Updated successfully"} | ||
mockingbird_server.routes( | ||
put("/update-json"). | ||
body(body). | ||
status(200) | ||
) | ||
resp = Client().put_as_json('/update-json', body=body) | ||
assert_that(resp.status_code, is_(200)) | ||
assert_that(resp.json(), equal_to(body)) |