Skip to content

Commit

Permalink
make linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF committed Jul 8, 2024
1 parent 3f70895 commit 4107d23
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 51 deletions.
56 changes: 5 additions & 51 deletions contrib/net/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@
package http

import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/contrib/internal/namingschematest"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"gopkg.in/DataDog/dd-trace-go.v1/internal/normalizer"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWithHeaderTags(t *testing.T) {
Expand Down Expand Up @@ -526,48 +522,6 @@ func TestServerNamingSchema(t *testing.T) {
namingschematest.NewHTTPServerTest(genSpans, "http.router")(t)
}

// TestUnwrap tests the implementation of the rwUnwrapper interface, which is used internally
// by the standard library: https://github.com/golang/go/blob/6d89b38ed86e0bfa0ddaba08dc4071e6bb300eea/src/net/http/responsecontroller.go#L42-L44
// See also: https://github.com/DataDog/dd-trace-go/issues/2674
func TestUnwrap(t *testing.T) {
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rc := http.NewResponseController(w)

// Use the SetReadDeadline and SetWriteDeadline methods, which are not explicitly implemented in the trace_gen.go
// generated file. Before the Unwrap change, these methods returned a "feature not supported" error.

err := rc.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
err = rc.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})

h := WrapHandler(fn, "service-name", "resource-name")
srv := httptest.NewServer(h)
defer srv.Close()

resp, err := srv.Client().Get(srv.URL + "/")
require.NoError(t, err)
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
respText := string(b)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "OK", respText)
}

func router(muxOpts ...Option) http.Handler {
defaultOpts := []Option{
WithServiceName("my-service"),
Expand Down
44 changes: 44 additions & 0 deletions contrib/net/http/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTraceAndServe(t *testing.T) {
Expand Down Expand Up @@ -357,6 +359,48 @@ func TestTraceAndServeHost(t *testing.T) {
})
}

// TestUnwrap tests the implementation of the rwUnwrapper interface, which is used internally
// by the standard library: https://github.com/golang/go/blob/6d89b38ed86e0bfa0ddaba08dc4071e6bb300eea/src/net/http/responsecontroller.go#L42-L44
// See also: https://github.com/DataDog/dd-trace-go/issues/2674
func TestUnwrap(t *testing.T) {
h := WrapHandler(deadlineHandler, "service-name", "resource-name")
srv := httptest.NewServer(h)
defer srv.Close()

resp, err := srv.Client().Get(srv.URL + "/")
require.NoError(t, err)
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
respText := string(b)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "OK", respText)
}

var deadlineHandler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
rc := http.NewResponseController(w)

// Use the SetReadDeadline and SetWriteDeadline methods, which are not explicitly implemented in the trace_gen.go
// generated file. Before the Unwrap change, these methods returned a "feature not supported" error.

err := rc.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
err = rc.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})

type noopHandler struct{}

func (noopHandler) ServeHTTP(_ http.ResponseWriter, _ *http.Request) {}
Expand Down

0 comments on commit 4107d23

Please sign in to comment.