Created
March 10, 2015 00:58
-
-
Save 0xhaven/fdfca26af8e248e1c693 to your computer and use it in GitHub Desktop.
Simple HTTP Test
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
| 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