|
280 | 280 | (deftest apply-on-accept |
281 | 281 | (is-applied client/wrap-accept |
282 | 282 | {:accept :json} |
283 | | - {:headers {"accept" "application/json"}})) |
| 283 | + {:headers {"accept" "application/json"}}) |
| 284 | + (is-applied client/wrap-accept |
| 285 | + {:accept :transit+json} |
| 286 | + {:headers {"accept" "application/transit+json"}}) |
| 287 | + (is-applied client/wrap-accept |
| 288 | + {:accept :transit+msgpack} |
| 289 | + {:headers {"accept" "application/transit+msgpack"}})) |
284 | 290 |
|
285 | 291 | (deftest pass-on-no-accept |
286 | 292 | (is-passed client/wrap-accept |
|
349 | 355 | (is-applied client/wrap-content-type |
350 | 356 | {:content-type :json :character-encoding "UTF-8"} |
351 | 357 | {:headers {"content-type" "application/json; charset=UTF-8"} |
352 | | - :content-type :json :character-encoding "UTF-8"})) |
| 358 | + :content-type :json :character-encoding "UTF-8"}) |
| 359 | + (is-applied client/wrap-content-type |
| 360 | + {:content-type :transit+json} |
| 361 | + {:headers {"content-type" "application/transit+json"} |
| 362 | + :content-type :transit+json}) |
| 363 | + (is-applied client/wrap-content-type |
| 364 | + {:content-type :transit+msgpack} |
| 365 | + {:headers {"content-type" "application/transit+msgpack"} |
| 366 | + :content-type :transit+msgpack})) |
353 | 367 |
|
354 | 368 | (deftest pass-on-no-content-type |
355 | 369 | (is-passed client/wrap-content-type |
|
437 | 451 | (is (= "param1=value1¶m2=value2" (:body resp))) |
438 | 452 | (is (= "application/x-www-form-urlencoded" (:content-type resp))) |
439 | 453 | (is (not (contains? resp :form-params))))) |
| 454 | + |
440 | 455 | (testing "With json form params" |
441 | 456 | (let [param-client (client/wrap-form-params identity) |
442 | 457 | params {:param1 "value1" :param2 "value2"} |
|
471 | 486 | (is (= (json/encode params {:date-format "yyyy-MM-dd"}) (:body resp))) |
472 | 487 | (is (= "application/json" (:content-type resp))) |
473 | 488 | (is (not (contains? resp :form-params))))) |
| 489 | + |
| 490 | + (testing "With EDN form params" |
| 491 | + (doseq [method [:post :put :patch]] |
| 492 | + (let [param-client (client/wrap-form-params identity) |
| 493 | + params {:param1 "value1" :param2 "value2"} |
| 494 | + resp (param-client {:request-method method |
| 495 | + :content-type :edn |
| 496 | + :form-params params})] |
| 497 | + (is (= (pr-str params) (:body resp))) |
| 498 | + (is (= "application/edn" (:content-type resp))) |
| 499 | + (is (not (contains? resp :form-params)))))) |
| 500 | + |
| 501 | + (testing "With Transit/JSON form params" |
| 502 | + (doseq [method [:post :put :patch]] |
| 503 | + (let [param-client (client/wrap-form-params identity) |
| 504 | + params {:param1 "value1" :param2 "value2"} |
| 505 | + resp (param-client {:request-method method |
| 506 | + :content-type :transit+json |
| 507 | + :form-params params})] |
| 508 | + (is (= params (client/parse-transit (ByteArrayInputStream. (:body resp)) :json))) |
| 509 | + (is (= "application/transit+json" (:content-type resp))) |
| 510 | + (is (not (contains? resp :form-params)))))) |
| 511 | + |
| 512 | + (testing "With Transit/MessagePack form params" |
| 513 | + (doseq [method [:post :put :patch]] |
| 514 | + (let [param-client (client/wrap-form-params identity) |
| 515 | + params {:param1 "value1" :param2 "value2"} |
| 516 | + resp (param-client {:request-method method |
| 517 | + :content-type :transit+msgpack |
| 518 | + :form-params params})] |
| 519 | + (is (= params (client/parse-transit (ByteArrayInputStream. (:body resp)) :msgpack))) |
| 520 | + (is (= "application/transit+msgpack" (:content-type resp))) |
| 521 | + (is (not (contains? resp :form-params)))))) |
| 522 | + |
474 | 523 | (testing "Ensure it does not affect GET requests" |
475 | 524 | (let [param-client (client/wrap-form-params identity) |
476 | 525 | resp (param-client {:request-method :get |
|
479 | 528 | :param2 "value2"}})] |
480 | 529 | (is (= "untouched" (:body resp))) |
481 | 530 | (is (not (contains? resp :content-type))))) |
| 531 | + |
482 | 532 | (testing "with no form params" |
483 | 533 | (let [param-client (client/wrap-form-params identity) |
484 | 534 | resp (param-client {:body "untouched"})] |
|
616 | 666 | (let [json-body (ByteArrayInputStream. (.getBytes "{\"foo\":\"bar\"}")) |
617 | 667 | auto-body (ByteArrayInputStream. (.getBytes "{\"foo\":\"bar\"}")) |
618 | 668 | edn-body (ByteArrayInputStream. (.getBytes "{:foo \"bar\"}")) |
| 669 | + transit-json-body (ByteArrayInputStream. (.getBytes "[\"^ \",\"~:foo\",\"bar\"]")) |
| 670 | + transit-msgpack-body (->> (map byte [-127 -91 126 58 102 111 111 -93 98 97 114]) |
| 671 | + (byte-array 11) |
| 672 | + (ByteArrayInputStream.)) |
619 | 673 | json-resp {:body json-body :status 200 |
620 | 674 | :headers {"content-type" "application/json"}} |
621 | 675 | auto-resp {:body auto-body :status 200 |
622 | 676 | :headers {"content-type" "application/json"}} |
623 | 677 | edn-resp {:body edn-body :status 200 |
624 | | - :headers {"content-type" "application/edn"}}] |
| 678 | + :headers {"content-type" "application/edn"}} |
| 679 | + transit-json-resp {:body transit-json-body :status 200 |
| 680 | + :headers {"content-type" "application/transit-json"}} |
| 681 | + transit-msgpack-resp {:body transit-msgpack-body :status 200 |
| 682 | + :headers {"content-type" "application/transit-msgpack"}}] |
625 | 683 | (is (= {:foo "bar"} |
626 | 684 | (:body (client/coerce-response-body {:as :json} json-resp)) |
627 | 685 | (:body (client/coerce-response-body {:as :clojure} edn-resp)) |
628 | | - (:body (client/coerce-response-body {:as :auto} auto-resp)))))) |
| 686 | + (:body (client/coerce-response-body {:as :auto} auto-resp)) |
| 687 | + (:body (client/coerce-response-body {:as :transit+json} transit-json-resp)) |
| 688 | + (:body (client/coerce-response-body {:as :transit+msgpack} transit-msgpack-resp)))))) |
629 | 689 |
|
630 | 690 | (deftest ^:integration t-with-middleware |
631 | 691 | (run-server) |
|
0 commit comments