MCPcopy
hub / github.com/syncthing/syncthing / httpRequest

Function httpRequest

lib/api/api_test.go:517–561  ·  view source on GitHub ↗
(method string, url string, body any, basicAuthUsername, basicAuthPassword, xapikeyHeader, authorizationBearer, csrfTokenName, csrfTokenValue string, cookies []*http.Cookie, t *testing.T)

Source from the content-addressed store, hash-verified

515}
516
517func httpRequest(method string, url string, body any, basicAuthUsername, basicAuthPassword, xapikeyHeader, authorizationBearer, csrfTokenName, csrfTokenValue string, cookies []*http.Cookie, t *testing.T) *http.Response {
518 t.Helper()
519
520 var bodyReader io.Reader = nil
521 if body != nil {
522 bodyBytes, err := json.Marshal(body)
523 if err != nil {
524 t.Fatal(err)
525 }
526 bodyReader = bytes.NewReader(bodyBytes)
527 }
528
529 req, err := http.NewRequest(method, url, bodyReader)
530 if err != nil {
531 t.Fatal(err)
532 }
533
534 if basicAuthUsername != "" || basicAuthPassword != "" {
535 req.SetBasicAuth(basicAuthUsername, basicAuthPassword)
536 }
537
538 if xapikeyHeader != "" {
539 req.Header.Set("X-API-Key", xapikeyHeader)
540 }
541
542 if authorizationBearer != "" {
543 req.Header.Set("Authorization", "Bearer "+authorizationBearer)
544 }
545
546 if csrfTokenName != "" && csrfTokenValue != "" {
547 req.Header.Set("X-"+csrfTokenName, csrfTokenValue)
548 }
549
550 for _, cookie := range cookies {
551 req.AddCookie(cookie)
552 }
553
554 client := http.Client{Timeout: 15 * time.Second}
555 resp, err := client.Do(req)
556 if err != nil {
557 t.Fatal(err)
558 }
559
560 return resp
561}
562
563func httpGet(url string, basicAuthUsername, basicAuthPassword, xapikeyHeader, authorizationBearer string, cookies []*http.Cookie, t *testing.T) *http.Response {
564 t.Helper()

Callers 3

httpGetFunction · 0.85
httpPostFunction · 0.85
TestHTTPLoginFunction · 0.85

Calls 5

HelperMethod · 0.80
MarshalMethod · 0.80
FatalMethod · 0.80
DoMethod · 0.80
SetMethod · 0.45

Tested by

no test coverage detected