Skip to content

Instantly share code, notes, and snippets.

@nak3
Last active December 18, 2015 10:28
Show Gist options
  • Save nak3/00c148a3aaab195f70be to your computer and use it in GitHub Desktop.
Save nak3/00c148a3aaab195f70be to your computer and use it in GitHub Desktop.
Go http GET client test
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)
}
}
}
@nak3
Copy link
Author

nak3 commented Jun 16, 2015

Usage:

  • Replace knakayam:redhat with USERNAME:PASSWD and localhost:3128 with PROXYHOST:PROXYPORT
# HTTP_PROXY=http://knakayam:redhat@localhost:3128 HTTPS_PROXY=https://knakayam:redhat@localhost:3128 ./httpclient

Output:

  • Check if it passed proxy or not with Via:[1.1 ose3-master.example.com (squid/3.3.8)]
url: http://registry.access.redhat.com/v1/repositories/openshift3_beta/mysql-55-rhel7/images, http.Client modified: false, proxyfromclient set: false
---------
&{200 OK 200 HTTP/1.1 1 1 map[Server:[Apache] Content-Type:[application/json] Via:[1.1 ose3-master.example.com (squid/3.3.8)] Connection:[keep-alive] X-Cache:[MISS from ose3-master.example.com] X-Cache-Lookup:[MISS from ose3-master.example.com:3128] Date:[Tue, 16 Jun 2015 09:22:50 GMT] Content-Length:[1976] X-Docker-Endpoints:[registry.access.redhat.com] X-Docker-Registry-Version:[0.6.6] X-Docker-Registry-Config:[common]] 0xc208040180 1976 [] false map[] 0xc208029380 <nil>}

url: http://registry.access.redhat.com/v1/repositories/openshift3_beta/mysql-55-rhel7/images, http.Client modified: true, proxyfromclient set: false
---------
&{200 OK 200 HTTP/1.1 1 1 map[Content-Length:[1976] X-Docker-Endpoints:[registry.access.redhat.com] X-Docker-Registry-Version:[0.6.6] X-Docker-Registry-Config:[common] Content-Type:[application/json] Date:[Tue, 16 Jun 2015 09:22:51 GMT] Server:[Apache]] 0xc208040340 1976 [] true map[] 0xc208028270 <nil>}

url: http://registry.access.redhat.com/v1/repositories/openshift3_beta/mysql-55-rhel7/images, http.Client modified: false, proxyfromclient set: true
---------
&{200 OK 200 HTTP/1.1 1 1 map[Server:[Apache] Content-Length:[1976] X-Docker-Endpoints:[registry.access.redhat.com] Content-Type:[application/json] Via:[1.1 ose3-master.example.com (squid/3.3.8)] Connection:[keep-alive] Date:[Tue, 16 Jun 2015 09:22:51 GMT] X-Docker-Registry-Version:[0.6.6] X-Docker-Registry-Config:[common] X-Cache:[MISS from ose3-master.example.com] X-Cache-Lookup:[MISS from ose3-master.example.com:3128]] 0xc208040440 1976 [] false map[] 0xc208028340 <nil>}

url: https://registry.access.redhat.com/v1/repositories/openshift3_beta/mysql-55-rhel7/images, http.Client modified: false, proxyfromclient set: false
---------
&{200 OK 200 HTTP/1.1 1 1 map[Server:[Apache] Content-Length:[1976] X-Docker-Endpoints:[registry.access.redhat.com] X-Docker-Registry-Version:[0.6.6] X-Docker-Registry-Config:[common] Content-Type:[application/json] Date:[Tue, 16 Jun 2015 09:22:52 GMT]] 0xc20821fd40 1976 [] true map[] 0xc2080284e0 0xc2081a3080}

url: https://registry.access.redhat.com/v1/repositories/openshift3_beta/mysql-55-rhel7/images, http.Client modified: true, proxyfromclient set: false
---------
&{200 OK 200 HTTP/1.1 1 1 map[Content-Type:[application/json] Date:[Tue, 16 Jun 2015 09:22:52 GMT] Server:[Apache] Content-Length:[1976] X-Docker-Endpoints:[registry.access.redhat.com] X-Docker-Registry-Version:[0.6.6] X-Docker-Registry-Config:[common]] 0xc20819a540 1976 [] true map[] 0xc208029450 0xc2081a3ec0}

url: https://registry.access.redhat.com/v1/repositories/openshift3_beta/mysql-55-rhel7/images, http.Client modified: false, proxyfromclient set: true
---------
&{200 OK 200 HTTP/1.1 1 1 map[X-Docker-Registry-Config:[common] Content-Type:[application/json] Date:[Tue, 16 Jun 2015 09:22:53 GMT] Server:[Apache] Content-Length:[1976] X-Docker-Endpoints:[registry.access.redhat.com] X-Docker-Registry-Version:[0.6.6]] 0xc20819ad40 1976 [] true map[] 0xc208029a00 0xc208005e60}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment