github.com/go-chef/chef@v0.30.1/testapi/http.go (about) 1 // 2 // Test the go-chef/chef chef server api http code 3 // 4 5 package testapi 6 7 import ( 8 "fmt" 9 "github.com/go-chef/chef" 10 "log" 11 "net/http" 12 "os" 13 ) 14 15 // http exercise the chef server api and config settings 16 func Http() { 17 log.SetFlags(log.LstdFlags | log.Lshortfile) 18 // Use config to set a roundtripper function 19 cfg := &chef.Config{Timeout: 1, RoundTripper: newTestRt} 20 client := Client(cfg) 21 22 // List the current groups 23 groupList, err := client.Groups.List() 24 if err != nil { 25 log.Println("FAILURE listing the existing groups:", err) 26 } 27 fmt.Fprintln(os.Stdout, "SUCCESS listing the existing groups:", groupList) 28 29 // print roundtripper information 30 rt, _ := client.Client.Transport.(*testRt) 31 groupList, err = client.Groups.List() 32 errcnt := 0 33 if err != nil { 34 errcnt++ 35 } 36 groupList, err = client.Groups.List() 37 if err != nil { 38 errcnt++ 39 } 40 if errcnt != rt.err_count { 41 log.Printf("FAILURE roundtrip err count is: %+v should be: %+v\n", errcnt, rt.err_count) 42 } 43 if rt.req_count != 3 { 44 log.Printf("FAILURE roundtrip call count is: %+v should be 3\n", rt.req_count) 45 } 46 } 47 48 type testRt struct { 49 req_count int 50 err_count int 51 next http.RoundTripper 52 } 53 54 func newTestRt(next http.RoundTripper) http.RoundTripper { return &testRt{next: next} } 55 56 func (this *testRt) RoundTrip(req *http.Request) (*http.Response, error) { 57 this.req_count++ 58 res, err := this.next.RoundTrip(req) 59 if err != nil { 60 this.err_count++ 61 } 62 return res, err 63 }