MCPcopy Index your code
hub / github.com/syncthing/syncthing / TestRandomString

Function TestRandomString

lib/api/api_test.go:1193–1236  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1191}
1192
1193func TestRandomString(t *testing.T) {
1194 t.Parallel()
1195
1196 baseURL := startHTTP(t, apiCfg)
1197 cli := &http.Client{
1198 Timeout: time.Second,
1199 }
1200
1201 // The default should be to return a 32 character random string
1202
1203 for _, url := range []string{"/rest/svc/random/string", "/rest/svc/random/string?length=-1", "/rest/svc/random/string?length=yo"} {
1204 req, _ := http.NewRequest("GET", baseURL+url, nil)
1205 req.Header.Set("X-API-Key", testAPIKey)
1206 resp, err := cli.Do(req)
1207 if err != nil {
1208 t.Fatal(err)
1209 }
1210
1211 var res map[string]string
1212 if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
1213 t.Fatal(err)
1214 }
1215 if len(res["random"]) != 32 {
1216 t.Errorf("Expected 32 random characters, got %q of length %d", res["random"], len(res["random"]))
1217 }
1218 }
1219
1220 // We can ask for a different length if we like
1221
1222 req, _ := http.NewRequest("GET", baseURL+"/rest/svc/random/string?length=27", nil)
1223 req.Header.Set("X-API-Key", testAPIKey)
1224 resp, err := cli.Do(req)
1225 if err != nil {
1226 t.Fatal(err)
1227 }
1228
1229 var res map[string]string
1230 if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
1231 t.Fatal(err)
1232 }
1233 if len(res["random"]) != 27 {
1234 t.Errorf("Expected 27 random characters, got %q of length %d", res["random"], len(res["random"]))
1235 }
1236}
1237
1238func TestConfigPostOK(t *testing.T) {
1239 t.Parallel()

Callers

nothing calls this directly

Calls 4

startHTTPFunction · 0.85
DoMethod · 0.80
FatalMethod · 0.80
SetMethod · 0.45

Tested by

no test coverage detected