// Code generated by gotmpl. DO NOT MODIFY. // source: internal/shared/semconv/client_test.go.tmpl // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 package semconv import ( "net/http" "strconv" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" ) func TestHTTPClientStatus(t *testing.T) { tests := []struct { code int stat codes.Code msg bool }{ {0, codes.Error, true}, {http.StatusContinue, codes.Ok, false}, {http.StatusSwitchingProtocols, codes.Ok, false}, {http.StatusProcessing, codes.Ok, false}, {http.StatusEarlyHints, codes.Ok, false}, {http.StatusOK, codes.Ok, false}, {http.StatusCreated, codes.Ok, false}, {http.StatusAccepted, codes.Ok, false}, {http.StatusNonAuthoritativeInfo, codes.Ok, false}, {http.StatusNoContent, codes.Ok, false}, {http.StatusResetContent, codes.Ok, false}, {http.StatusPartialContent, codes.Ok, false}, {http.StatusMultiStatus, codes.Ok, false}, {http.StatusAlreadyReported, codes.Ok, false}, {http.StatusIMUsed, codes.Ok, false}, {http.StatusMultipleChoices, codes.Ok, false}, {http.StatusMovedPermanently, codes.Ok, false}, {http.StatusFound, codes.Ok, false}, {http.StatusSeeOther, codes.Ok, false}, {http.StatusNotModified, codes.Ok, false}, {http.StatusUseProxy, codes.Ok, false}, {306, codes.Ok, false}, {http.StatusTemporaryRedirect, codes.Ok, false}, {http.StatusPermanentRedirect, codes.Ok, false}, {http.StatusBadRequest, codes.Error, true}, {http.StatusUnauthorized, codes.Error, true}, {http.StatusPaymentRequired, codes.Error, true}, {http.StatusForbidden, codes.Error, true}, {http.StatusNotFound, codes.Error, true}, {http.StatusMethodNotAllowed, codes.Error, true}, {http.StatusNotAcceptable, codes.Error, true}, {http.StatusProxyAuthRequired, codes.Error, true}, {http.StatusRequestTimeout, codes.Error, true}, {http.StatusConflict, codes.Error, true}, {http.StatusGone, codes.Error, true}, {http.StatusLengthRequired, codes.Error, true}, {http.StatusPreconditionFailed, codes.Error, true}, {http.StatusRequestEntityTooLarge, codes.Error, true}, {http.StatusRequestURITooLong, codes.Error, true}, {http.StatusUnsupportedMediaType, codes.Error, true}, {http.StatusRequestedRangeNotSatisfiable, codes.Error, true}, {http.StatusExpectationFailed, codes.Error, true}, {http.StatusTeapot, codes.Error, true}, {http.StatusMisdirectedRequest, codes.Error, true}, {http.StatusUnprocessableEntity, codes.Error, true}, {http.StatusLocked, codes.Error, true}, {http.StatusFailedDependency, codes.Error, true}, {http.StatusTooEarly, codes.Error, true}, {http.StatusUpgradeRequired, codes.Error, true}, {http.StatusPreconditionRequired, codes.Error, true}, {http.StatusTooManyRequests, codes.Error, true}, {http.StatusRequestHeaderFieldsTooLarge, codes.Error, true}, {http.StatusUnavailableForLegalReasons, codes.Error, true}, {499, codes.Error, false}, {http.StatusInternalServerError, codes.Error, true}, {http.StatusNotImplemented, codes.Error, true}, {http.StatusBadGateway, codes.Error, true}, {http.StatusServiceUnavailable, codes.Error, true}, {http.StatusGatewayTimeout, codes.Error, true}, {http.StatusHTTPVersionNotSupported, codes.Error, true}, {http.StatusVariantAlsoNegotiates, codes.Error, true}, {http.StatusInsufficientStorage, codes.Error, true}, {http.StatusLoopDetected, codes.Error, true}, {http.StatusNotExtended, codes.Error, true}, {http.StatusNetworkAuthenticationRequired, codes.Error, true}, {600, codes.Error, true}, } for _, test := range tests { t.Run(strconv.Itoa(test.code), func(t *testing.T) { c, msg := HTTPClient{}.Status(test.code) assert.Equal(t, test.stat, c) if test.msg && msg == "" { t.Errorf("expected non-empty message for %d", test.code) } else if !test.msg && msg != "" { t.Errorf("expected empty message for %d, got: %s", test.code, msg) } }) } } func TestHTTPClient_MetricAttributes(t *testing.T) { defaultRequest, err := http.NewRequest("GET", "http://example.com/path?query=test", http.NoBody) require.NoError(t, err) httpsRequest, err := http.NewRequest("GET", "https://example.com/path?query=test", http.NoBody) require.NoError(t, err) tests := []struct { name string server string req *http.Request statusCode int additionalAttributes []attribute.KeyValue wantFunc func(t *testing.T, attrs []attribute.KeyValue) }{ { name: "routine testing", req: defaultRequest, statusCode: 200, additionalAttributes: []attribute.KeyValue{attribute.String("test", "test")}, wantFunc: func(t *testing.T, attrs []attribute.KeyValue) { require.Len(t, attrs, 7) assert.ElementsMatch(t, []attribute.KeyValue{ attribute.String("http.request.method", "GET"), attribute.String("server.address", "example.com"), attribute.String("url.scheme", "http"), attribute.String("network.protocol.name", "http"), attribute.String("network.protocol.version", "1.1"), attribute.Int64("http.response.status_code", 200), attribute.String("test", "test"), }, attrs) }, }, { name: "use server address", req: defaultRequest, statusCode: 200, additionalAttributes: nil, wantFunc: func(t *testing.T, attrs []attribute.KeyValue) { require.Len(t, attrs, 6) assert.ElementsMatch(t, []attribute.KeyValue{ attribute.String("http.request.method", "GET"), attribute.String("server.address", "example.com"), attribute.String("url.scheme", "http"), attribute.String("network.protocol.name", "http"), attribute.String("network.protocol.version", "1.1"), attribute.Int64("http.response.status_code", 200), }, attrs) }, }, { name: "https scheme", req: httpsRequest, statusCode: 200, additionalAttributes: nil, wantFunc: func(t *testing.T, attrs []attribute.KeyValue) { require.Len(t, attrs, 6) assert.ElementsMatch(t, []attribute.KeyValue{ attribute.String("http.request.method", "GET"), attribute.String("server.address", "example.com"), attribute.String("url.scheme", "https"), attribute.String("network.protocol.name", "http"), attribute.String("network.protocol.version", "1.1"), attribute.Int64("http.response.status_code", 200), }, attrs) }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := HTTPClient{}.MetricAttributes(tt.req, tt.statusCode, tt.additionalAttributes) tt.wantFunc(t, got) }) } }