Last active
December 18, 2015 10:28
-
-
Save nak3/00c148a3aaab195f70be to your computer and use it in GitHub Desktop.
Go http GET client test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "crypto/tls" | |
| "fmt" | |
| "net/http" | |
| ) | |
| func get(url string, allowInsecure bool, proxyfromclientset bool) (*http.Response, error) { | |
| client := http.DefaultClient | |
| if allowInsecure { | |
| tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} | |
| client = &http.Client{Transport: tr} | |
| } else if proxyfromclientset { | |
| tr := &http.Transport{ | |
| TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | |
| Proxy: http.ProxyFromEnvironment, | |
| } | |
| client = &http.Client{Transport: tr} | |
| } | |
| req, err := http.NewRequest("GET", url, nil) | |
| if err != nil { | |
| return nil, fmt.Errorf("error creating request: %v", err) | |
| } | |
| req.Header.Add("X-Docker-Token", "true") | |
| resp, err := client.Do(req) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return resp, nil | |
| } | |
| func main() { | |
| url1 := fmt.Sprintf("http://registry.access.redhat.com/v1/repositories/openshift3_beta/%s/images", "mysql-55-rhel7") | |
| url2 := fmt.Sprintf("https://registry.access.redhat.com/v1/repositories/openshift3_beta/%s/images", "mysql-55-rhel7") | |
| urls := [2]string{url1, url2} | |
| for i := range urls { | |
| allowInsecure := false | |
| proxyfromclientset := false | |
| fmt.Printf("\n") | |
| fmt.Printf("url: %s, http.Client modified: %t, proxyfromclient set: %t\n", urls[i], allowInsecure, proxyfromclientset) | |
| fmt.Printf("---------\n") | |
| if resp, err := get(urls[i], allowInsecure, proxyfromclientset); err != nil { | |
| fmt.Println(err) | |
| } else { | |
| fmt.Println(resp) | |
| } | |
| allowInsecure = true | |
| fmt.Printf("\n") | |
| fmt.Printf("url: %s, http.Client modified: %t, proxyfromclient set: %t\n", urls[i], allowInsecure, proxyfromclientset) | |
| fmt.Printf("---------\n") | |
| if resp, err := get(urls[i], allowInsecure, proxyfromclientset); err != nil { | |
| fmt.Println(err) | |
| } else { | |
| fmt.Println(resp) | |
| } | |
| allowInsecure = false | |
| proxyfromclientset = true | |
| fmt.Printf("\n") | |
| fmt.Printf("url: %s, http.Client modified: %t, proxyfromclient set: %t\n", urls[i], allowInsecure, proxyfromclientset) | |
| fmt.Printf("---------\n") | |
| if resp, err := get(urls[i], allowInsecure, proxyfromclientset); err != nil { | |
| fmt.Println(err) | |
| } else { | |
| fmt.Println(resp) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
USERNAME:PASSWDand localhost:3128 withPROXYHOST:PROXYPORTOutput:
Via:[1.1 ose3-master.example.com (squid/3.3.8)]