Skip to content

Instantly share code, notes, and snippets.

@0xhaven
Created March 10, 2015 00:58
Show Gist options
  • Select an option

  • Save 0xhaven/fdfca26af8e248e1c693 to your computer and use it in GitHub Desktop.

Select an option

Save 0xhaven/fdfca26af8e248e1c693 to your computer and use it in GitHub Desktop.
Simple HTTP Test
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/some/api/string", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.UserAgent())
})
srv := httptest.NewUnstartedServer(mux)
go srv.Start()
for srv.URL == "" {
fmt.Print(".")
}
fmt.Println()
resp, err := http.Get(srv.URL + "/some/api/string")
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", body)
srv.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment