Last active
August 29, 2015 14:12
-
-
Save atedja/201e8fc431e30d9a3e8a to your computer and use it in GitHub Desktop.
How to use httptest in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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