|
| 1 | +package update |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestManifestURLsDefaultOrder(t *testing.T) { |
| 11 | + t.Setenv("MSCODE_MANIFEST_URL", "") |
| 12 | + |
| 13 | + got := ManifestURLs() |
| 14 | + if len(got) != 2 { |
| 15 | + t.Fatalf("ManifestURLs() len = %d, want 2", len(got)) |
| 16 | + } |
| 17 | + if got[0] != defaultMirrorManifestURL { |
| 18 | + t.Fatalf("ManifestURLs()[0] = %q, want %q", got[0], defaultMirrorManifestURL) |
| 19 | + } |
| 20 | + if got[1] != defaultGitHubManifestURL { |
| 21 | + t.Fatalf("ManifestURLs()[1] = %q, want %q", got[1], defaultGitHubManifestURL) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestManifestURLsEnvOverride(t *testing.T) { |
| 26 | + t.Setenv("MSCODE_MANIFEST_URL", "http://example.test/manifest.json") |
| 27 | + |
| 28 | + got := ManifestURLs() |
| 29 | + if len(got) != 1 { |
| 30 | + t.Fatalf("ManifestURLs() len = %d, want 1", len(got)) |
| 31 | + } |
| 32 | + if got[0] != "http://example.test/manifest.json" { |
| 33 | + t.Fatalf("ManifestURLs()[0] = %q, want env override", got[0]) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestFetchManifestFallsBackToNextURL(t *testing.T) { |
| 38 | + success := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 39 | + w.Header().Set("Content-Type", "application/json") |
| 40 | + _, _ = w.Write([]byte(`{"latest":"0.5.0-beta.2","min_allowed":"","download_base":"http://mirror.example/mscode/releases"}`)) |
| 41 | + })) |
| 42 | + defer success.Close() |
| 43 | + |
| 44 | + got, err := fetchManifestFromURLs(context.Background(), []string{ |
| 45 | + "http://127.0.0.1:1/latest/manifest.json", |
| 46 | + success.URL, |
| 47 | + }) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("fetchManifestFromURLs() error = %v", err) |
| 50 | + } |
| 51 | + if got.Latest != "0.5.0-beta.2" { |
| 52 | + t.Fatalf("manifest latest = %q, want %q", got.Latest, "0.5.0-beta.2") |
| 53 | + } |
| 54 | + if got.DownloadBase != "http://mirror.example/mscode/releases" { |
| 55 | + t.Fatalf("manifest download_base = %q", got.DownloadBase) |
| 56 | + } |
| 57 | +} |
0 commit comments