Skip to content

Commit

Permalink
[chore] cleanup confighttp tests (#11176)
Browse files Browse the repository at this point in the history
#### Description

Minor life improvements when reviewing the tests,

- Using stdlib for error reading
- Using the config client from test server
- Moving examples into their own file for easy discovery/maintainability

#### Link to tracking issue
N/A
  • Loading branch information
MovieStoreGuy authored Sep 19, 2024
1 parent 37f7833 commit 6fab8ae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
43 changes: 17 additions & 26 deletions config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"compress/gzip"
"compress/zlib"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/iotest"

"github.com/golang/snappy"
"github.com/klauspost/compress/zstd"
Expand Down Expand Up @@ -142,7 +144,7 @@ func TestHTTPCustomDecompression(t *testing.T) {
require.NoError(t, err, "failed to create request to test handler")
req.Header.Set("Content-Encoding", "custom-encoding")

client := http.Client{}
client := srv.Client()
res, err := client.Do(req)
require.NoError(t, err)

Expand Down Expand Up @@ -260,7 +262,7 @@ func TestHTTPContentDecompressionHandler(t *testing.T) {
require.NoError(t, err, "failed to create request to test handler")
req.Header.Set("Content-Encoding", tt.encoding)

client := http.Client{}
client := srv.Client()
res, err := client.Do(req)
require.NoError(t, err)

Expand All @@ -276,18 +278,18 @@ func TestHTTPContentDecompressionHandler(t *testing.T) {

func TestHTTPContentCompressionRequestWithNilBody(t *testing.T) {
compressedGzipBody := compressGzip(t, []byte{})
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
body, err := io.ReadAll(r.Body)
assert.NoError(t, err, "failed to read request body: %v", err)
assert.EqualValues(t, compressedGzipBody.Bytes(), body)
}))
defer server.Close()
defer srv.Close()

req, err := http.NewRequest(http.MethodGet, server.URL, nil)
req, err := http.NewRequest(http.MethodGet, srv.URL, nil)
require.NoError(t, err, "failed to create request to test handler")

client := http.Client{}
client := srv.Client()
client.Transport, err = newCompressRoundTripper(http.DefaultTransport, configcompression.TypeGzip)
require.NoError(t, err)
res, err := client.Do(req)
Expand All @@ -298,27 +300,16 @@ func TestHTTPContentCompressionRequestWithNilBody(t *testing.T) {
require.NoError(t, res.Body.Close(), "failed to close request body: %v", err)
}

type copyFailBody struct {
}

func (*copyFailBody) Read([]byte) (n int, err error) {
return 0, fmt.Errorf("read failed")
}

func (*copyFailBody) Close() error {
return nil
}

func TestHTTPContentCompressionCopyError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(server.Close)
t.Cleanup(srv.Close)

req, err := http.NewRequest(http.MethodGet, server.URL, &copyFailBody{})
req, err := http.NewRequest(http.MethodGet, srv.URL, iotest.ErrReader(errors.New("read failed")))
require.NoError(t, err)

client := http.Client{}
client := srv.Client()
client.Transport, err = newCompressRoundTripper(http.DefaultTransport, configcompression.TypeGzip)
require.NoError(t, err)
_, err = client.Do(req)
Expand All @@ -334,15 +325,15 @@ func (*closeFailBody) Close() error {
}

func TestHTTPContentCompressionRequestBodyCloseError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(server.Close)
t.Cleanup(srv.Close)

req, err := http.NewRequest(http.MethodGet, server.URL, &closeFailBody{Buffer: bytes.NewBuffer([]byte("blank"))})
req, err := http.NewRequest(http.MethodGet, srv.URL, &closeFailBody{Buffer: bytes.NewBuffer([]byte("blank"))})
require.NoError(t, err)

client := http.Client{}
client := srv.Client()
client.Transport, err = newCompressRoundTripper(http.DefaultTransport, configcompression.TypeGzip)
require.NoError(t, err)
_, err = client.Do(req)
Expand All @@ -362,7 +353,7 @@ func TestOverrideCompressionList(t *testing.T) {
require.NoError(t, err, "failed to create request to test handler")
req.Header.Set("Content-Encoding", "snappy")

client := http.Client{}
client := srv.Client()

// test
res, err := client.Do(req)
Expand Down
33 changes: 33 additions & 0 deletions config/confighttp/confighttp_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package confighttp

import (
"context"
"net/http"

"go.opentelemetry.io/collector/component/componenttest"
)

func ExampleServerConfig() {
settings := NewDefaultServerConfig()
settings.Endpoint = "localhost:443"

s, err := settings.ToServer(
context.Background(),
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings(),
http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
if err != nil {
panic(err)
}

l, err := settings.ToListener(context.Background())
if err != nil {
panic(err)
}
if err = s.Serve(l); err != nil {
panic(err)
}
}
22 changes: 0 additions & 22 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,28 +1011,6 @@ func verifyHeadersResp(t *testing.T, url string, expected map[string]configopaqu
}
}

func ExampleServerConfig() {
settings := NewDefaultServerConfig()
settings.Endpoint = "localhost:443"

s, err := settings.ToServer(
context.Background(),
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings(),
http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
if err != nil {
panic(err)
}

l, err := settings.ToListener(context.Background())
if err != nil {
panic(err)
}
if err = s.Serve(l); err != nil {
panic(err)
}
}

func TestHttpClientHeaders(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 6fab8ae

Please sign in to comment.