PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/test/oauth/util_test.clj

http://github.com/r0man/oauth-clj
Clojure | 126 lines | 109 code | 16 blank | 1 comment | 23 complexity | a71acd2b0467154780aa4fb43f60011c MD5 | raw file
  1. (ns oauth.util-test
  2. (:import org.apache.http.entity.StringEntity)
  3. (:require [clojure.string :refer [blank?]]
  4. [clojure.test :refer :all]
  5. [oauth.twitter-test :refer :all]
  6. [oauth.util :refer :all]))
  7. (deftest test-byte-array?
  8. (is (not (byte-array? nil)))
  9. (is (not (byte-array? "")))
  10. (is (byte-array? (.getBytes "123"))))
  11. (deftest test-compact-map
  12. (is (= {} (compact-map {})))
  13. (is (= {:a "1"} (compact-map {:a "1"})))
  14. (is (= {:a "1"} (compact-map {:a "1" :b nil})))
  15. (is (= {:a "1"} (compact-map {:a "1" :b ""} blank?))))
  16. (deftest test-content-type
  17. (are [type expected]
  18. (is (= expected (content-type {:headers {"content-type" type}})))
  19. "text/javascript" "text/javascript"
  20. "text/javascript; charset=UTF-8" "text/javascript"))
  21. (deftest test-format-base-url
  22. (are [request expected]
  23. (is (= expected (format-base-url request)))
  24. twitter-update-status "https://api.twitter.com/1.1/statuses/update.json"))
  25. (deftest test-format-authorization
  26. (is (string? (format-authorization twitter-update-status))))
  27. (deftest test-format-query-params
  28. (are [params expected]
  29. (is (= expected (format-query-params params)))
  30. nil nil
  31. {} nil
  32. {:client-id "287169314674516"} "client_id=287169314674516"
  33. {"client_id" "287169314674516"} "client_id=287169314674516"
  34. {"client-id" "287169314674516"} "client-id=287169314674516"))
  35. (deftest test-format-http-method
  36. (are [request expected]
  37. (is (= expected (format-http-method request)))
  38. {:method :get} "GET"
  39. {:request-method :get} "GET"))
  40. (deftest test-format-options
  41. (is (= ["body=\"status%3DHello%2520Ladies%2520%252b%2520Gentlemen%252c%2520a%2520signed%2520OAuth%2520request%2521\""
  42. "method=\"%3Apost\""
  43. "oauth_consumer_key=\"xvz1evFS4wEEPTGEFPHBog\""
  44. "oauth_nonce=\"kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg\""
  45. "oauth_signature_method=\"HMAC-SHA1\""
  46. "oauth_timestamp=\"1318622958\""
  47. "oauth_token=\"370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb\""
  48. "oauth_version=\"1.0\""
  49. "query_params=\"%7B%3Ainclude_entities+true%7D\""
  50. "scheme=\"https\""
  51. "server_name=\"api.twitter.com\""
  52. "uri=\"%2F1.1%2Fstatuses%2Fupdate.json\""]
  53. (format-options twitter-update-status))))
  54. (deftest test-root-url
  55. (are [request expected]
  56. (is (= expected (root-url request)))
  57. twitter-update-status "https://api.twitter.com"))
  58. (deftest test-parse-respone
  59. (are [response expected]
  60. (is (= expected (parse-body response)))
  61. "oauth_token=Z6eEdO8MOmk394WozF5oKyuAv855l4Mlqo7hhlSLik&oauth_callback_confirmed=true"
  62. {:oauth-callback-confirmed "true" :oauth-token "Z6eEdO8MOmk394WozF5oKyuAv855l4Mlqo7hhlSLik"}))
  63. (deftest test-percent-encode
  64. (are [unencoded expected]
  65. (is (= expected (percent-encode unencoded)))
  66. "" ""
  67. "Ladies + Gentlemen" "Ladies%20%2B%20Gentlemen"
  68. "An encoded string!" "An%20encoded%20string%21"
  69. "Dogs, Cats & Mice" "Dogs%2C%20Cats%20%26%20Mice"
  70. "☃" "%E2%98%83"))
  71. ;
  72. (deftest test-parse-body-params
  73. (testing "body must be key/value pairs"
  74. (is (thrown? Exception (parse-body-params {:body "x"}))))
  75. (are [request expected]
  76. (is (= expected (parse-body-params request)))
  77. {} nil
  78. {:body ""} nil
  79. twitter-update-status
  80. {"status" "Hello Ladies + Gentlemen, a signed OAuth request!"}
  81. (assoc twitter-update-status :body (.getBytes (:body twitter-update-status)))
  82. {"status" "Hello Ladies + Gentlemen, a signed OAuth request!"}
  83. (assoc twitter-update-status :body "x=foo&y=bar")
  84. {"x" "foo" "y" "bar"}
  85. {:body (StringEntity. (:body twitter-update-status))}
  86. {"status" "Hello Ladies + Gentlemen, a signed OAuth request!"}))
  87. (deftest test-random-base64
  88. (is (string? (random-base64 1)))
  89. (is (not (= (random-base64 1) (random-base64 1)))))
  90. (deftest test-random-bytes
  91. (is (not (= (seq (random-bytes 1)) (seq (random-bytes 1))))))
  92. (deftest test-oauth-keys
  93. (are [map expected]
  94. (is (= expected (oauth-keys map)))
  95. {} []
  96. {:oauth-signature-method "HMAC-SHA1" "oauth_version" "1.0" :other-key "x"}
  97. [:oauth-signature-method "oauth_version"]))
  98. (deftest test-oauth-params
  99. (are [map expected]
  100. (is (= expected (oauth-params map)))
  101. {} {}
  102. {:oauth-signature-method "HMAC-SHA1" :oauth-version "1.0" :other-key "x"}
  103. {"oauth_signature_method" "HMAC-SHA1" "oauth_version" "1.0"}
  104. {:oauth_signature_method "HMAC-SHA1" :oauth-version "1.0" :q 1}
  105. {"oauth_signature_method" "HMAC-SHA1" "oauth_version" "1.0"}))
  106. (deftest test-wrap-content-type
  107. (is (= {:content-type x-www-form-urlencoded}
  108. ((wrap-content-type identity x-www-form-urlencoded) {})))
  109. (is (= {:content-type "text/plain"}
  110. ((wrap-content-type identity x-www-form-urlencoded) {:content-type "text/plain"}))))