|
7 | 7 | [clojure.string :as str] |
8 | 8 | [clojure.java.io :refer [resource]] |
9 | 9 | [clojure.test :refer :all] |
| 10 | + [cognitect.transit :as transit] |
10 | 11 | [ring.util.codec :refer [form-decode-str]] |
11 | 12 | [ring.middleware.nested-params :refer [parse-nested-keys]]) |
12 | 13 | (:import (java.net UnknownHostException) |
|
467 | 468 | client/parse-url |
468 | 469 | :user-info)))) |
469 | 470 |
|
| 471 | +(defrecord Point [x y]) |
| 472 | + |
| 473 | +(def write-point |
| 474 | + "Write a point in Transit format." |
| 475 | + (transit/write-handler |
| 476 | + (constantly "point") |
| 477 | + (fn [point] [(:x point) (:y point)]) |
| 478 | + (constantly nil))) |
| 479 | + |
| 480 | +(def read-point |
| 481 | + "Read a point in Transit format." |
| 482 | + (transit/read-handler |
| 483 | + (fn [[x y]] |
| 484 | + (->Point x y)))) |
| 485 | + |
| 486 | +(def transit-opts |
| 487 | + "Transit read and write options." |
| 488 | + {:encode {:handlers {Point write-point}} |
| 489 | + :decode {:handlers {"point" read-point}}}) |
| 490 | + |
470 | 491 | (deftest apply-on-form-params |
471 | 492 | (testing "With form params" |
472 | 493 | (let [param-client (client/wrap-form-params identity) |
|
522 | 543 | (testing "With EDN form params" |
523 | 544 | (doseq [method [:post :put :patch]] |
524 | 545 | (let [param-client (client/wrap-form-params identity) |
525 | | - params {:param1 "value1" :param2 "value2"} |
| 546 | + params {:param1 "value1" :param2 (Point. 1 2)} |
526 | 547 | resp (param-client {:request-method method |
527 | 548 | :content-type :edn |
528 | 549 | :form-params params})] |
|
533 | 554 | (testing "With Transit/JSON form params" |
534 | 555 | (doseq [method [:post :put :patch]] |
535 | 556 | (let [param-client (client/wrap-form-params identity) |
536 | | - params {:param1 "value1" :param2 "value2"} |
| 557 | + params {:param1 "value1" :param2 (Point. 1 2)} |
537 | 558 | resp (param-client {:request-method method |
538 | 559 | :content-type :transit+json |
539 | | - :form-params params})] |
| 560 | + :form-params params |
| 561 | + :transit-opts transit-opts})] |
540 | 562 | (is (= params (client/parse-transit |
541 | | - (ByteArrayInputStream. (:body resp)) :json))) |
| 563 | + (ByteArrayInputStream. (:body resp)) |
| 564 | + :json transit-opts))) |
542 | 565 | (is (= "application/transit+json" (:content-type resp))) |
543 | 566 | (is (not (contains? resp :form-params)))))) |
544 | 567 |
|
|
548 | 571 | params {:param1 "value1" :param2 "value2"} |
549 | 572 | resp (param-client {:request-method method |
550 | 573 | :content-type :transit+msgpack |
551 | | - :form-params params})] |
| 574 | + :form-params params |
| 575 | + :transit-opts transit-opts})] |
552 | 576 | (is (= params (client/parse-transit |
553 | | - (ByteArrayInputStream. (:body resp)) :msgpack))) |
| 577 | + (ByteArrayInputStream. (:body resp)) |
| 578 | + :msgpack transit-opts))) |
554 | 579 | (is (= "application/transit+msgpack" (:content-type resp))) |
555 | 580 | (is (not (contains? resp :form-params)))))) |
556 | 581 |
|
|
0 commit comments