(t *testing.T, cfg config.Wrapper, shutdownTimeout time.Duration)
| 1018 | } |
| 1019 | |
| 1020 | func startHTTPWithShutdownTimeout(t *testing.T, cfg config.Wrapper, shutdownTimeout time.Duration) string { |
| 1021 | m := new(modelmocks.Model) |
| 1022 | assetDir := "../../gui" |
| 1023 | eventSub := new(eventmocks.BufferedSubscription) |
| 1024 | diskEventSub := new(eventmocks.BufferedSubscription) |
| 1025 | discoverer := new(discovermocks.Manager) |
| 1026 | connections := new(connmocks.Service) |
| 1027 | errorLog := slogutil.NewRecorder(0) |
| 1028 | systemLog := slogutil.NewRecorder(0) |
| 1029 | addrChan := make(chan string) |
| 1030 | mockedSummary := &modelmocks.FolderSummaryService{} |
| 1031 | mockedSummary.SummaryReturns(new(model.FolderSummary), nil) |
| 1032 | |
| 1033 | // Instantiate the API service |
| 1034 | urService := ur.New(cfg, m, connections, false) |
| 1035 | mdb, err := sqlite.Open(t.TempDir()) |
| 1036 | if err != nil { |
| 1037 | t.Fatal(err) |
| 1038 | } |
| 1039 | t.Cleanup(func() { |
| 1040 | mdb.Close() |
| 1041 | }) |
| 1042 | kdb := db.NewMiscDB(mdb) |
| 1043 | svc := New(protocol.LocalDeviceID, cfg, assetDir, "syncthing", m, eventSub, diskEventSub, events.NoopLogger, discoverer, connections, urService, mockedSummary, errorLog, systemLog, false, kdb).(*service) |
| 1044 | svc.started = addrChan |
| 1045 | |
| 1046 | if shutdownTimeout > 0 { |
| 1047 | svc.shutdownTimeout = shutdownTimeout |
| 1048 | } |
| 1049 | |
| 1050 | // Actually start the API service |
| 1051 | supervisor := suture.New("API test", suture.Spec{ |
| 1052 | PassThroughPanics: true, |
| 1053 | }) |
| 1054 | supervisor.Add(svc) |
| 1055 | ctx, cancel := context.WithCancel(context.Background()) |
| 1056 | t.Cleanup(cancel) |
| 1057 | supervisor.ServeBackground(ctx) |
| 1058 | |
| 1059 | // Make sure the API service is listening, and get the URL to use. |
| 1060 | addr := <-addrChan |
| 1061 | tcpAddr, err := net.ResolveTCPAddr("tcp", addr) |
| 1062 | if err != nil { |
| 1063 | t.Fatal(fmt.Errorf("weird address from API service: %w", err)) |
| 1064 | } |
| 1065 | |
| 1066 | host, _, _ := net.SplitHostPort(cfg.GUI().RawAddress) |
| 1067 | if host == "" || host == "0.0.0.0" { |
| 1068 | host = "127.0.0.1" |
| 1069 | } |
| 1070 | baseURL := fmt.Sprintf("http://%s", net.JoinHostPort(host, strconv.Itoa(tcpAddr.Port))) |
| 1071 | |
| 1072 | return baseURL |
| 1073 | } |
| 1074 | |
| 1075 | func TestCSRFRequired(t *testing.T) { |
| 1076 | t.Parallel() |
no test coverage detected