mirror of https://github.com/golang/go.git
net/http/httptest: add examples
R=golang-dev, adg, bradfitz CC=golang-dev https://golang.org/cl/7314046
This commit is contained in:
parent
5fa6721a31
commit
f26fc0c017
|
|
@ -0,0 +1,50 @@
|
|||
// 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.
|
||||
|
||||
package httptest_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
func ExampleRecorder() {
|
||||
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "something failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", "http://example.com/foo", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler(w, req)
|
||||
|
||||
fmt.Printf("%d - %s", w.Code, w.Body.String())
|
||||
// Output: 500 - something failed
|
||||
}
|
||||
|
||||
func ExampleServer() {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Hello, client")
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
res, err := http.Get(ts.URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
greeting, err := ioutil.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("%s", greeting)
|
||||
// Output: Hello, client
|
||||
}
|
||||
Loading…
Reference in New Issue