github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/net/http/httptest/server_test.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package httptest
     6  
     7  import (
     8  	"io/ioutil"
     9  	"net/http"
    10  	"testing"
    11  )
    12  
    13  func TestServer(t *testing.T) {
    14  	ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    15  		w.Write([]byte("hello"))
    16  	}))
    17  	defer ts.Close()
    18  	res, err := http.Get(ts.URL)
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	got, err := ioutil.ReadAll(res.Body)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	if string(got) != "hello" {
    27  		t.Errorf("got %q, want hello", string(got))
    28  	}
    29  }