Skip to content

Instantly share code, notes, and snippets.

@atedja
Last active August 29, 2015 14:12
Show Gist options
  • Save atedja/201e8fc431e30d9a3e8a to your computer and use it in GitHub Desktop.
Save atedja/201e8fc431e30d9a3e8a to your computer and use it in GitHub Desktop.
How to use httptest in Go
import "net/http/httptest"
import "net/http"
type DummyHttp struct {
}
func (d DummyHttp) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "text/plain")
rw.Write([]byte("Result from Dummy Server"))
}
func NewServer() *httptest.Server {
return httptest.NewServer(&DummyHttp{})
}
func main() {
server := NewServer()
defer server.Close()
url := server.URL
// Do something with server
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment