Skip to content

Commit

Permalink
Merge pull request #100 from questionlp/develop
Browse files Browse the repository at this point in the history
HTTP 418 Teapot
  • Loading branch information
questionlp authored Jan 28, 2025
2 parents a63f164 + a30725b commit f22871c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## 6.4.0

### Application Changes

- Implementing [HTTP 418](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/418)

## 6.3.1

### Application Changes
Expand Down
25 changes: 24 additions & 1 deletion app/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
# vim: set noai syntax=python ts=4 sw=4:
"""Main Routes for Wait Wait Stats Page."""

import json
from pathlib import Path

import mysql.connector
from flask import Blueprint, Response, current_app, render_template, send_file
from flask import Blueprint, Response, current_app, render_template, request, send_file
from wwdtm.show import Show

from app.config import DEFAULT_RECENT_DAYS_AHEAD, DEFAULT_RECENT_DAYS_BACK
Expand Down Expand Up @@ -79,3 +80,25 @@ def about() -> str:
def site_history() -> str:
"""View: Site History Page."""
return render_template("pages/site_history.html")


@blueprint.route("/teapot", methods=["GET", "POST"])
def teapot() -> Response:
"""View: Teapot."""
return render_template("pages/teapot.html"), 418


@blueprint.route("/", methods=["BREW"])
@blueprint.route("/teapot", methods=["BREW"])
def teapot_brew() -> Response:
"""View Teapot Brew Method."""
_content_type = request.headers.get("Content-Type")
if _content_type == "application/coffee-pot-command":
_response_data = {"data": "I'm a teapot."}
return (
Response(json.dumps(_response_data), content_type="application/json"),
418,
)

_response_data = {"data": "Move along home"}
return Response(json.dumps(_response_data), content_type="application/json"), 404
20 changes: 20 additions & 0 deletions app/templates/pages/teapot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends "base.html" %}
{% block title %}Teapot{% endblock %}

{% block content %}
<div class="static-page">
<h2>I'm a little teapot, short and stout</h2>

<p>
Hello, I see that you found my little easter egg based on
<a href="https://datatracker.ietf.org/doc/html/rfc2324">RFC 2324</a>.
For more information, check out the MDN document on
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/418">HTTP
status response code 418</a>.
</p>

<p>
You can now <a href="{{ url_for('main.index') }}"
title="Allamaraine!">move along home</a>.
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion app/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# vim: set noai syntax=python ts=4 sw=4:
"""Application Version for Wait Wait Stats Page."""

APP_VERSION = "6.3.1"
APP_VERSION = "6.4.0"
44 changes: 44 additions & 0 deletions tests/test_main_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,47 @@ def test_site_history(client: FlaskClient) -> None:
assert response.status_code == 200
assert b"Versions 1 and 2" in response.data
assert b"Version 4" in response.data


def test_teapot(client: FlaskClient) -> None:
"""Testing main.teapot."""
response: TestResponse = client.get("/teapot")
assert response.status_code == 418
assert b"I'm a little teapot" in response.data

response = client.post("/teapot")
assert response.status_code == 418
assert b"I'm a little teapot" in response.data


def test_teapot_brew(client: FlaskClient) -> None:
"""Testing main.teapot_brew."""
# Testing passing in valid Content-Type for both endpoints
response = client.open(
"/",
method="BREW",
headers={"Content-Type": "application/coffee-pot-command"},
)
assert response.status_code == 418
assert response.content_type == "application/json"
assert b"I'm a teapot" in response.data

response = client.open(
"/teapot",
method="BREW",
headers={"Content-Type": "application/coffee-pot-command"},
)
assert response.status_code == 418
assert response.content_type == "application/json"
assert b"I'm a teapot" in response.data

# Testing passing in incorrect Content-Type for both endpoints
response = client.open("/", method="BREW")
assert response.status_code == 404
assert response.content_type == "application/json"
assert b"Move along home" in response.data

response = client.open("/teapot", method="BREW")
assert response.status_code == 404
assert response.content_type == "application/json"
assert b"Move along home" in response.data

0 comments on commit f22871c

Please sign in to comment.