Correctly using httptest to mock responses
<p>I have something that looks like so:</p>

<pre><code>func (client *MyCustomClient) CheckURL(url string, json_response *MyCustomResponseStruct) bool {
     r, err = http.Get(url)
     if err != nil {
         return false
     }
     defer r.Body.Close()
     .... do stuff with json_response
</code></pre>

<p>And in my test, I have the following:</p>

<pre><code>  func TestCheckURL(t *test.T) {
       ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
           w.Header().Set("Content-Type", "text/html; charset=UTF-8")
           fmt.Fprintln(w, `{"status": "success"}`)
       }))
       defer ts.Close()

       json_response := new(MyCustomResponseStruct)
       client := NewMyCustomClient()  // returns instance of MyCustomClient
       done := client.CheckURL("test.com", json_response)
</code></pre>

<p>However, it does not appear as if the HTTP test server is working and that it actually goes out to test.com, as evidenced by the log output:</p>

<pre><code> Get http:/test.com: dial tcp X.Y.Z.A: i/o timeout
</code></pre>

<p>My question is how to properly use the httptest package to mock out this request... I read through <a href="https://golang.org/pkg/net/http/httptest/" rel="nofollow noreferrer">the docs</a> and this helpful <a href="https://stackoverflow.com/questions/16154999/how-to-test-http-calls-in-go-using-httptest">SO Answer</a> but I'm still stuck.</p>