github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/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  	"time"
    12  )
    13  
    14  func TestServer(t *testing.T) {
    15  	ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    16  		w.Write([]byte("hello"))
    17  	}))
    18  	defer ts.Close()
    19  	res, err := http.Get(ts.URL)
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	got, err := ioutil.ReadAll(res.Body)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	if string(got) != "hello" {
    28  		t.Errorf("got %q, want hello", string(got))
    29  	}
    30  }
    31  
    32  func TestIssue7264(t *testing.T) {
    33  	for i := 0; i < 1000; i++ {
    34  		func() {
    35  			inHandler := make(chan bool, 1)
    36  			ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    37  				inHandler <- true
    38  			}))
    39  			defer ts.Close()
    40  			tr := &http.Transport{
    41  				ResponseHeaderTimeout: time.Nanosecond,
    42  			}
    43  			defer tr.CloseIdleConnections()
    44  			c := &http.Client{Transport: tr}
    45  			res, err := c.Get(ts.URL)
    46  			<-inHandler
    47  			if err == nil {
    48  				res.Body.Close()
    49  			}
    50  		}()
    51  	}
    52  }