-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from zhuangxm/inject-request-parameters
using the idea of the web_func to inject params to function
- Loading branch information
Showing
8 changed files
with
192 additions
and
85 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,45 @@ | ||
(ns clj-rpc.test.context | ||
(:use [clj-rpc.context :reload true] | ||
[clojure.test]) | ||
[clojure.test] | ||
[midje.sweet]) | ||
(:require [clj-rpc.command :as command])) | ||
|
||
|
||
(deftest check-add-context-to-command-map | ||
(let [command-map {"str" (command/mk-command "str" #'str) | ||
"concat" (command/mk-command "concat" #'concat)} | ||
options {:require-context true :params-check {0 1}} | ||
options [ [:require-context true] [:params-check {0 1}]] | ||
new-map (add-context-to-command-map command-map options) | ||
vs (vals new-map)] | ||
(is (= (get-in (first vs) [:params-check]) {0 1})) | ||
(is (= (get-in (second vs) [:params-check]) {0 1})))) | ||
(is (= (get (first vs) :options) options)) | ||
(is (= (get (second vs) :options) options)))) | ||
|
||
(deftest test-check-context | ||
(facts | ||
"check whether or not the command and params statifies context requirement" | ||
(let [cmd (-> (command/mk-command "concat" #'concat) | ||
(add-context {:require-context true | ||
:params-checks {0 [:p1] 1 [:p2]}})) | ||
cmd-not-require (command/mk-command "str" #'str)] | ||
(is (nil? (check-context cmd {:p1 [1 2] :p2 [3 4]} | ||
[ [1 2] [3 4] [5 6]])) | ||
"stastify authorized and params requirement") | ||
(is (= :unauthorized (:code (check-context cmd nil [ [1 2] [3 4] [5 6]]))) | ||
"do not statify authorization requirement") | ||
(is (= :unauthorized (:code (check-context cmd {} [ [1 2] [3 4] [5 6]]))) | ||
"do not statify authorization requirement") | ||
(is (= :invalid-params (:code (check-context cmd {:p1 [1 2]} | ||
[ [1 2] [3 4] [5 6]]))) | ||
"do not statify params check requirement") | ||
(is (nil? (check-context cmd-not-require nil ["str1" "str2"])) | ||
"do not require context check"))) | ||
(add-context [ [:require-context true] | ||
[:params-check {0 [:p1] 1 [:p2]}]])) | ||
cmd-not-require (command/mk-command "str" #'str) | ||
method-request {:method "method" :params [ [1 2] [3 4] [5 6]]}] | ||
;;stastify authorized and params requirement | ||
(adjust-method-request cmd {:context {:p1 [1 2] :p2 [3 4]}} | ||
method-request) => method-request | ||
|
||
;;do not statify authorization requirement | ||
(adjust-method-request cmd nil method-request) => (contains {:error {:code :unauthorized}}) | ||
(adjust-method-request cmd {} method-request) => (contains {:error {:code :unauthorized}}) | ||
|
||
;;do not statify params check requirement | ||
(get-in (adjust-method-request cmd {:context {:p1 [1 2] }} method-request) | ||
[:error :code]) => :invalid-params | ||
|
||
;;do not require context check | ||
(adjust-method-request cmd-not-require nil {:method "method" :params ["str1" "str2"]}) | ||
=> {:method "method" :params ["str1" "str2"]})) | ||
|
||
;.;. The work itself praises the master. -- CPE Bach | ||
(facts "test inject parasm" | ||
(let [cmd (-> (command/mk-command "str" #'str) | ||
(add-context {:params-inject [ [:remote-addr] [:inject]]})) ] | ||
(adjust-method-request cmd {:remote-addr "192.168.1.1" :inject "inject-param2"} | ||
{:method "method" :params ["p1" "p2"]}) | ||
=> {:method "method" :params ["192.168.1.1" "inject-param2" "p1" "p2"]})) |
Oops, something went wrong.