func apiMock() *apimock.APIMock { a := apimock.NewAPIMock(true, logrus.New(), "json") route1 := apimock.URIMock{ Method: "GET", URI: "/hello", StatusCode: http.StatusOK, Response: "world", } route2 := apimock.URIMock{ Method: "GET", URI: "/struct", StatusCode: http.StatusOK, Response: tt{ Name: "TestName", Age: 39, }, } route3 := apimock.URIMock{ Method: "GET", URI: "/function", StatusCode: http.StatusOK, Response: testFunction(), } // add routes a.AddMock(&route1) a.AddMock(&route2) a.AddMock(&route3) return a }
func main() { a := apimock.NewAPIMock(true, logrus.New(), "json") route1 := apimock.URIMock{ Method: "GET", URI: "/hello", StatusCode: http.StatusOK, Response: "world", } a.AddMock(&route1) a.Add("GET", "/hi", 200, []byte("ho")) a.Start() defer a.Stop() b, r := httpCall("GET", fmt.Sprintf("%s/hello", a.URL())) fmt.Println("response:", string(b)) fmt.Println("status :", r.StatusCode) b, r = httpCall("GET", fmt.Sprintf("%s/hi", a.URL())) fmt.Println("response:", string(b)) fmt.Println("status :", r.StatusCode) }