MCPcopy Index your code
hub / github.com/ddev/ddev / GetLocalHTTPResponse

Function GetLocalHTTPResponse

pkg/testcommon/testcommon.go:453–539  ·  view source on GitHub ↗

GetLocalHTTPResponse takes a URL and optional parameters, hits the local Docker for it, returns result Returns error (with the body) if not 200 status code. Parameters can be either: - HTTPRequestOpts struct with TimeoutSeconds and MaxRetries fields - int representing timeout seconds (for backward c

(t *testing.T, rawurl string, params ...any)

Source from the content-addressed store, hash-verified

451// - HTTPRequestOpts struct with TimeoutSeconds and MaxRetries fields
452// - int representing timeout seconds (for backward compatibility)
453func GetLocalHTTPResponse(t *testing.T, rawurl string, params ...any) (string, *http.Response, error) {
454 options := parseHTTPRequestOpts(60, params...)
455
456 timeoutTime := time.Duration(options.TimeoutSeconds) * time.Second
457 assert := asrt.New(t)
458
459 u, err := url.Parse(rawurl)
460 if err != nil {
461 t.Fatalf("Failed to parse url %s: %v", rawurl, err)
462 }
463 port := u.Port()
464
465 dockerIP, err := dockerutil.GetDockerIP()
466 assert.NoError(err)
467
468 fakeHost := u.Hostname()
469 // Add the port if there is one.
470 u.Host = dockerIP
471 if port != "" {
472 u.Host = u.Host + ":" + port
473 }
474 localAddress := u.String()
475
476 // Use ServerName: fakeHost to verify basic usage of certificate.
477 // This technique is from https://stackoverflow.com/a/47169975/215713
478 transport := &http.Transport{
479 TLSClientConfig: &tls.Config{ServerName: fakeHost},
480 }
481
482 // Do not follow redirects, https://stackoverflow.com/a/38150816/215713
483 client := &http.Client{
484 CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
485 return http.ErrUseLastResponse
486 },
487 Transport: transport,
488 Timeout: timeoutTime,
489 }
490
491 var lastErr error
492 var resp *http.Response
493 var bodyString string
494
495 for attempt := 1; attempt <= options.MaxRetries; attempt++ {
496 req, err := http.NewRequest("GET", localAddress, nil)
497 if err != nil {
498 return "", nil, fmt.Errorf("failed to NewRequest GET %s: %v", localAddress, err)
499 }
500 req.Host = fakeHost
501
502 resp, err = client.Do(req)
503 if err != nil {
504 lastErr = err
505 if attempt < options.MaxRetries {
506 time.Sleep(time.Second)
507 continue
508 }
509 return "", resp, err
510 }

Callers 15

TestConfigValidateFunction · 0.92
TestPHPConfigFunction · 0.92
TestConfigFunctionalityFunction · 0.92
TestDdevXdebugEnabledFunction · 0.92
TestDdevFullSiteSetupFunction · 0.92
TestHttpsRedirectionFunction · 0.92
TestPHPWebserverTypeFunction · 0.92
TestHardenedStartFunction · 0.92
TestCustomGlobalConfigFunction · 0.92

Calls 3

GetDockerIPFunction · 0.92
parseHTTPRequestOptsFunction · 0.85
ErrorfMethod · 0.80

Tested by 14

TestConfigValidateFunction · 0.74
TestPHPConfigFunction · 0.74
TestConfigFunctionalityFunction · 0.74
TestDdevXdebugEnabledFunction · 0.74
TestDdevFullSiteSetupFunction · 0.74
TestHttpsRedirectionFunction · 0.74
TestPHPWebserverTypeFunction · 0.74
TestHardenedStartFunction · 0.74
TestCustomGlobalConfigFunction · 0.74