Add metadata retrieved from the context to the user agent when a new HTTP client is created by MatteoPologruto · Pull Request #2789 · arduino/arduino-cli (original) (raw)

Here, if possible would be nice to extract the assertion code that is done by passing the true flag in the HTTPServerFile. Or make an abstraction that passes some assertion functions.

diff --git a/internal/integrationtest/daemon/daemon_test.go b/internal/integrationtest/daemon/daemon_test.go index 7d800c84..982ba8bb 100644 --- a/internal/integrationtest/daemon/daemon_test.go +++ b/internal/integrationtest/daemon/daemon_test.go @@ -20,6 +20,10 @@ import ( "errors" "fmt" "io" + "maps" + "net/http" + "net/http/httptest" + "strings" "testing" "time"

@@ -562,15 +566,41 @@ func TestDaemonUserAgent(t *testing.T) { // Set up an http server to serve our custom index file // The user-agent is tested inside the HTTPServeFile function test_index := paths.New("..", "testdata", "test_index.json") - url := env.HTTPServeFile(8000, test_index, true) + url := env.HTTPServeFile(8000, test_index) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Test that the user-agent contains metadata from the context when the CLI is in daemon mode + userAgent := r.Header.Get("User-Agent") + + require.Contains(t, userAgent, "cli-test/0.0.0") + require.Contains(t, userAgent, "grpc-go") + // Depends on how we built the client we may have git-snapshot or 0.0.0-git in dev releases + require.Condition(t, func() (success bool) { + return strings.Contains(userAgent, "arduino-cli/git-snapshot") || + strings.Contains(userAgent, "arduino-cli/0.0.0-git") + }) + + proxiedReq, err := http.NewRequest(r.Method, url.String(), r.Body) + require.NoError(t, err) + maps.Copy(proxiedReq.Header, r.Header) + + proxiedResp, err := http.DefaultTransport.RoundTrip(proxiedReq) + require.NoError(t, err) + defer proxiedResp.Body.Close() + + // Copy the headers from the proxy response to the original response + maps.Copy(r.Header, proxiedReq.Header) + w.WriteHeader(proxiedResp.StatusCode) + io.Copy(w, proxiedResp.Body) + })) + defer ts.Close()

 grpcInst := cli.Create()
 require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) {
     fmt.Printf("INIT> %v\n", ir.GetMessage())
 }))

@@ -579,7 +609,7 @@ func TestDaemonUserAgent(t *testing.T) { res, err := analyzeUpdateIndexClient(t, cl) require.NoError(t, err) require.Len(t, res, 2) - require.True(t, res[url.String()].GetSuccess()) + require.True(t, res[additionalURL].GetSuccess()) } }