Code maintenance: use native Go HTTP client
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
@@ -12,14 +14,15 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/os"
|
||||
"github.com/qdm12/gluetun/internal/os/mock_os"
|
||||
"github.com/qdm12/golibs/logging/mock_logging"
|
||||
"github.com/qdm12/golibs/network/mock_network"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_downloadAndSave(t *testing.T) {
|
||||
t.Parallel()
|
||||
const defaultURL = "https://test.com"
|
||||
tests := map[string]struct {
|
||||
url string // to trigger a new request error
|
||||
content []byte
|
||||
status int
|
||||
clientErr error
|
||||
@@ -30,37 +33,39 @@ func Test_downloadAndSave(t *testing.T) {
|
||||
err error
|
||||
}{
|
||||
"no data": {
|
||||
url: defaultURL,
|
||||
status: http.StatusOK,
|
||||
},
|
||||
"bad status": {
|
||||
url: defaultURL,
|
||||
status: http.StatusBadRequest,
|
||||
err: fmt.Errorf("HTTP status code is 400 for https://raw.githubusercontent.com/qdm12/files/master/named.root.updated"), //nolint:lll
|
||||
err: fmt.Errorf("bad HTTP status from %s: Bad Request", defaultURL),
|
||||
},
|
||||
"client error": {
|
||||
url: defaultURL,
|
||||
clientErr: fmt.Errorf("error"),
|
||||
err: fmt.Errorf("error"),
|
||||
err: fmt.Errorf("Get %q: error", defaultURL),
|
||||
},
|
||||
"open error": {
|
||||
url: defaultURL,
|
||||
status: http.StatusOK,
|
||||
openErr: fmt.Errorf("error"),
|
||||
err: fmt.Errorf("error"),
|
||||
},
|
||||
"write error": {
|
||||
status: http.StatusOK,
|
||||
writeErr: fmt.Errorf("error"),
|
||||
err: fmt.Errorf("error"),
|
||||
},
|
||||
"chown error": {
|
||||
url: defaultURL,
|
||||
status: http.StatusOK,
|
||||
chownErr: fmt.Errorf("error"),
|
||||
err: fmt.Errorf("error"),
|
||||
},
|
||||
"close error": {
|
||||
url: defaultURL,
|
||||
status: http.StatusOK,
|
||||
closeErr: fmt.Errorf("error"),
|
||||
err: fmt.Errorf("error"),
|
||||
},
|
||||
"data": {
|
||||
url: defaultURL,
|
||||
content: []byte("content"),
|
||||
status: http.StatusOK,
|
||||
},
|
||||
@@ -73,30 +78,46 @@ func Test_downloadAndSave(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
logger := mock_logging.NewMockLogger(mockCtrl)
|
||||
logger.EXPECT().Info("downloading %s from %s", "root hints", string(constants.NamedRootURL))
|
||||
client := mock_network.NewMockClient(mockCtrl)
|
||||
client.EXPECT().Get(ctx, string(constants.NamedRootURL)).
|
||||
Return(tc.content, tc.status, tc.clientErr)
|
||||
logger.EXPECT().Info("downloading %s from %s", "root hints", tc.url)
|
||||
|
||||
client := &http.Client{
|
||||
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
assert.Equal(t, tc.url, r.URL.String())
|
||||
if tc.clientErr != nil {
|
||||
return nil, tc.clientErr
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: tc.status,
|
||||
Status: http.StatusText(tc.status),
|
||||
Body: ioutil.NopCloser(bytes.NewReader(tc.content)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
openFile := func(name string, flag int, perm os.FileMode) (os.File, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
const filepath = "/test"
|
||||
|
||||
if tc.clientErr == nil && tc.status == http.StatusOK {
|
||||
file := mock_os.NewMockFile(mockCtrl)
|
||||
if tc.openErr == nil {
|
||||
writeCall := file.EXPECT().Write(tc.content).
|
||||
Return(0, tc.writeErr)
|
||||
if tc.writeErr != nil {
|
||||
file.EXPECT().Close().Return(tc.closeErr).After(writeCall)
|
||||
} else {
|
||||
chownCall := file.EXPECT().Chown(1000, 1000).Return(tc.chownErr).After(writeCall)
|
||||
file.EXPECT().Close().Return(tc.closeErr).After(chownCall)
|
||||
if len(tc.content) > 0 {
|
||||
file.EXPECT().
|
||||
Write(tc.content).
|
||||
Return(len(tc.content), tc.writeErr)
|
||||
}
|
||||
file.EXPECT().
|
||||
Close().
|
||||
Return(tc.closeErr)
|
||||
file.EXPECT().
|
||||
Chown(1000, 1000).
|
||||
Return(tc.chownErr)
|
||||
}
|
||||
|
||||
openFile = func(name string, flag int, perm os.FileMode) (os.File, error) {
|
||||
assert.Equal(t, string(constants.RootHints), name)
|
||||
assert.Equal(t, filepath, name)
|
||||
assert.Equal(t, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, flag)
|
||||
assert.Equal(t, os.FileMode(0400), perm)
|
||||
return file, tc.openErr
|
||||
@@ -110,7 +131,7 @@ func Test_downloadAndSave(t *testing.T) {
|
||||
}
|
||||
|
||||
err := c.downloadAndSave(ctx, "root hints",
|
||||
string(constants.NamedRootURL), string(constants.RootHints),
|
||||
tc.url, filepath,
|
||||
1000, 1000)
|
||||
|
||||
if tc.err != nil {
|
||||
@@ -130,9 +151,13 @@ func Test_DownloadRootHints(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
logger := mock_logging.NewMockLogger(mockCtrl)
|
||||
logger.EXPECT().Info("downloading %s from %s", "root hints", string(constants.NamedRootURL))
|
||||
client := mock_network.NewMockClient(mockCtrl)
|
||||
client.EXPECT().Get(ctx, string(constants.NamedRootURL)).
|
||||
Return(nil, http.StatusOK, errors.New("test"))
|
||||
|
||||
client := &http.Client{
|
||||
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
assert.Equal(t, string(constants.NamedRootURL), r.URL.String())
|
||||
return nil, errors.New("test")
|
||||
}),
|
||||
}
|
||||
|
||||
c := &configurator{
|
||||
logger: logger,
|
||||
@@ -141,7 +166,7 @@ func Test_DownloadRootHints(t *testing.T) {
|
||||
|
||||
err := c.DownloadRootHints(ctx, 1000, 1000)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "test", err.Error())
|
||||
assert.Equal(t, `Get "https://raw.githubusercontent.com/qdm12/files/master/named.root.updated": test`, err.Error())
|
||||
}
|
||||
|
||||
func Test_DownloadRootKey(t *testing.T) {
|
||||
@@ -151,9 +176,13 @@ func Test_DownloadRootKey(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
logger := mock_logging.NewMockLogger(mockCtrl)
|
||||
logger.EXPECT().Info("downloading %s from %s", "root key", string(constants.RootKeyURL))
|
||||
client := mock_network.NewMockClient(mockCtrl)
|
||||
client.EXPECT().Get(ctx, string(constants.RootKeyURL)).
|
||||
Return(nil, http.StatusOK, errors.New("test"))
|
||||
|
||||
client := &http.Client{
|
||||
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
assert.Equal(t, string(constants.RootKeyURL), r.URL.String())
|
||||
return nil, errors.New("test")
|
||||
}),
|
||||
}
|
||||
|
||||
c := &configurator{
|
||||
logger: logger,
|
||||
@@ -162,5 +191,5 @@ func Test_DownloadRootKey(t *testing.T) {
|
||||
|
||||
err := c.DownloadRootKey(ctx, 1000, 1000)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "test", err.Error())
|
||||
assert.Equal(t, `Get "https://raw.githubusercontent.com/qdm12/files/master/root.key.updated": test`, err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user