Skip to content

Commit

Permalink
switch to go 1.16, fix all deprecation warns
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Nov 17, 2021
1 parent ddd7b77 commit ce16769
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Expand Up @@ -27,7 +27,7 @@ linters-settings:
linters:
enable:
- megacheck
- golint
- revive
- govet
- unconvert
- megacheck
Expand Down
6 changes: 3 additions & 3 deletions basic_auth_test.go
Expand Up @@ -25,15 +25,15 @@ func TestBasicAuth(t *testing.T) {
client := http.Client{Timeout: 5 * time.Second}

{
req, err := http.NewRequest("GET", u, nil)
req, err := http.NewRequest("GET", u, http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
}

{
req, err := http.NewRequest("GET", u, nil)
req, err := http.NewRequest("GET", u, http.NoBody)
require.NoError(t, err)
req.SetBasicAuth("dev", "good")
resp, err := client.Do(req)
Expand All @@ -42,7 +42,7 @@ func TestBasicAuth(t *testing.T) {
}

{
req, err := http.NewRequest("GET", u, nil)
req, err := http.NewRequest("GET", u, http.NoBody)
require.NoError(t, err)
req.SetBasicAuth("dev", "bad")
resp, err := client.Do(req)
Expand Down
6 changes: 3 additions & 3 deletions blackwords.go
Expand Up @@ -2,7 +2,7 @@ package rest

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"strings"
)
Expand All @@ -13,9 +13,9 @@ func BlackWords(words ...string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {

if content, err := ioutil.ReadAll(r.Body); err == nil {
if content, err := io.ReadAll(r.Body); err == nil {
body := strings.ToLower(string(content))
r.Body = ioutil.NopCloser(bytes.NewReader(content))
r.Body = io.NopCloser(bytes.NewReader(content))

if len(body) > 0 {
for _, word := range words {
Expand Down
2 changes: 1 addition & 1 deletion depricattion.go
Expand Up @@ -11,7 +11,7 @@ import (
func Deprecation(version string, date time.Time) func(http.Handler) http.Handler {
f := func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
headerVal := fmt.Sprintf("version=\"%s\", date=\"%s\"", version, date.Format(time.RFC3339))
headerVal := fmt.Sprintf("version=%q, date=%q", version, date.Format(time.RFC3339))
w.Header().Set("Deprecation", headerVal)
h.ServeHTTP(w, r)
}
Expand Down
2 changes: 1 addition & 1 deletion depricattion_test.go
Expand Up @@ -19,7 +19,7 @@ func TestDeprecated(t *testing.T) {
u := fmt.Sprintf("%s%s", ts.URL, "/something")

client := http.Client{Timeout: 5 * time.Second}
req, err := http.NewRequest("GET", u, nil)
req, err := http.NewRequest("GET", u, http.NoBody)
require.NoError(t, err)

r, err := client.Do(req)
Expand Down
3 changes: 1 addition & 2 deletions file_server.go
Expand Up @@ -3,7 +3,6 @@ package rest
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -172,7 +171,7 @@ func custom404Handler(next http.Handler, notFound io.Reader) (http.Handler, erro
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) }), nil
}

body, err := ioutil.ReadAll(notFound)
body, err := io.ReadAll(notFound)
if err != nil {
return nil, err
}
Expand Down
40 changes: 20 additions & 20 deletions file_server_test.go
Expand Up @@ -2,7 +2,7 @@ package rest

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strconv"
Expand Down Expand Up @@ -56,19 +56,19 @@ func TestFileServerDefault(t *testing.T) {
tt := tt
t.Run(strconv.Itoa(i), func(t *testing.T) {
for _, ts := range []*httptest.Server{ts1, ts2} {
req, err := http.NewRequest("GET", ts.URL+tt.req, nil)
req, err := http.NewRequest("GET", ts.URL+tt.req, http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
t.Logf("headers: %v", resp.Header)
assert.Equal(t, tt.status, resp.StatusCode)
if resp.StatusCode == http.StatusNotFound {
msg, e := ioutil.ReadAll(resp.Body)
msg, e := io.ReadAll(resp.Body)
require.NoError(t, e)
assert.Equal(t, "404 page not found\n", string(msg))
return
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, tt.body, string(body))
}
Expand All @@ -84,12 +84,12 @@ func TestFileServerWithListing(t *testing.T) {
client := http.Client{Timeout: 599 * time.Second}

{
req, err := http.NewRequest("GET", ts.URL+"/static/1", nil)
req, err := http.NewRequest("GET", ts.URL+"/static/1", http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
msg, err := ioutil.ReadAll(resp.Body)
msg, err := io.ReadAll(resp.Body)
require.NoError(t, err)
exp := `<pre>
<a href="f1.html">f1.html</a>
Expand All @@ -100,18 +100,18 @@ func TestFileServerWithListing(t *testing.T) {
}

{
req, err := http.NewRequest("GET", ts.URL+"/static/xyz.js", nil)
req, err := http.NewRequest("GET", ts.URL+"/static/xyz.js", http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
msg, err := ioutil.ReadAll(resp.Body)
msg, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "testdata/xyz.js", string(msg))
}

{
req, err := http.NewRequest("GET", ts.URL+"/static/no-such-thing.html", nil)
req, err := http.NewRequest("GET", ts.URL+"/static/no-such-thing.html", http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
Expand All @@ -128,45 +128,45 @@ func TestFileServer_Custom404(t *testing.T) {
client := http.Client{Timeout: 599 * time.Second}

{
req, err := http.NewRequest("GET", ts.URL+"/static/xyz.js", nil)
req, err := http.NewRequest("GET", ts.URL+"/static/xyz.js", http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
msg, err := ioutil.ReadAll(resp.Body)
msg, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "testdata/xyz.js", string(msg))
}

{
req, err := http.NewRequest("GET", ts.URL+"/static/nofile.js", nil)
req, err := http.NewRequest("GET", ts.URL+"/static/nofile.js", http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
msg, err := ioutil.ReadAll(resp.Body)
msg, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "custom 404", string(msg))
}

{
req, err := http.NewRequest("GET", ts.URL+"/xyz.html", nil)
req, err := http.NewRequest("GET", ts.URL+"/xyz.html", http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
msg, err := ioutil.ReadAll(resp.Body)
msg, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "custom 404", string(msg))
}

{
req, err := http.NewRequest("GET", ts.URL+"/static/xyz.js", nil)
req, err := http.NewRequest("GET", ts.URL+"/static/xyz.js", http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
msg, err := ioutil.ReadAll(resp.Body)
msg, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "testdata/xyz.js", string(msg))
}
Expand Down Expand Up @@ -213,19 +213,19 @@ func TestFileServerSPA(t *testing.T) {
tt := tt
t.Run(strconv.Itoa(i), func(t *testing.T) {
for _, ts := range []*httptest.Server{ts1, ts2} {
req, err := http.NewRequest("GET", ts.URL+tt.req, nil)
req, err := http.NewRequest("GET", ts.URL+tt.req, http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
t.Logf("headers: %v", resp.Header)
assert.Equal(t, tt.status, resp.StatusCode)
if resp.StatusCode == http.StatusNotFound {
msg, e := ioutil.ReadAll(resp.Body)
msg, e := io.ReadAll(resp.Body)
require.NoError(t, e)
assert.Equal(t, "404 page not found\n", string(msg))
return
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, tt.body, string(body))
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
@@ -1,6 +1,6 @@
module github.com/go-pkgz/rest

go 1.15
go 1.16

require (
github.com/pkg/errors v0.9.1
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Expand Up @@ -4,10 +4,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
3 changes: 1 addition & 2 deletions gzip.go
Expand Up @@ -3,7 +3,6 @@ package rest
import (
"compress/gzip"
"io"
"io/ioutil"
"net/http"
"strings"
"sync"
Expand All @@ -21,7 +20,7 @@ var gzDefaultContentTypes = []string{
}

var gzPool = sync.Pool{
New: func() interface{} { return gzip.NewWriter(ioutil.Discard) },
New: func() interface{} { return gzip.NewWriter(io.Discard) },
}

type gzipResponseWriter struct {
Expand Down
26 changes: 13 additions & 13 deletions gzip_test.go
Expand Up @@ -3,7 +3,7 @@ package rest
import (
"bytes"
"compress/gzip"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -29,35 +29,35 @@ func TestGzipCustom(t *testing.T) {
client := http.Client{}

{
req, err := http.NewRequest("GET", ts.URL+"/something", nil)
req, err := http.NewRequest("GET", ts.URL+"/something", http.NoBody)
require.NoError(t, err)
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, 357, len(b), "compressed size")

gzr, err := gzip.NewReader(bytes.NewBuffer(b))
require.NoError(t, err)
b, err = ioutil.ReadAll(gzr)
b, err = io.ReadAll(gzr)
require.NoError(t, err)
assert.True(t, strings.HasPrefix(string(b), "Lorem Ipsum"), string(b))
}

{
req, err := http.NewRequest("GET", ts.URL+"/something", nil)
req, err := http.NewRequest("GET", ts.URL+"/something", http.NoBody)
require.NoError(t, err)
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set("Content-Type", "something")
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, 576, len(b), "uncompressed size")
}
Expand All @@ -80,47 +80,47 @@ func TestGzipDefault(t *testing.T) {
client := http.Client{}

{
req, err := http.NewRequest("GET", ts.URL+"/something", nil)
req, err := http.NewRequest("GET", ts.URL+"/something", http.NoBody)
require.NoError(t, err)
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set("Content-Type", "text/plain")
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, 357, len(b), "compressed size")

gzr, err := gzip.NewReader(bytes.NewBuffer(b))
require.NoError(t, err)
b, err = ioutil.ReadAll(gzr)
b, err = io.ReadAll(gzr)
require.NoError(t, err)
assert.True(t, strings.HasPrefix(string(b), "Lorem Ipsum"), string(b))
}

{
req, err := http.NewRequest("GET", ts.URL+"/something", nil)
req, err := http.NewRequest("GET", ts.URL+"/something", http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.Nil(t, err)
assert.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, 576, len(b), "uncompressed size")
}

{
req, err := http.NewRequest("GET", ts.URL+"/something", nil)
req, err := http.NewRequest("GET", ts.URL+"/something", http.NoBody)
require.NoError(t, err)
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set("Content-Type", "something")
resp, err := client.Do(req)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, 576, len(b), "uncompressed size")
}
Expand Down

0 comments on commit ce16769

Please sign in to comment.