             [clj-http.client :as http-client]
             [freefrog.rest :as r]))
 
+(defn http-post-request 
+  ([uri body]
+   (http-post-request uri body nil))
+  ([uri body options]
+   (http-client/post (str "http://localhost:3000/" uri) 
+                     (merge {:throw-exceptions false
+                             :content-type :json
+                             :body body} options))))
+
+(defn http-get-request 
+  ([]
+   (http-get-request "" nil))
+  ([uri]
+   (http-get-request uri nil))
+  ([uri options]
+   (http-client/get (str "http://localhost:3000/" uri) (merge {:throw-exceptions false} options))))
+
 (defn should-return-400 [spec-context circle-content response-content]
   (context spec-context
-    (with response (http-client/post "http://localhost:3000/circles" 
-                                    {:throw-exceptions false
-                                     :content-type :json
-                                     :body circle-content}))
+    (with response (http-post-request "circles" circle-content))
     (it "should return a bad request"
       (should= 400 (:status @response))
       (should-contain response-content (:body @response)))))

  (after (r/reset-database))

  (it "should return status code 404 at the root"
    (should= 404 (:status (http-client/get "http://localhost:3000" {:throw-exceptions false}))))

  (context "circles"
    (context "when no circles have been created"
      (with response (http-client/get "http://localhost:3000/circles" {:throw-exceptions false}))
      (it "should return an empty array"
          (should= 200 (:status @response))
          (should= "[]" (:body @response))))
  
    (context "when creating a circle"
      (with response (http-client/post "http://localhost:3000/circles" 
                                      {:throw-exceptions false
                                       :content-type :json
                                       :body (json/generate-string 
                                               {:name "Test Circle!"
                                                :lead-link-name "Bill"
                                                :lead-link-email "bfinn@example.com"})}))
      (it "should redirect to the location of the newly created resource"
        (should= 303 (:status @response))
        (should-contain "application/json" (get-in @response [:headers "Content-Type"]))

                   (json/parse-string (:body @get-response)))))
  
    (context "when creating multiple circles"
      (before (http-post-request "circles" 
                                 (json/generate-string 
                                   {:name "Test Circle!"
                                    :lead-link-name "Bill"
                                    :lead-link-email "bfinn@example.com"})
                                 {:throw-exceptions true}))

      (before (http-post-request "circles" 
                                 (json/generate-string 
                                   {:name "Test Circle 2"
                                    :lead-link-name "Bill"
                                    :lead-link-email "bfinn@example.com"})
                                 {:throw-exceptions true}))

      (with get-response (http-get-request "circles"))

      (it "should return an array of created circles"
        (should= 200 (:status @get-response))
