github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/testing/httpserver.go (about)

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"log"
     9  	"net/http"
    10  	"net/http/httptest"
    11  )
    12  
    13  func main() {
    14  	// START OMIT
    15  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    16  		fmt.Fprintln(w, "Hello, client")
    17  	}))
    18  	defer ts.Close()
    19  
    20  	res, err := http.Get(ts.URL)
    21  	if err != nil {
    22  		log.Fatal(err)
    23  	}
    24  
    25  	greeting, err := ioutil.ReadAll(res.Body)
    26  	res.Body.Close()
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  
    31  	fmt.Printf("%s", greeting)
    32  	// STOP OMIT
    33  }