StartServerContext starts a test server with a given root context and returns APIServerInfo.
(ctx context.Context, t *testing.T, env *repotesting.Environment, tls bool)
| 41 | |
| 42 | // StartServerContext starts a test server with a given root context and returns APIServerInfo. |
| 43 | func StartServerContext(ctx context.Context, t *testing.T, env *repotesting.Environment, tls bool) *repo.APIServerInfo { |
| 44 | t.Helper() |
| 45 | |
| 46 | s, err := server.New(ctx, &server.Options{ |
| 47 | ConfigFile: env.ConfigFile(), |
| 48 | PasswordPersist: passwordpersist.File(), |
| 49 | Authorizer: auth.LegacyAuthorizer(), |
| 50 | Authenticator: auth.CombineAuthenticators( |
| 51 | auth.AuthenticateSingleUser(TestUsername+"@"+TestHostname, TestPassword), |
| 52 | auth.AuthenticateSingleUser(TestUIUsername, TestUIPassword), |
| 53 | ), |
| 54 | RefreshInterval: 1 * time.Minute, |
| 55 | UIUser: TestUIUsername, |
| 56 | UIPreferencesFile: filepath.Join(testutil.TempDirectory(t), "ui-pref.json"), |
| 57 | }) |
| 58 | |
| 59 | require.NoError(t, err) |
| 60 | |
| 61 | s.SetRepository(ctx, env.Repository) |
| 62 | |
| 63 | // ensure we disconnect the repository before shutting down the server. |
| 64 | t.Cleanup(func() { s.SetRepository(ctx, nil) }) |
| 65 | |
| 66 | require.NoError(t, err) |
| 67 | |
| 68 | asi := &repo.APIServerInfo{} |
| 69 | |
| 70 | m := mux.NewRouter() |
| 71 | s.SetupHTMLUIAPIHandlers(m) |
| 72 | s.SetupControlAPIHandlers(m) |
| 73 | s.ServeStaticFiles(m, server.AssetFile()) |
| 74 | |
| 75 | hs := httptest.NewUnstartedServer(s.GRPCRouterHandler(m)) |
| 76 | if tls { |
| 77 | hs.EnableHTTP2 = true |
| 78 | hs.StartTLS() |
| 79 | serverHash := sha256.Sum256(hs.Certificate().Raw) |
| 80 | asi.BaseURL = hs.URL |
| 81 | asi.TrustedServerCertificateFingerprint = hex.EncodeToString(serverHash[:]) |
| 82 | } else { |
| 83 | hs.Start() |
| 84 | asi.BaseURL = hs.URL |
| 85 | } |
| 86 | |
| 87 | t.Cleanup(hs.Close) |
| 88 | |
| 89 | return asi |
| 90 | } |
| 91 | |
| 92 | // ConnectAndOpenAPIServer creates temporary config file and to and opens API server for testing. |
| 93 | func ConnectAndOpenAPIServer(t *testing.T, ctx context.Context, asi *repo.APIServerInfo, rco repo.ClientOptions, caching content.CachingOptions, password string, opt *repo.Options) (repo.Repository, error) { |