mercredi 5 avril 2017

How to mock http.Client Do method

I'm trying to find a solution to write test and mock HTTP response. In my function where I accept interface:

type HttpClient interface {
    Do(req *http.Request) (*http.Response, error)
}

I makes http get request with base auth

func GetOverview(client HttpClient, overview *Overview) (*Overview, error) {

    request, err := http.NewRequest("GET", fmt.Sprintf("%s:%s/api/overview", overview.Config.Url, overview.Config.Port), nil)
    if (err != nil) {
        log.Println(err)
    }
    request.SetBasicAuth(overview.Config.User, overview.Config.Password)
    resp, err := client.Do(request)

How can I mock this HttpClient? I'm looking for mock library, for instance: http://ift.tt/21ErDB5 but there is only mock for Get and Post

Maybe I should do it in a different way. I'll be grateful for advice

Aucun commentaire:

Enregistrer un commentaire