@@ -24,6 +24,7 @@ import (
2424 "fmt"
2525 "net/http"
2626 "net/http/httptest"
27+ "strings"
2728 "testing"
2829 "time"
2930
@@ -33,9 +34,12 @@ import (
3334 "github.com/stretchr/testify/require"
3435 "go.opentelemetry.io/collector/client"
3536 "go.opentelemetry.io/collector/component/componenttest"
37+ "go.opentelemetry.io/collector/extension/extensiontest"
3638 "google.golang.org/genproto/googleapis/rpc/errdetails"
3739 "google.golang.org/grpc/codes"
3840 "google.golang.org/grpc/status"
41+
42+ "github.com/elastic/opentelemetry-collector-components/extension/apikeyauthextension/internal/metadata"
3943)
4044
4145const user = "test"
@@ -196,6 +200,21 @@ func TestAuthenticator_Caching(t *testing.T) {
196200 assert .EqualError (t , err , `rpc error: code = Unauthenticated desc = API Key "id2" unauthorized` )
197201}
198202
203+ func TestAuthenticator_UserAgent (t * testing.T ) {
204+ srv := newMockElasticsearch (t , func (w http.ResponseWriter , r * http.Request ) {
205+ wantPrefix := "OpenTelemetry Collector/latest ("
206+ userAgent := r .Header .Get ("User-Agent" )
207+ assert .Truef (t , strings .HasPrefix (userAgent , wantPrefix ), "want prefix %s; got %s" , wantPrefix , userAgent )
208+ assert .NoError (t , json .NewEncoder (w ).Encode (successfulResponse ))
209+ })
210+
211+ authenticator := newTestAuthenticator (t , srv , createDefaultConfig ().(* Config ))
212+ _ , err := authenticator .Authenticate (context .Background (), map [string ][]string {
213+ "Authorization" : {"ApiKey " + base64 .StdEncoding .EncodeToString ([]byte ("id:secret" ))},
214+ })
215+ assert .NoError (t , err )
216+ }
217+
199218func TestAuthenticator_ErrorWithDetails (t * testing.T ) {
200219 for name , testcase := range map [string ]struct {
201220 setup func (* testing.T ) (* authenticator , map [string ][]string )
@@ -483,7 +502,7 @@ func BenchmarkAuthenticator(b *testing.B) {
483502
484503func newTestAuthenticator (t testing.TB , srv * httptest.Server , config * Config ) * authenticator {
485504 config .Endpoint = srv .URL
486- auth , err := newAuthenticator (config , componenttest . NewNopTelemetrySettings ( ))
505+ auth , err := newAuthenticator (config , extensiontest . NewNopSettings ( metadata . Type ))
487506 require .NoError (t , err )
488507
489508 err = auth .Start (context .Background (), componenttest .NewNopHost ())
0 commit comments