Created
April 17, 2019 00:37
-
-
Save ddeaguiar/defa8b2634c35cdf4db2ca626637db2f to your computer and use it in GitHub Desktop.
Stubbing session in Pedestal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns session.service | |
(:require [io.pedestal.http :as http] | |
[io.pedestal.http.route :as route] | |
[io.pedestal.http.body-params :as body-params] | |
[ring.util.response :as ring-resp])) | |
(defn about-page | |
[request] | |
(ring-resp/response (format "Clojure %s - served from %s" | |
(clojure-version) | |
(route/url-for ::about-page)))) | |
(defn home-page | |
[request] | |
(ring-resp/response (str (get-in request [:session :id])))) | |
;; Defines "/" and "/about" routes with their associated :get handlers. | |
;; The interceptors defined after the verb map (e.g., {:get home-page} | |
;; apply to / and its children (/about). | |
(def common-interceptors [(body-params/body-params) http/html-body]) | |
;; Tabular routes | |
(def routes #{["/" :get (conj common-interceptors `home-page)] | |
["/about" :get (conj common-interceptors `about-page)]}) | |
;; Map-based routes | |
;(def routes `{"/" {:interceptors [(body-params/body-params) http/html-body] | |
; :get home-page | |
; "/about" {:get about-page}}}) | |
;; Terse/Vector-based routes | |
;(def routes | |
; `[[["/" {:get home-page} | |
; ^:interceptors [(body-params/body-params) http/html-body] | |
; ["/about" {:get about-page}]]]]) | |
;; Consumed by session.server/create-server | |
;; See http/default-interceptors for additional options you can configure | |
(def service {:env :prod | |
;; You can bring your own non-default interceptors. Make | |
;; sure you include routing and set it up right for | |
;; dev-mode. If you do, many other keys for configuring | |
;; default interceptors will be ignored. | |
;; ::http/interceptors [] | |
::http/routes routes | |
;; Uncomment next line to enable CORS support, add | |
;; string(s) specifying scheme, host and port for | |
;; allowed source(s): | |
;; | |
;; "http://localhost:8080" | |
;; | |
;;::http/allowed-origins ["scheme://host:port"] | |
;; Tune the Secure Headers | |
;; and specifically the Content Security Policy appropriate to your service/application | |
;; For more information, see: https://content-security-policy.com/ | |
;; See also: https://github.com/pedestal/pedestal/issues/499 | |
;;::http/secure-headers {:content-security-policy-settings {:object-src "'none'" | |
;; :script-src "'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:" | |
;; :frame-ancestors "'none'"}} | |
;; Root for resource interceptor that is available by default. | |
::http/resource-path "/public" | |
::http/enable-session {} | |
;; Either :jetty, :immutant or :tomcat (see comments in project.clj) | |
;; This can also be your own chain provider/server-fn -- http://pedestal.io/reference/architecture-overview#_chain_provider | |
::http/type :jetty | |
;;::http/host "localhost" | |
::http/port 8080 | |
;; Options to pass to the container (Jetty) | |
::http/container-options {:h2c? true | |
:h2? false | |
;:keystore "test/hp/keystore.jks" | |
;:key-password "password" | |
;:ssl-port 8443 | |
:ssl? false}}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns session.service-test | |
(:require [clojure.test :refer :all] | |
[io.pedestal.test :refer :all] | |
[io.pedestal.http :as bootstrap] | |
[session.service :as service] | |
[ring.middleware.session.store :as session.store])) | |
;; Testing via `response-for` adapted from https://github.com/pedestal/pedestal/blob/09dd88c4ce7f89c7fbb7a398077eb970b3785d2d/service/test/io/pedestal/http/ring_middlewares_test.clj#L181-L212 | |
(defn make-session-store | |
[reader writer deleter] | |
(reify session.store/SessionStore | |
(read-session [_ k] (reader k)) | |
(write-session [_ k s] (writer k s)) | |
(delete-session [_ k] (deleter k)))) | |
(defn make-service-fn | |
[session-store] | |
(::bootstrap/service-fn (bootstrap/create-servlet (assoc service/service | |
::bootstrap/enable-session {:store session-store})))) | |
(deftest stub-session-store-test | |
(let [expected-id "some-id" | |
session-store (make-session-store (constantly {:id expected-id}) | |
(constantly nil) | |
(constantly nil)) | |
service-fn (make-service-fn session-store)] | |
(is (= expected-id (:body (response-for service-fn :get "/")))))) | |
(comment | |
(run-tests) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment