Skip to content

Commit

Permalink
Set maximum of 'limit' query parameter to 100
Browse files Browse the repository at this point in the history
If a limit greater than 100 is used the user is redirected to the same
page with a limit of 100.

Relates to innoq#184
  • Loading branch information
mvitz committed Aug 16, 2016
1 parent b2bcf2e commit 997ab5e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
19 changes: 15 additions & 4 deletions src/statuses/routes.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
(ns statuses.routes
(:require [statuses.configuration :refer [config]]))
(:require [clojure.string :as s]
[statuses.configuration :refer [config]]))

(defn query-params [params]
(let [filtered-params (into {} (remove (comp nil? second)) params)]
(if (empty? filtered-params)
""
(->> filtered-params
(map #(str (name (key %)) "=" (val %)))
(s/join "&")
(str "?")))))

(def base-template "/statuses")
(defn base-path [] base-template)

(def updates-template (str base-template "/updates"))
(defn updates-path
([] (updates-path nil))
([response-format]
([] (updates-path {}))
([params]
(str updates-template
(if response-format (str "?format=" (name response-format)) ""))))
(query-params (select-keys params
[:limit :offset :author :query :format])))))

(def update-template (str updates-template "/:id"))
(defn update-path [id] (str (updates-path) "/" id))
Expand Down
38 changes: 20 additions & 18 deletions src/statuses/routing.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,26 @@
(defn updates-page [params request]
(let [next (next-uri (update-in params [:offset] (partial + (:limit params))) request)
{:keys [limit offset author query format]} params]
(with-etag request (:time (first (core/get-latest @db 1 offset author query)))
(let [items (core/label-updates :can-delete?
(partial core/can-delete? @db (user request))
(core/get-latest @db limit offset author query))]
(cond
(= format "json") (content-type
"application/json"
(json/as-json {:items (json-decorator/decorate items), :next next}))
(= format "atom") (content-type
"application/atom+xml;charset=utf-8"
(atom/render-atom items
(str (base-uri request) "/statuses")
(str (base-uri request)
"/statuses/updates?"
(:query-string request))))
:else (content-type
"text/html;charset=utf-8"
(list-page items next (user request) nil)))))))
(if (> limit 100)
(redirect (route/updates-path (assoc params :limit 100)))
(with-etag request (:time (first (core/get-latest @db 1 offset author query)))
(let [items (core/label-updates :can-delete?
(partial core/can-delete? @db (user request))
(core/get-latest @db limit offset author query))]
(cond
(= format "json") (content-type
"application/json"
(json/as-json {:items (json-decorator/decorate items), :next next}))
(= format "atom") (content-type
"application/atom+xml;charset=utf-8"
(atom/render-atom items
(str (base-uri request) "/statuses")
(str (base-uri request)
"/statuses/updates?"
(:query-string request))))
:else (content-type
"text/html;charset=utf-8"
(list-page items next (user request) nil))))))))

(defn new-update
"Handles the request to add a new update. Checks whether the post values 'entry-text' or
Expand Down
20 changes: 20 additions & 0 deletions test/statuses/test/routes.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns statuses.test.routes
(:require [clojure.test :refer [deftest is]]
[statuses.routes :as sut]))

(deftest test-query-params
(is (=
(sut/query-params {})
""))
(is (=
(sut/query-params {:foo "bar"})
"?foo=bar"))
(is (=
(sut/query-params {:foo "bar" :bar "foo"})
"?foo=bar&bar=foo"))
(is (=
(sut/query-params {:foo "bar" :bar nil})
"?foo=bar"))
(is (=
(sut/query-params {:foo nil :bar nil}))))

0 comments on commit 997ab5e

Please sign in to comment.