ducky/devices

Paddy 2015-11-12 Parent:b6494e1a499e

3:8d40c0e5f4d9 Go to Latest

ducky/devices/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go

Fix happy path test for getting devices. We forgot to create the Devices in the Storer tests, so our GetDevices happy path test was invalid. Add the call, and we're ok.

History
paddy@0 1 // Copyright 2015 The Go Authors. All rights reserved.
paddy@0 2 // Use of this source code is governed by a BSD-style
paddy@0 3 // license that can be found in the LICENSE file.
paddy@0 4
paddy@0 5 // Package ctxhttp provides helper functions for performing context-aware HTTP requests.
paddy@0 6 package ctxhttp
paddy@0 7
paddy@0 8 import (
paddy@0 9 "io"
paddy@0 10 "net/http"
paddy@0 11 "net/url"
paddy@0 12 "strings"
paddy@0 13
paddy@0 14 "golang.org/x/net/context"
paddy@0 15 )
paddy@0 16
paddy@0 17 // Do sends an HTTP request with the provided http.Client and returns an HTTP response.
paddy@0 18 // If the client is nil, http.DefaultClient is used.
paddy@0 19 // If the context is canceled or times out, ctx.Err() will be returned.
paddy@0 20 func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
paddy@0 21 if client == nil {
paddy@0 22 client = http.DefaultClient
paddy@0 23 }
paddy@0 24
paddy@0 25 // Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go.
paddy@0 26 cancel := canceler(client, req)
paddy@0 27
paddy@0 28 type responseAndError struct {
paddy@0 29 resp *http.Response
paddy@0 30 err error
paddy@0 31 }
paddy@0 32 result := make(chan responseAndError, 1)
paddy@0 33
paddy@0 34 go func() {
paddy@0 35 resp, err := client.Do(req)
paddy@0 36 result <- responseAndError{resp, err}
paddy@0 37 }()
paddy@0 38
paddy@0 39 select {
paddy@0 40 case <-ctx.Done():
paddy@0 41 cancel()
paddy@0 42 return nil, ctx.Err()
paddy@0 43 case r := <-result:
paddy@0 44 return r.resp, r.err
paddy@0 45 }
paddy@0 46 }
paddy@0 47
paddy@0 48 // Get issues a GET request via the Do function.
paddy@0 49 func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
paddy@0 50 req, err := http.NewRequest("GET", url, nil)
paddy@0 51 if err != nil {
paddy@0 52 return nil, err
paddy@0 53 }
paddy@0 54 return Do(ctx, client, req)
paddy@0 55 }
paddy@0 56
paddy@0 57 // Head issues a HEAD request via the Do function.
paddy@0 58 func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
paddy@0 59 req, err := http.NewRequest("HEAD", url, nil)
paddy@0 60 if err != nil {
paddy@0 61 return nil, err
paddy@0 62 }
paddy@0 63 return Do(ctx, client, req)
paddy@0 64 }
paddy@0 65
paddy@0 66 // Post issues a POST request via the Do function.
paddy@0 67 func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) {
paddy@0 68 req, err := http.NewRequest("POST", url, body)
paddy@0 69 if err != nil {
paddy@0 70 return nil, err
paddy@0 71 }
paddy@0 72 req.Header.Set("Content-Type", bodyType)
paddy@0 73 return Do(ctx, client, req)
paddy@0 74 }
paddy@0 75
paddy@0 76 // PostForm issues a POST request via the Do function.
paddy@0 77 func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) {
paddy@0 78 return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
paddy@0 79 }