I'm writing a package which needs to pass an instance of a *gorequest.SuperAgent
to a method in a subpackage
// main.go
func main() {
req := gorequest.New()
result := subpackage.Method(req)
fmt.Println(result)
}
// subpackage.go
func Method(req *gorequest.SuperAgent) string {
req.Get("http://www.foo.com").Set("bar", "baz")
_, body, _ := req.End()
return body
}
I keep going in circles trying to write an interface for the gorequest superagent so I can properly isolate and test my subpackage methods with a stub of gorequest
.
type Getter Interface {
Get(url string) Getter
// In the previous Method, Get() returns a *gorequest.SuperAgent
// which allows chaining of methods
// Here I tried returning the interface itself
// But I get a 'wrong type for Get method' error when passing a gorequest instance
// have Get(string) *gorequest.SuperAgent
// want Get(string) Getter
End(callback ...func(response *gorequest.Response, body string, errs []error)) (*gorequest.Response, string, []error)
// I have no idea how to handle the param and returned *gorequest.Response here
// Put another interface ?
// Tried replacing it with *http.Response but not quite understanding it
}
func Method(req Getter) string {
...
}
So as you can see I'm tripping up on several points here and haven't been able to find a good source to learn from. Any pointers would be greatly appreciated
Besides defining the
Getter
interface you can also define a thin wrapper around*gorequest.SuperAgent
that implements theGetter
interface.Then with your
Method
defined as:You can use the
saGetter
in main like so:Then mocking
Getter
for testing theMethod
implementation is easy.That said, I agree with @JimB's comments that you probably don't need
gorequest
and usingnet/http
is generally the better choice.