Skip to content

Commit

Permalink
Merge pull request #33 from AndreaCrotti/teapot
Browse files Browse the repository at this point in the history
add teapot functionality
  • Loading branch information
ikitommi authored Jun 29, 2024
2 parents 3de24d3 + 44b3858 commit f2b7ef2
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dev/user.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns user
(:require [org.tobereplaced.lettercase :as lc]
[clojure.string :as str]
[stencil.core :as mustache]))

(def responses
Expand Down Expand Up @@ -43,6 +44,7 @@
[415 "Unsupported Media Type" "The request entity has a media type which the server or resource does not support."]
[416 "Requested Range Not Satisfiable" "The client has asked for a portion of the file but the server cannot supply that portion."]
[417 "Expectation Failed" "The server cannot meet the requirements of the Expect request-header field."]
[418 "I'm a teapot" "Any attempt to brew coffee with a teapot should result in the error code \"418 I'm a teapot\". The resulting entity body MAY be short and stout."]
[420 "Enhance Your Calm" "You are being rate-limited."]
[422 "Unprocessable Entity" "The request was well-formed but was unable to be followed due to semantic errors."]
[423 "Locked" "The resource that is being accessed is locked."]
Expand Down Expand Up @@ -86,7 +88,7 @@
(def template
(for [[status name description options] responses]
(merge
{:fn-name (lc/lower-hyphen-name name)
{:fn-name (str/replace (lc/lower-hyphen-name name) #"'" "")
:status status
:name name
:description description
Expand Down
6 changes: 6 additions & 0 deletions src/ring/util/http_predicates.clj
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@
{:pre (map? response)}
(= (:status response) 417))

(defn im-a-teapot?
"Checks whether the response has status code 418"
[response]
{:pre (map? response)}
(= (:status response) 418))

(defn enhance-your-calm?
"Checks whether the response has status code 420"
[response]
Expand Down
6 changes: 6 additions & 0 deletions src/ring/util/http_predicates.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@
{:pre (map? response)}
(= (:status response) 417))

(defn im-a-teapot?
"Checks whether the response has status code 418"
[response]
{:pre (map? response)}
(= (:status response) 418))

(defn enhance-your-calm?
"Checks whether the response has status code 420"
[response]
Expand Down
22 changes: 22 additions & 0 deletions src/ring/util/http_response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,28 @@
:headers {}
:body body})))

(defn im-a-teapot
"418 I'm a teapot (ClientError)
Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout."
([] (im-a-teapot nil))
([body]
{:status 418
:headers {}
:body body}))

(defn im-a-teapot!
"418 I'm a teapot (ClientError)
Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout.
Throws an exception with ex-info:
{:type :ring.util.http-response/response
:response response}"
([] (im-a-teapot! nil))
([body]
(throw!
{:status 418
:headers {}
:body body})))

(defn enhance-your-calm
"420 Enhance Your Calm (ClientError)
You are being rate-limited."
Expand Down
3 changes: 3 additions & 0 deletions src/ring/util/http_status.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
(def unsupported-media-type 415)
(def requested-range-not-satisfiable 416)
(def expectation-failed 417)
(def im-a-teapot 418)
(def enhance-your-calm 420)
(def unprocessable-entity 422)
(def locked 423)
Expand Down Expand Up @@ -147,6 +148,8 @@
:description "The client has asked for a portion of the file but the server cannot supply that portion."}
417 {:name "Expectation Failed"
:description "The server cannot meet the requirements of the Expect request-header field."}
418 {:name "I'm a teapot"
:description "Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout."}
420 {:name "Enhance Your Calm"
:description "You are being rate-limited."}
422 {:name "Unprocessable Entity"
Expand Down
3 changes: 3 additions & 0 deletions src/ring/util/http_status.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
(def unsupported-media-type 415)
(def requested-range-not-satisfiable 416)
(def expectation-failed 417)
(def im-a-teapot 418)
(def enhance-your-calm 420)
(def unprocessable-entity 422)
(def locked 423)
Expand Down Expand Up @@ -147,6 +148,8 @@
:description "The client has asked for a portion of the file but the server cannot supply that portion."}
417 {:name "Expectation Failed"
:description "The server cannot meet the requirements of the Expect request-header field."}
418 {:name "I'm a teapot"
:description "Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout."}
420 {:name "Enhance Your Calm"
:description "You are being rate-limited."}
422 {:name "Unprocessable Entity"
Expand Down
2 changes: 2 additions & 0 deletions test/ring/util/http_response_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
(is (= {:status 415 :headers {} :body "body"} (unsupported-media-type "body")))
(is (= {:status 416 :headers {} :body "body"} (requested-range-not-satisfiable "body")))
(is (= {:status 417 :headers {} :body "body"} (expectation-failed "body")))
(is (= {:status 418 :headers {} :body "body"} (im-a-teapot "body")))
(is (= {:status 420 :headers {} :body "body"} (enhance-your-calm "body")))
(is (= {:status 422 :headers {} :body "body"} (unprocessable-entity "body")))
(is (= {:status 423 :headers {} :body "body"} (locked "body")))
Expand Down Expand Up @@ -120,6 +121,7 @@
(is (slingshots? {:status 415 :headers {} :body "body"} (unsupported-media-type! "body")))
(is (slingshots? {:status 416 :headers {} :body "body"} (requested-range-not-satisfiable! "body")))
(is (slingshots? {:status 417 :headers {} :body "body"} (expectation-failed! "body")))
(is (slingshots? {:status 418 :headers {} :body "body"} (im-a-teapot! "body")))
(is (slingshots? {:status 420 :headers {} :body "body"} (enhance-your-calm! "body")))
(is (slingshots? {:status 422 :headers {} :body "body"} (unprocessable-entity! "body")))
(is (slingshots? {:status 423 :headers {} :body "body"} (locked! "body")))
Expand Down

0 comments on commit f2b7ef2

Please sign in to comment.