Ive written a simple twitter client that makes a http Get call to twitter's search API and returns a JSON feed. I want to mock out a http.Get call to the twitter API:So far I havevar (twitterResponse = "{ 'results': [{'text':'hello','id_str':'34455w4','from_user_name':'bob','from_user_id_str':'345424'}]}")func TestdownloadTweets(t *testing.T) {ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {fmt.Fprintln(w, twitterResponse)}))defer ts.Close()url := ts.URL + "?q=%23UCL"r, err := http.Get(url)if err != nil {t.Fatalf("http.Get(%q) unexpected error: %v", url, err)}const expectedStatus = 200if r.StatusCode != expectedStatus {t.Fatalf("For %v, got status %d, want %d", url, r.StatusCode, expectedStatus)}}Would appreciate some pointers
Ive written a simple twitter client that makes a http Get call to twitter's search API and returns a JSON feed. I want to mock out a http.Get call to the twitter API:So far I havevar (twitterResponse = "{ 'results': [{'text':'hello','id_str':'34455w4','from_user_name':'bob','from_user_id_str':'345424'}]}")func TestdownloadTweets(t *testing.T) {ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {fmt.Fprintln(w, twitterResponse)}))defer ts.Close()url := ts.URL + "?q=%23UCL"r, err := http.Get(url)if err != nil {t.Fatalf("http.Get(%q) unexpected error: %v", url, err)}const expectedStatus = 200if r.StatusCode != expectedStatus {t.Fatalf("For %v, got status %d, want %d", url, r.StatusCode, expectedStatus)}}
Would appreciate some pointers
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
func closeDummyServer(dummy_server *httptest.Server) { transport = nil dummy_server.Close() } func createDummyServer(handler func(w http.ResponseWriter, r *http.Request)) *httptest.Server { dummy_server := httptest.NewServer(http.HandlerFunc(handler)) //change the host to use the test server SetTransport(&http.Transport{Proxy: func(*http.Request) (*url.URL, error) { return url.Parse(dummy_server.URL) }}) //turn off SSL UseSSL = false return dummy_server } func returnDummyResponseForPath(path string, dummy_response string, t *testing.T) *httptest.Server { //serve dummy responses dummy_data := []byte(dummy_response) return createDummyServer(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != path { fmt.Printf("path: %v", r.URL.Path) t.Error("Path doesn't match") } w.Write(dummy_data) }) }
I came up with this:var request = struct {path, query string // requestcontenttype, 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)
}}
On Friday, 26 April 2013 15:49:43 UTC+3, John Wesonga wrote:Ive written a simple twitter client that makes a http Get call to twitter's search API and returns a JSON feed. I want to mock out a http.Get call to the twitter API:So far I havevar (twitterResponse = "{ 'results': [{'text':'hello','id_str':'34455w4','from_user_name':'bob','from_user_id_str':'345424'}]}")func TestdownloadTweets(t *testing.T) {ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {fmt.Fprintln(w, twitterResponse)}))defer ts.Close()url := ts.URL + "?q=%23UCL"r, err := http.Get(url)if err != nil {t.Fatalf("http.Get(%q) unexpected error: %v", url, err)}const expectedStatus = 200if r.StatusCode != expectedStatus {t.Fatalf("For %v, got status %d, want %d", url, r.StatusCode, expectedStatus)}}
Would appreciate some pointers