github.com/koron/hk@v0.0.0-20150303213137-b8aeaa3ab34c/main_test.go (about) 1 package main 2 3 import ( 4 "net/http" 5 "os" 6 "testing" 7 8 "github.com/heroku/hk/postgresql" 9 ) 10 11 func TestSSLEnabled(t *testing.T) { 12 initClients() 13 14 if client.HTTP == nil { 15 // No http.Client means the client defaults to SSL enabled 16 return 17 } 18 if client.HTTP.Transport == nil { 19 // No transport means the client defaults to SSL enabled 20 return 21 } 22 conf := client.HTTP.Transport.(*http.Transport).TLSClientConfig 23 if conf == nil { 24 // No TLSClientConfig means the client defaults to SSL enabled 25 return 26 } 27 if conf.InsecureSkipVerify { 28 t.Errorf("expected InsecureSkipVerify == false") 29 } 30 31 if pgclient.HTTP == nil { 32 // No http.Client means the pgclient defaults to SSL enabled 33 return 34 } 35 if pgclient.HTTP.Transport == nil { 36 // No transport means the pgclient defaults to SSL enabled 37 return 38 } 39 conf = pgclient.HTTP.Transport.(*http.Transport).TLSClientConfig 40 if conf == nil { 41 // No TLSClientConfig means the pgclient defaults to SSL enabled 42 return 43 } 44 if conf.InsecureSkipVerify { 45 t.Errorf("expected InsecureSkipVerify == false") 46 } 47 48 client = nil 49 pgclient = nil 50 } 51 52 func TestSSLDisable(t *testing.T) { 53 os.Setenv("HEROKU_SSL_VERIFY", "disable") 54 initClients() 55 56 if client.HTTP == nil { 57 t.Fatalf("client.HTTP not set, expected http.Client") 58 } 59 if client.HTTP.Transport == nil { 60 t.Fatalf("client.HTTP.Transport not set") 61 } 62 conf := client.HTTP.Transport.(*http.Transport).TLSClientConfig 63 if conf == nil { 64 t.Fatalf("client.HTTP.Transport's TLSClientConfig is nil") 65 } 66 if !conf.InsecureSkipVerify { 67 t.Errorf("expected InsecureSkipVerify == true") 68 } 69 70 if pgclient.HTTP == nil { 71 t.Fatalf("pgclient.HTTP not set, expected http.Client") 72 } 73 if pgclient.HTTP.Transport == nil { 74 t.Fatalf("pgclient.HTTP.Transport not set") 75 } 76 conf = pgclient.HTTP.Transport.(*http.Transport).TLSClientConfig 77 if conf == nil { 78 t.Fatalf("pgclient.HTTP.Transport's TLSClientConfig is nil") 79 } 80 if !conf.InsecureSkipVerify { 81 t.Errorf("expected InsecureSkipVerify == true") 82 } 83 84 os.Setenv("HEROKU_SSL_VERIFY", "") 85 client = nil 86 pgclient = nil 87 } 88 89 func TestHerokuAPIURL(t *testing.T) { 90 newURL := "https://api.otherheroku.com" 91 os.Setenv("HEROKU_API_URL", newURL) 92 initClients() 93 94 if client.URL != newURL { 95 t.Errorf("expected client.URL to be %q, got %q", newURL, client.URL) 96 } 97 98 if apiURL != newURL { 99 t.Errorf("expected apiURL to be %q, got %q", newURL, apiURL) 100 } 101 102 // cleanup 103 os.Setenv("HEROKU_API_URL", "") 104 } 105 106 func TestHerokuPostgresqlHost(t *testing.T) { 107 newHost := "maciek" 108 newURL := "https://" + newHost + ".herokuapp.com" + postgresql.DefaultAPIPath 109 os.Setenv("HEROKU_POSTGRESQL_HOST", newHost) 110 initClients() 111 112 if pgclient.URL != newURL { 113 t.Errorf("expected client.URL to be %q, got %q", newURL, pgclient.URL) 114 } 115 if pgclient.StarterURL != newURL { 116 t.Errorf("expected client.StarterURL to be %q, got %q", newURL, pgclient.StarterURL) 117 } 118 119 // cleanup 120 os.Setenv("HEROKU_POSTGRESQL_HOST", "") 121 } 122 123 func TestShogun(t *testing.T) { 124 newShogun := "will" 125 newURL := "https://shogun-" + newShogun + ".herokuapp.com" + postgresql.DefaultAPIPath 126 os.Setenv("SHOGUN", newShogun) 127 initClients() 128 129 if pgclient.URL != newURL { 130 t.Errorf("expected client.URL to be %q, got %q", newURL, pgclient.URL) 131 } 132 // starter URL should be unchanged 133 if pgclient.StarterURL != "" { 134 t.Errorf("expected client.StarterURL to be empty, got %q", pgclient.StarterURL) 135 } 136 137 // cleanup 138 os.Setenv("SHOGUN", "") 139 } 140 141 func TestShogunAndHerokuPostgresqlHost(t *testing.T) { 142 newShogun := "fdr" 143 newShogunURL := "https://shogun-" + newShogun + ".herokuapp.com" + postgresql.DefaultAPIPath 144 newHost := "maciek" 145 newHostURL := "https://" + newHost + ".herokuapp.com" + postgresql.DefaultAPIPath 146 os.Setenv("HEROKU_POSTGRESQL_HOST", newHost) 147 os.Setenv("SHOGUN", newShogun) 148 initClients() 149 150 if pgclient.URL != newShogunURL { 151 t.Errorf("expected client.URL to be %q, got %q", newShogunURL, pgclient.URL) 152 } 153 // starter URL should be unchanged 154 if pgclient.StarterURL != newHostURL { 155 t.Errorf("expected client.StarterURL to be %q, got %q", newHostURL, pgclient.StarterURL) 156 } 157 158 // TODO 159 // cleanup 160 os.Setenv("HEROKU_POSTGRESQL_HOST", "") 161 os.Setenv("SHOGUN", "") 162 }