How to test http calls in Go using httptest

后端 未结 3 1775
北恋
北恋 2020-12-04 06:09

I have the following code:

package main

import (
    \"encoding/json\"
    \"fmt\"
    \"io/ioutil\"
    \"log\"
    \"net/http\"
    \"time\"
)

type twitt         


        
3条回答
  •  独厮守ぢ
    2020-12-04 06:54

    If you want to test your program, it is often best to write it with testing in mind. For instance, if you extracted the inner loop of your retrieveTweets function into something like this:

    func downloadTweets(tweetsUrl string) (*twitterResult, error)
    

    You could invoke it with the URL of a test server you've set up using the httptest package without having to worry about the sleeps or repeated requests.

提交回复
热议问题