@@ -1115,35 +1115,65 @@ func TestTransportNoHost(t *testing.T) {
1115
1115
}
1116
1116
}
1117
1117
1118
- var proxyFromEnvTests = []struct {
1118
+ type proxyFromEnvTest struct {
1119
+ req string // URL to fetch; blank means "http://example.com"
1119
1120
env string
1120
- wanturl string
1121
+ noenv string
1122
+ want string
1121
1123
wanterr error
1122
- }{
1123
- {"127.0.0.1:8080" , "http://127.0.0.1:8080" , nil },
1124
- {"cache.corp.example.com:1234" , "http://cache.corp.example.com:1234" , nil },
1125
- {"cache.corp.example.com" , "http://cache.corp.example.com" , nil },
1126
- {"https://cache.corp.example.com" , "https://cache.corp.example.com" , nil },
1127
- {"http://127.0.0.1:8080" , "http://127.0.0.1:8080" , nil },
1128
- {"https://127.0.0.1:8080" , "https://127.0.0.1:8080" , nil },
1129
- {"" , "<nil>" , nil },
1124
+ }
1125
+
1126
+ func (t proxyFromEnvTest ) String () string {
1127
+ var buf bytes.Buffer
1128
+ if t .env != "" {
1129
+ fmt .Fprintf (& buf , "http_proxy=%q" , t .env )
1130
+ }
1131
+ if t .noenv != "" {
1132
+ fmt .Fprintf (& buf , " no_proxy=%q" , t .noenv )
1133
+ }
1134
+ req := "http://example.com"
1135
+ if t .req != "" {
1136
+ req = t .req
1137
+ }
1138
+ fmt .Fprintf (& buf , " req=%q" , req )
1139
+ return strings .TrimSpace (buf .String ())
1140
+ }
1141
+
1142
+ var proxyFromEnvTests = []proxyFromEnvTest {
1143
+ {env : "127.0.0.1:8080" , want : "http://127.0.0.1:8080" },
1144
+ {env : "cache.corp.example.com:1234" , want : "http://cache.corp.example.com:1234" },
1145
+ {env : "cache.corp.example.com" , want : "http://cache.corp.example.com" },
1146
+ {env : "https://cache.corp.example.com" , want : "https://cache.corp.example.com" },
1147
+ {env : "http://127.0.0.1:8080" , want : "http://127.0.0.1:8080" },
1148
+ {env : "https://127.0.0.1:8080" , want : "https://127.0.0.1:8080" },
1149
+ {want : "<nil>" },
1150
+ {noenv : "example.com" , req : "http://example.com/" , env : "proxy" , want : "<nil>" },
1151
+ {noenv : ".example.com" , req : "http://example.com/" , env : "proxy" , want : "<nil>" },
1152
+ {noenv : "ample.com" , req : "http://example.com/" , env : "proxy" , want : "http://proxy" },
1153
+ {noenv : "example.com" , req : "http://foo.example.com/" , env : "proxy" , want : "<nil>" },
1154
+ {noenv : ".foo.com" , req : "http://example.com/" , env : "proxy" , want : "http://proxy" },
1130
1155
}
1131
1156
1132
1157
func TestProxyFromEnvironment (t * testing.T ) {
1133
1158
os .Setenv ("HTTP_PROXY" , "" )
1134
1159
os .Setenv ("http_proxy" , "" )
1135
1160
os .Setenv ("NO_PROXY" , "" )
1136
1161
os .Setenv ("no_proxy" , "" )
1137
- for i , tt := range proxyFromEnvTests {
1162
+ for _ , tt := range proxyFromEnvTests {
1138
1163
os .Setenv ("HTTP_PROXY" , tt .env )
1139
- req , _ := NewRequest ("GET" , "http://example.com" , nil )
1164
+ os .Setenv ("NO_PROXY" , tt .noenv )
1165
+ reqURL := tt .req
1166
+ if reqURL == "" {
1167
+ reqURL = "http://example.com"
1168
+ }
1169
+ req , _ := NewRequest ("GET" , reqURL , nil )
1140
1170
url , err := ProxyFromEnvironment (req )
1141
1171
if g , e := fmt .Sprintf ("%v" , err ), fmt .Sprintf ("%v" , tt .wanterr ); g != e {
1142
- t .Errorf ("%d. got error = %q, want %q" , i , g , e )
1172
+ t .Errorf ("%v: got error = %q, want %q" , tt , g , e )
1143
1173
continue
1144
1174
}
1145
- if got := fmt .Sprintf ("%s" , url ); got != tt .wanturl {
1146
- t .Errorf ("%d. got URL = %q, want %q" , i , url , tt .wanturl )
1175
+ if got := fmt .Sprintf ("%s" , url ); got != tt .want {
1176
+ t .Errorf ("%v: got URL = %q, want %q" , tt , url , tt .want )
1147
1177
}
1148
1178
}
1149
1179
}
0 commit comments