Skip to content

[Bug]: NewRESTHandler does not inject HTTP headers into CallContext ServiceParams #275

@cchinchilla-dev

Description

@cchinchilla-dev

What happened?

NewRESTHandler does not call NewCallContext(ctx, NewServiceParams(req.Header)) in any of its handler methods. Because of this, HTTP headers from incoming requests are not accessible via callCtx.ServiceParams() inside a CallInterceptor.

NewJSONRPCHandler does inject them in its ServeHTTP (jsonrpc_server.go:52):

ctx, _ = a2asrv.NewCallContext(ctx, ToServiceParams(req.Header))

The REST handler methods pass req.Context() directly, so attachMethodCallContext creates a CallContext with nil ServiceParams.

Steps to reproduce

Add this test to rest_server_test.go:

func TestREST_ServiceParams(t *testing.T) {
	ctx := t.Context()
	tid := a2a.NewTaskID()
	task := &a2a.Task{ID: tid, ContextID: a2a.NewContextID()}
	store := testutil.NewTestTaskStore().WithTasks(t, task)

	var gotAuth []string
	interceptor := &testInterceptor{
		BeforeFn: func(ctx context.Context, callCtx *CallContext, req *Request) (context.Context, any, error) {
			gotAuth, _ = callCtx.ServiceParams().Get("authorization")
			return ctx, nil, nil
		},
	}

	handler := NewHandler(&mockAgentExecutor{}, WithTaskStore(store), WithCallInterceptors(interceptor))
	server := httptest.NewServer(NewRESTHandler(handler))
	defer server.Close()

	req, err := http.NewRequestWithContext(ctx, "GET", server.URL+"/tasks/"+string(tid), nil)
	if err != nil {
		t.Fatalf("http.NewRequestWithContext() error = %v", err)
	}
	req.Header.Set("Authorization", "Bearer test-token")

	resp, err := server.Client().Do(req)
	if err != nil {
		t.Fatalf("server.Client().Do() error = %v", err)
	}
	defer resp.Body.Close()

	if len(gotAuth) == 0 || gotAuth[0] != "Bearer test-token" {
		t.Errorf("ServiceParams[authorization] = %v, want [Bearer test-token]", gotAuth)
	}
}

Run:

go test ./a2asrv/ -v -run TestREST_ServiceParams

Expected

callCtx.ServiceParams().Get("authorization") returns ["Bearer test-token"].

Actual

=== RUN   TestREST_ServiceParams
    example_test.go:44: ServiceParams[authorization] = [], want [Bearer test-token]
--- FAIL: TestREST_ServiceParams (0.00s)

Relevant log output

=== RUN   TestREST_ServiceParams
    example_test.go:44: ServiceParams[authorization] = [], want [Bearer test-token]
--- FAIL: TestREST_ServiceParams (0.00s)
FAIL
FAIL    github.com/a2aproject/a2a-go/v2/a2asrv  0.420s
FAIL

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions