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

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"log"
     8  	"net/http"
     9  	"net/http/httptest"
    10  )
    11  
    12  func main() {
    13  	// START OMIT
    14  	handler := func(w http.ResponseWriter, r *http.Request) {
    15  		http.Error(w, "something failed", http.StatusInternalServerError)
    16  	}
    17  
    18  	req, err := http.NewRequest("GET", "http://example.com/foo", nil)
    19  	if err != nil {
    20  		log.Fatal(err)
    21  	}
    22  
    23  	w := httptest.NewRecorder()
    24  	handler(w, req)
    25  
    26  	fmt.Printf("%d - %s", w.Code, w.Body.String())
    27  	// STOP OMIT
    28  }