 ;;;;
 
 (defn transit->clj
+  "Receives a byte array expected to contain transit+json, and coverts it
+  to a clojure data structure"
   [arr]
-  (let [in     arr
-        reader (transit/reader in :json)]
-    (transit/read reader)))
+  (try
+    (let [reader (transit/reader arr :json)]
+      (transit/read reader))
+    (catch Exception _ nil)))
+
+(defn clj->transit
+  [data]
+  (let [out    (ByteArrayOutputStream. 4096)
+        writer (transit/writer out :json)]
+    (transit/write writer data)
+    (.toString out)))
+
+(defn get-request
+  "Executes a GET request with an optional set of parameters. Returns
+  a vector with the response and the translated body"
+  ([url]
+   (get-request url nil))
+  ([url params]
+   (let [response (app (-> (request :get url params)
+                           (header "Accept" "application/transit+json")))]
+     [response (transit->clj (:body response))])))
 
 ;;;;
 ;;;; Tests

        (is (= String (type (:created e)))))
      ))
  (testing "Searching with a query filters the items"
    (let [response (app (-> (request :get "/api/memory/search?q=always")
                            (header "Accept" "application/transit+json")))
          clj-data (transit->clj (:body response))]
      (is response)
      (is (= 200 (:status response)))
      (is (= "application/transit+json" (get-in response [:headers "Content-Type"])))

(ns memento.test.routes.api
  (:require [clojure.test :refer :all]
            [cognitect.transit :as transit]
            [ring.mock.request :refer [request header body]]
            [memento.handler :refer [app]]
            [memento.test.db.core :as tdb]
            [memento.test.db.memory :as tdm])
  (:import java.io.ByteArrayOutputStream))


;;;;
