|
4 | 4 | "io" |
5 | 5 | "net/http" |
6 | 6 | "net/http/httptest" |
| 7 | + "os" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/github/gh-aw-mcpg/internal/difc" |
@@ -844,3 +845,122 @@ func TestUnwrapSingleObject(t *testing.T) { |
844 | 845 | }) |
845 | 846 | } |
846 | 847 | } |
| 848 | + |
| 849 | +func TestDeriveAPIFromServerURL(t *testing.T) { |
| 850 | + tests := []struct { |
| 851 | + name string |
| 852 | + serverURL string |
| 853 | + expected string |
| 854 | + }{ |
| 855 | + { |
| 856 | + name: "github.com returns default", |
| 857 | + serverURL: "https://github.com", |
| 858 | + expected: DefaultGitHubAPIBase, |
| 859 | + }, |
| 860 | + { |
| 861 | + name: "github.com with trailing slash", |
| 862 | + serverURL: "https://github.com/", |
| 863 | + expected: DefaultGitHubAPIBase, |
| 864 | + }, |
| 865 | + { |
| 866 | + name: "GHEC tenant derives copilot-api subdomain", |
| 867 | + serverURL: "https://mycompany.ghe.com", |
| 868 | + expected: "https://copilot-api.mycompany.ghe.com", |
| 869 | + }, |
| 870 | + { |
| 871 | + name: "GHEC tenant with trailing slash", |
| 872 | + serverURL: "https://mycompany.ghe.com/", |
| 873 | + expected: "https://copilot-api.mycompany.ghe.com", |
| 874 | + }, |
| 875 | + { |
| 876 | + name: "GHES uses /api/v3 path", |
| 877 | + serverURL: "https://github.mycompany.com", |
| 878 | + expected: "https://github.mycompany.com/api/v3", |
| 879 | + }, |
| 880 | + { |
| 881 | + name: "GHEC tenant with port", |
| 882 | + serverURL: "https://mycompany.ghe.com:8443", |
| 883 | + expected: "https://copilot-api.mycompany.ghe.com:8443", |
| 884 | + }, |
| 885 | + { |
| 886 | + name: "GHES with port", |
| 887 | + serverURL: "https://github.example.com:8443", |
| 888 | + expected: "https://github.example.com:8443/api/v3", |
| 889 | + }, |
| 890 | + { |
| 891 | + name: "empty string", |
| 892 | + serverURL: "", |
| 893 | + expected: "", |
| 894 | + }, |
| 895 | + { |
| 896 | + name: "invalid URL", |
| 897 | + serverURL: "not-a-url", |
| 898 | + expected: "", |
| 899 | + }, |
| 900 | + } |
| 901 | + |
| 902 | + for _, tt := range tests { |
| 903 | + t.Run(tt.name, func(t *testing.T) { |
| 904 | + result := deriveAPIFromServerURL(tt.serverURL) |
| 905 | + assert.Equal(t, tt.expected, result) |
| 906 | + }) |
| 907 | + } |
| 908 | +} |
| 909 | + |
| 910 | +func TestDeriveGitHubAPIURL(t *testing.T) { |
| 911 | + tests := []struct { |
| 912 | + name string |
| 913 | + envVars map[string]string |
| 914 | + expected string |
| 915 | + }{ |
| 916 | + { |
| 917 | + name: "no env vars returns empty", |
| 918 | + envVars: map[string]string{}, |
| 919 | + expected: "", |
| 920 | + }, |
| 921 | + { |
| 922 | + name: "GITHUB_API_URL takes priority", |
| 923 | + envVars: map[string]string{"GITHUB_API_URL": "https://api.custom.ghe.com", "GITHUB_SERVER_URL": "https://other.ghe.com"}, |
| 924 | + expected: "https://api.custom.ghe.com", |
| 925 | + }, |
| 926 | + { |
| 927 | + name: "GITHUB_SERVER_URL auto-derives GHEC", |
| 928 | + envVars: map[string]string{"GITHUB_SERVER_URL": "https://mycompany.ghe.com"}, |
| 929 | + expected: "https://copilot-api.mycompany.ghe.com", |
| 930 | + }, |
| 931 | + { |
| 932 | + name: "GITHUB_SERVER_URL auto-derives GHES", |
| 933 | + envVars: map[string]string{"GITHUB_SERVER_URL": "https://github.example.com"}, |
| 934 | + expected: "https://github.example.com/api/v3", |
| 935 | + }, |
| 936 | + } |
| 937 | + |
| 938 | + for _, tt := range tests { |
| 939 | + t.Run(tt.name, func(t *testing.T) { |
| 940 | + // Save and clear relevant env vars |
| 941 | + savedAPI, hadAPI := os.LookupEnv("GITHUB_API_URL") |
| 942 | + savedServer, hadServer := os.LookupEnv("GITHUB_SERVER_URL") |
| 943 | + os.Unsetenv("GITHUB_API_URL") |
| 944 | + os.Unsetenv("GITHUB_SERVER_URL") |
| 945 | + defer func() { |
| 946 | + if hadAPI { |
| 947 | + _ = os.Setenv("GITHUB_API_URL", savedAPI) |
| 948 | + } else { |
| 949 | + _ = os.Unsetenv("GITHUB_API_URL") |
| 950 | + } |
| 951 | + if hadServer { |
| 952 | + _ = os.Setenv("GITHUB_SERVER_URL", savedServer) |
| 953 | + } else { |
| 954 | + _ = os.Unsetenv("GITHUB_SERVER_URL") |
| 955 | + } |
| 956 | + }() |
| 957 | + |
| 958 | + for k, v := range tt.envVars { |
| 959 | + os.Setenv(k, v) |
| 960 | + } |
| 961 | + |
| 962 | + result := DeriveGitHubAPIURL() |
| 963 | + assert.Equal(t, tt.expected, result) |
| 964 | + }) |
| 965 | + } |
| 966 | +} |
0 commit comments