title: How to test http calls in Go using httptest
body: <p>I have the following code:</p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
      "io/ioutil"
      "log"
      "net/http"
      "time"
  )
  
  type twitterResult struct {
      Results []struct {
          Text     string `json:"text"`
          Ids      string `json:"id_str"`
          Name     string `json:"from_user_name"`
          Username string `json:"from_user"`
          UserId   string `json:"from_user_id_str"`
      }
  }
  
  var (
    twitterUrl = "http://search.twitter.com/search.json?q=%23UCL"
    pauseDuration = 5 * time.Second
  )
  
  func retrieveTweets(c chan&lt;- *twitterResult) {
      for {
          resp, err := http.Get(twitterUrl)
          if err != nil {
              log.Fatal(err)
          }
  
          defer resp.Body.Close()
          body, err := ioutil.ReadAll(resp.Body)
          r := new(twitterResult) //or &amp;twitterResult{} which returns *twitterResult
          err = json.Unmarshal(body, &amp;r)
          if err != nil {
              log.Fatal(err)
          }
          c &lt;- r
          time.Sleep(pauseDuration)
      }
  
  }
  
  func displayTweets(c chan *twitterResult) {
      tweets := &lt;-c
      for _, v := range tweets.Results {
          fmt.Printf("%v:%v\n", v.Username, v.Text)
      }
  
  }
  
  func main() {
      c := make(chan *twitterResult)
      go retrieveTweets(c)
      for {
          displayTweets(c)
      }
  
  }
  </code></pre>
  
  <p>I'd like to write some tests for it, but I'm not sure how to use the httptest package <a href="http://golang.org/pkg/net/http/httptest/">http://golang.org/pkg/net/http/httptest/</a> would appreciate some pointers </p>
  
  <p>I came up with this (shamelessly copied from the tests for go OAuth <a href="https://code.google.com/p/goauth2/source/browse/oauth/oauth_test.go">https://code.google.com/p/goauth2/source/browse/oauth/oauth_test.go</a>):</p>
  
  <pre><code>var request = struct {
      path, query       string // request
      contenttype, body string // response
  }{
      path:        "/search.json?",
      query:       "q=%23Kenya",
      contenttype: "application/json",
      body:        twitterResponse,
  }
  
  var (
      twitterResponse = `{ 'results': [{'text':'hello','id_str':'34455w4','from_user_name':'bob','from_user_id_str':'345424'}]}`
  )
  
  func TestRetrieveTweets(t *testing.T) {
      handler := func(w http.ResponseWriter, r *http.Request) {
  
          w.Header().Set("Content-Type", request.contenttype)
          io.WriteString(w, request.body)
      }
  
      server := httptest.NewServer(http.HandlerFunc(handler))
      defer server.Close()
  
      resp, err := http.Get(server.URL)
      if err != nil {
          t.Fatalf("Get: %v", err)
      }
      checkBody(t, resp, twitterResponse)
  }
  
  func checkBody(t *testing.T, r *http.Response, body string) {
      b, err := ioutil.ReadAll(r.Body)
      if err != nil {
          t.Error("reading reponse body: %v, want %q", err, body)
      }
      if g, w := string(b), body; g != w {
          t.Errorf("request body mismatch: got %q, want %q", g, w)
      }
  }
  </code></pre>