Share
Embed
Transcript
48 slides
-
kaneshin (Shintaro Kaneko) - CTO of Eureka, Inc. - Twitter:
@kaneshin0120 - Gopher, Vimmer, Photographer, Mathematician -
Today's special var sleepHandler = func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Hour) w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
net/http/httptest var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
net/http/httptest var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) http://blog.kaneshin.co/entry/2016/12/02/200108 net/http/httptest.Server net/http/httptest.ResponseRecorder -
net/http/httptest var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { } -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello world!")) } -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) w.Write([]byte("Hello world!")) } -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { w.Header("X-Foo", "bar") w.WriteHeader(http.StatusCreated) w.Write([]byte("Hello world!")) } -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { w.Header("X-Foo", "bar") w.WriteHeader(http.StatusCreated) w.Write([]byte("Hello world!")) // ❌ w.WriteHeader(http.StatusBadRequest) after a call to Write // ❌ w.Header("X-Foo", "qux") after a call to WriteHeader } -
net/http.ResponseWriter var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) /// package http -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) /// package http func Get(url string) (resp *Response, err error) { return DefaultClient.Get(url) } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) /// package http func Get(url string) (resp *Response, err error) { return DefaultClient.Get(url) } var DefaultClient = &Client{} -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) /// package http func Get(url string) (resp *Response, err error) { return DefaultClient.Get(url) } var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) /// package http var DefaultTransport RoundTripper = &Transport{ Proxy: ProxyFromEnvironment, DialContext: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, DualStack: true, }).DialContext, MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar // A Timeout of zero means no timeout. Timeout time.Duration } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar // A Timeout of zero means no timeout. Timeout time.Duration } func init() { } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar // A Timeout of zero means no timeout. Timeout time.Duration } func init() { http.DefaultClient.Timeout = 30 * time.Second } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar // A Timeout of zero means no timeout. Timeout time.Duration } func init() { http.DefaultClient.Timeout = 30 * time.Second // Modify if needed. http.DefaultTransport = &http.Transport{ // ... } } -
net/http.Client var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
net/http.Response.Body.Close() var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
net/http.Response.Body.Close() var sleepHandler = func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Hour)
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) -
Settable net/http.Client type FooClient struct { HTTPClient *http.Client } ///
OR type FooClient struct { httpClient *http.Client } func NewFooClient(httpClient *http.Client) FooClient { return FooClient{ httpClient: httpClient, } } -
Summaries • net/http/httptest • net/http.ResponseWriter • net/http.Client • net/http.Response.Body.Close() •
net/http.Constants • Settable net/http.Client