Skip to content

Commit

Permalink
chore: add tests for put, delete and post
Browse files Browse the repository at this point in the history
  • Loading branch information
Amazia Gur authored and Amazia Gur committed Nov 12, 2024
1 parent b53e4d5 commit 21b976a
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/support/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,34 @@ def __init__(self, root='http://localhost:8080'):

def get(self, path, headers=None):
return requests.get(f"{self.root + path}", headers=headers)

def post_as_json(self, path, body=None, headers=None):
return requests.post(f"{self.root + path}", json=body, headers=headers)

def post_as_text(self, path, body=None, headers=None):
headers = headers or {}
headers['Content-Type'] = 'text/plain'
return requests.post(f"{self.root + path}", data=body, headers=headers)

def post_as_image(self, path, body=None, headers=None):
headers = headers or {}
headers['Content-Type'] = 'image/png'
return requests.post(f"{self.root + path}", data=body, headers=headers)

def post_as_file(self, path, body=None, headers=None):
return requests.post(f"{self.root + path}", files=body, headers=headers)

def put_as_json(self, path, body=None, headers=None):
return requests.put(f"{self.root + path}", json=body, headers=headers)

def put_as_text(self, path, body=None, headers=None):
return requests.put(f"{self.root + path}", data=body, headers=headers)

def put_as_file(self, path, body=None, headers=None):
return requests.put(f"{self.root + path}", files=body, headers=headers)

def put(self, path, body=None, headers=None):
return requests.put(f"{self.root + path}", json=body, headers=headers)

def delete(self, path, headers=None):
return requests.delete(f"{self.root + path}", headers=headers)
40 changes: 40 additions & 0 deletions tests/test_mockingbird_delete.py
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_(""))
102 changes: 102 additions & 0 deletions tests/test_mockingbird_post.py
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"))
85 changes: 85 additions & 0 deletions tests/test_mockingbird_put.py
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))

0 comments on commit 21b976a

Please sign in to comment.