(t *testing.T)
| 27 | ) |
| 28 | |
| 29 | func TestAuthPluginWrapTransport(t *testing.T) { |
| 30 | if err := RegisterAuthProviderPlugin("pluginA", pluginAProvider); err != nil { |
| 31 | t.Errorf("Unexpected error: failed to register pluginA: %v", err) |
| 32 | } |
| 33 | if err := RegisterAuthProviderPlugin("pluginB", pluginBProvider); err != nil { |
| 34 | t.Errorf("Unexpected error: failed to register pluginB: %v", err) |
| 35 | } |
| 36 | if err := RegisterAuthProviderPlugin("pluginFail", pluginFailProvider); err != nil { |
| 37 | t.Errorf("Unexpected error: failed to register pluginFail: %v", err) |
| 38 | } |
| 39 | testCases := []struct { |
| 40 | useWrapTransport bool |
| 41 | plugin string |
| 42 | expectErr bool |
| 43 | expectPluginA bool |
| 44 | expectPluginB bool |
| 45 | }{ |
| 46 | {false, "", false, false, false}, |
| 47 | {false, "pluginA", false, true, false}, |
| 48 | {false, "pluginB", false, false, true}, |
| 49 | {false, "pluginFail", true, false, false}, |
| 50 | {false, "pluginUnknown", true, false, false}, |
| 51 | } |
| 52 | for i, tc := range testCases { |
| 53 | c := Config{} |
| 54 | if tc.useWrapTransport { |
| 55 | // Specify an existing WrapTransport in the config to make sure that |
| 56 | // plugins play nicely. |
| 57 | c.WrapTransport = func(rt http.RoundTripper) http.RoundTripper { |
| 58 | return &wrapTransport{rt} |
| 59 | } |
| 60 | } |
| 61 | if len(tc.plugin) != 0 { |
| 62 | c.AuthProvider = &clientcmdapi.AuthProviderConfig{Name: tc.plugin} |
| 63 | } |
| 64 | tConfig, err := c.TransportConfig() |
| 65 | if err != nil { |
| 66 | // Unknown/bad plugins are expected to fail here. |
| 67 | if !tc.expectErr { |
| 68 | t.Errorf("%d. Did not expect errors loading Auth Plugin: %q. Got: %v", i, tc.plugin, err) |
| 69 | } |
| 70 | continue |
| 71 | } |
| 72 | var fullyWrappedTransport http.RoundTripper |
| 73 | fullyWrappedTransport = &emptyTransport{} |
| 74 | if tConfig.WrapTransport != nil { |
| 75 | fullyWrappedTransport = tConfig.WrapTransport(&emptyTransport{}) |
| 76 | } |
| 77 | res, err := fullyWrappedTransport.RoundTrip(&http.Request{}) |
| 78 | if err != nil { |
| 79 | t.Errorf("%d. Unexpected error in RoundTrip: %v", i, err) |
| 80 | continue |
| 81 | } |
| 82 | hasWrapTransport := res.Header.Get("wrapTransport") == "Y" |
| 83 | hasPluginA := res.Header.Get("pluginA") == "Y" |
| 84 | hasPluginB := res.Header.Get("pluginB") == "Y" |
| 85 | if hasWrapTransport != tc.useWrapTransport { |
| 86 | t.Errorf("%d. Expected Existing config.WrapTransport: %t; Got: %t", i, tc.useWrapTransport, hasWrapTransport) |
nothing calls this directly
no test coverage detected