Hi,
I'm trying to inject a DB connection into my HTTP handlers using net/http/httptest.
This is what I am currently doing, which seems to work:
// Create a handler instance with a reference to the DB
handler := New(db)
// Wrap the function I want to test
closure := http.HandlerFunc(func (w http.ResponseWriter, r *http.Request {handler.DoSomething(w,r)} )
// After creating the test server, I can execute my tests
server := httptest.NewServer(closure)
I'd like to somehow wrap the creation of the closure in order to reduce code verbosity. Is there some way to declare a function that takes my handler.DoSomething(w,r) as a pointer and wraps it with the http.Handler interface?
Or have I maybe misunderstood how to effectively use anonymous interface implementations?
Or is there potentially a better way to inject shared state into HTTP handlers?
Cheers,
Ben
--