|
| 1 | +package utils //nolint:revive //TODO: figure out a better name for this package |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestOAuthURL(t *testing.T) { |
| 12 | + ctx := context.Background() |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + host string |
| 17 | + expectedOAuth string |
| 18 | + expectError bool |
| 19 | + errorSubstring string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "dotcom (empty host)", |
| 23 | + host: "", |
| 24 | + expectedOAuth: "https://github.com/login/oauth", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "dotcom (explicit github.com)", |
| 28 | + host: "https://github.com", |
| 29 | + expectedOAuth: "https://github.com/login/oauth", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "GHEC with HTTPS", |
| 33 | + host: "https://acme.ghe.com", |
| 34 | + expectedOAuth: "https://acme.ghe.com/login/oauth", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "GHEC with HTTP (should error)", |
| 38 | + host: "http://acme.ghe.com", |
| 39 | + expectError: true, |
| 40 | + errorSubstring: "GHEC URL must be HTTPS", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "GHES with HTTPS", |
| 44 | + host: "https://ghes.example.com", |
| 45 | + expectedOAuth: "https://ghes.example.com/login/oauth", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "GHES with HTTP", |
| 49 | + host: "http://ghes.example.com", |
| 50 | + expectedOAuth: "http://ghes.example.com/login/oauth", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "GHES with HTTP and custom port (port stripped - not supported yet)", |
| 54 | + host: "http://ghes.local:8080", |
| 55 | + expectedOAuth: "http://ghes.local/login/oauth", // Port is stripped ref: ln222 api.go comment |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "GHES with HTTPS and custom port (port stripped - not supported yet)", |
| 59 | + host: "https://ghes.local:8443", |
| 60 | + expectedOAuth: "https://ghes.local/login/oauth", // Port is stripped ref: ln222 api.go comment |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "host without scheme (should error)", |
| 64 | + host: "ghes.example.com", |
| 65 | + expectError: true, |
| 66 | + errorSubstring: "host must have a scheme", |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + apiHost, err := NewAPIHost(tt.host) |
| 73 | + |
| 74 | + if tt.expectError { |
| 75 | + require.Error(t, err) |
| 76 | + if tt.errorSubstring != "" { |
| 77 | + assert.Contains(t, err.Error(), tt.errorSubstring) |
| 78 | + } |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + require.NoError(t, err) |
| 83 | + require.NotNil(t, apiHost) |
| 84 | + |
| 85 | + oauthURL, err := apiHost.OAuthURL(ctx) |
| 86 | + require.NoError(t, err) |
| 87 | + require.NotNil(t, oauthURL) |
| 88 | + |
| 89 | + assert.Equal(t, tt.expectedOAuth, oauthURL.String()) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestAPIHost_AllURLsHaveConsistentScheme(t *testing.T) { |
| 95 | + ctx := context.Background() |
| 96 | + |
| 97 | + tests := []struct { |
| 98 | + name string |
| 99 | + host string |
| 100 | + expectedScheme string |
| 101 | + }{ |
| 102 | + { |
| 103 | + name: "GHES with HTTPS", |
| 104 | + host: "https://ghes.example.com", |
| 105 | + expectedScheme: "https", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "GHES with HTTP", |
| 109 | + host: "http://ghes.example.com", |
| 110 | + expectedScheme: "http", |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tt := range tests { |
| 115 | + t.Run(tt.name, func(t *testing.T) { |
| 116 | + apiHost, err := NewAPIHost(tt.host) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + restURL, err := apiHost.BaseRESTURL(ctx) |
| 120 | + require.NoError(t, err) |
| 121 | + assert.Equal(t, tt.expectedScheme, restURL.Scheme, "REST URL scheme should match") |
| 122 | + |
| 123 | + gqlURL, err := apiHost.GraphqlURL(ctx) |
| 124 | + require.NoError(t, err) |
| 125 | + assert.Equal(t, tt.expectedScheme, gqlURL.Scheme, "GraphQL URL scheme should match") |
| 126 | + |
| 127 | + uploadURL, err := apiHost.UploadURL(ctx) |
| 128 | + require.NoError(t, err) |
| 129 | + assert.Equal(t, tt.expectedScheme, uploadURL.Scheme, "Upload URL scheme should match") |
| 130 | + |
| 131 | + rawURL, err := apiHost.RawURL(ctx) |
| 132 | + require.NoError(t, err) |
| 133 | + assert.Equal(t, tt.expectedScheme, rawURL.Scheme, "Raw URL scheme should match") |
| 134 | + |
| 135 | + oauthURL, err := apiHost.OAuthURL(ctx) |
| 136 | + require.NoError(t, err) |
| 137 | + assert.Equal(t, tt.expectedScheme, oauthURL.Scheme, "OAuth URL scheme should match") |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
0 commit comments