| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"net/http/httptest" |
| 8 |
) |
| 9 |
|
| 10 |
func mock(name string) http.HandlerFunc { |
| 11 |
return func(rw http.ResponseWriter, req *http.Request) { |
| 12 |
fmt.Fprint(rw, "Hello ", name) |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
func client(uri string) { |
| 17 |
resp, err := http.Get(uri) |
| 18 |
if err != nil { |
| 19 |
panic(err) |
| 20 |
} |
| 21 |
body, err := io.ReadAll(resp.Body) |
| 22 |
if err != nil { |
| 23 |
panic(err) |
| 24 |
} |
| 25 |
fmt.Println(string(body)) |
| 26 |
} |
| 27 |
|
| 28 |
func main() { |
| 29 |
mux := http.NewServeMux() |
| 30 |
server := httptest.NewServer(mux) |
| 31 |
defer server.Close() |
| 32 |
mux.Handle("/", mock("foo")) |
| 33 |
client(server.URL) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|