http_mock

package main



import (
“fmt”
“io/ioutil”
“log”
“net/http”
“net/http/httptest” // 使用的包
)



func main() {
// mock 实现
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 这里构造 mock 的具体处理细节
fmt.Fprintln(w, “Hello, client”)
}))
defer ts.Close()



    // 请求第三方服务的逻辑
res, err := http.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
greeting, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}

fmt.Printf("%s", greeting) }


https://zhuanlan.zhihu.com/p/320135253

https://github.com/jarcoal/httpmock



httpmock.Activate()
defer httpmock.DeactivateAndReset()



// Exact URL match
httpmock.RegisterResponder(“GET”, “https://api.mybiz.com/articles”,
httpmock.NewStringResponder(200, [{"id": 1, "name": "My Great Article"}]))



https://www.cnblogs.com/Detector/p/9769840.html



https://github.com/goware/httpmock


Category golang