From f8d6df25e8f5fb1c32ac66c21e43f4717c1426ea Mon Sep 17 00:00:00 2001 From: mariamiren Date: Fri, 9 Jan 2026 15:04:06 +0100 Subject: [PATCH] Update ej2e3.py --- 2e/ej2e3.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/2e/ej2e3.py b/2e/ej2e3.py index b46a4e4..b4de630 100644 --- a/2e/ej2e3.py +++ b/2e/ej2e3.py @@ -119,3 +119,88 @@ def post_binary(): if __name__ == '__main__': app = create_app() app.run(debug=True) + +import pytest +from flask.testing import FlaskClient +from ej2e3 import create_app +import io +import os + +@pytest.fixture +def client() -> FlaskClient: + app = create_app() + with app.test_client() as client: + yield client + +def test_post_text(client): + response = client.post("/text", data="Este es un texto de prueba", content_type="text/plain") + assert response.status_code == 200 + assert response.data.decode("utf-8") == "Este es un texto de prueba" + assert response.content_type == "text/plain" + +def test_post_html(client): + response = client.post("/html", data="

Prueba HTML

", content_type="text/html") + assert response.status_code == 200 + assert response.data.decode("utf-8") == "

Prueba HTML

" + assert response.content_type == "text/html" + +def test_post_json(client): + response = client.post("/json", json={"key": "value"}) + assert response.status_code == 200 + assert response.json == {"key": "value"}) + assert response.content_type == "application/json" + +def test_post_xml(client): + xml_data = "value" + response = client.post("/xml", data=xml_data, content_type="application/xml") + assert response.status_code == 200 + assert response.data.decode("utf-8") == xml_data + assert response.content_type == "application/xml" + +def test_post_image(client): + """ + Test POST/ image endpoint -should accept an image file and save it + """ + # Create a test image using scipy.misc.face() + from scipy import misc + import numpy as np + from PIL import Image + + # Get the face image from scipy + face = misc.face() + + # Convert to bytes in PNG format + img = Image.fromarray(face) + img_bytes_io = io.BytesIO() + img.save(img_bytes_io, format='PNG') + img_bytes = img_bytes_io_getvalue() + + #Send image + response = client.post( + "/image", + data=img_bytes, + content_type="image/png" + + # Check response + assert response.status_code == 200 + assert response.is_json + assert "mensaje" in response.json + assert "archivo" in response.json + +def test_post_binary(client): + # Create some binary data + binary_data = os.urandom(64) # 64 random bytes + + # Send the binary data + response = client.post( + "/binary", + data=binary_data, + content_type="application/octet-stream" + ) + + # Check response + assert response.status_code == 200 + assert response.is_json + assert "mensaje" in response.json + assert "tamaño" in response.json + assert response.json["tamaño"] == 64 # Should match the size of our test data