| // Copyright 2013 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| func ExampleResponseRecorder() { |
| handler := func(w http.ResponseWriter, r *http.Request) { |
| io.WriteString(w, "<html><body>Hello World!</body></html>") |
| req := httptest.NewRequest("GET", "http://example.com/foo", nil) |
| w := httptest.NewRecorder() |
| body, _ := ioutil.ReadAll(resp.Body) |
| fmt.Println(resp.StatusCode) |
| fmt.Println(resp.Header.Get("Content-Type")) |
| fmt.Println(string(body)) |
| // text/html; charset=utf-8 |
| // <html><body>Hello World!</body></html> |
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| fmt.Fprintln(w, "Hello, client") |
| res, err := http.Get(ts.URL) |
| greeting, err := ioutil.ReadAll(res.Body) |
| fmt.Printf("%s", greeting) |