fix lint warnings (issue 2541) (#2551)

* Lint: fix some errcheck #2541

* Lint: fix passing structcheck #2541

* Lint: update fix structcheck #2541

* Lint: fix errcheck for basicauth, browse, fastcgi_test #2541

* Lint: fix errcheck for browse, fastcgi_test, fcgiclient, fcgiclient_test #2541

* Lint: fix errcheck for responsefilter_test, fcgilient_test #2541

* Lint: fix errcheck for header_test #2541

* Lint: update errcheck for fcgiclient_test #2541

* Lint: fix errcheck for server, header_test, fastcgi_test, https_test, recorder_test #2541

* Lint: fix errcheck for tplcontext, vhosttrie_test, internal_test, handler_test #2541

* Lint: fix errcheck for log_test, markdown mholt#2541

* Lint: fix errcheck for policy, body_test, proxy_test #2541

* Lint: fix errcheck for on multiple packages #2541

- reverseproxy
- reverseproxy_test
- upstream
- upstream_test
- body_test

* Lint: fix errcheck in multiple packages mholt#2541
- handler_test
- redirect_test
- requestid_test
- rewrite_test
- fileserver_test

* Lint: fix errcheck in multiple packages mholt#2541

- websocket
- setup
- collection
- redirect_test
- templates_test

* Lint: fix errcheck in logger test #2541

run goimports against #2551
- lexer_test
- log_test
- markdown

* Update caddyhttp/httpserver/logger_test.go

Co-Authored-By: Inconnu08 <taufiqrx8@gmail.com>

* Update log_test.go

* Lint: fix scope in logger_test #2541

* remove redundant err check in logger_test #2541

* fix alias in logger_test #2541

* fix import for format #2541

* refactor variable names and error check #2541
This commit is contained in:
Taufiq Rahman
2019-04-22 22:20:37 +06:00
committed by Matt Holt
parent 0c3d90ed21
commit c32a0f5f71
35 changed files with 498 additions and 220 deletions

View File

@@ -16,6 +16,7 @@ package httpserver
import (
"fmt"
"log"
"net"
"net/http"
"net/http/httptest"
@@ -180,7 +181,9 @@ func TestEnableAutoHTTPS(t *testing.T) {
{}, // not managed - no changes!
}
enableAutoHTTPS(configs, false)
if err := enableAutoHTTPS(configs, false); err != nil {
log.Println("[ERROR] enableAutoHTTPS failed: ", err)
}
if !configs[0].TLS.Enabled {
t.Errorf("Expected config 0 to have TLS.Enabled == true, but it was false")

View File

@@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
@@ -179,9 +180,13 @@ func bootServer(location string, ch chan format.LogParts) (*syslog.Server, error
switch address.network {
case "tcp":
server.ListenTCP(address.address)
if err := server.ListenTCP(address.address); err != nil {
log.Println("[ERROR] server failed to listen on TCP address: ", err)
}
case "udp":
server.ListenUDP(address.address)
if err := server.ListenUDP(address.address); err != nil {
log.Println("[ERROR] server failed to listen on UDP address: ", err)
}
}
server.SetHandler(syslog.NewChannelHandler(ch))

View File

@@ -44,7 +44,7 @@ func TestWrite(t *testing.T) {
responseTestString := "test"
recordRequest := NewResponseRecorder(w)
buf := []byte(responseTestString)
recordRequest.Write(buf)
_, _ = recordRequest.Write(buf)
if recordRequest.size != len(buf) {
t.Fatalf("Expected the bytes written counter to be %d, but instead found %d\n", len(buf), recordRequest.size)
}

View File

@@ -234,7 +234,9 @@ func makeHTTPServerWithTimeouts(addr string, group []*SiteConfig) *http.Server {
func (s *Server) wrapWithSvcHeaders(previousHandler http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
s.quicServer.SetQuicHeaders(w.Header())
if err := s.quicServer.SetQuicHeaders(w.Header()); err != nil {
log.Println("[Error] failed to set proper headers for QUIC: ", err)
}
previousHandler.ServeHTTP(w, r)
}
}
@@ -243,7 +245,7 @@ func (s *Server) wrapWithSvcHeaders(previousHandler http.Handler) http.HandlerFu
// used to serve requests.
func (s *Server) Listen() (net.Listener, error) {
if s.Server == nil {
return nil, fmt.Errorf("Server field is nil")
return nil, fmt.Errorf("server field is nil")
}
ln, err := net.Listen("tcp", s.Server.Addr)
@@ -324,7 +326,9 @@ func (s *Server) Serve(ln net.Listener) error {
defer func() {
if s.quicServer != nil {
s.quicServer.Close()
if err := s.quicServer.Close(); err != nil {
log.Println("[ERROR] failed to close QUIC server: ", err)
}
}
}()
@@ -555,8 +559,12 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
if err = tc.SetKeepAlive(true); err != nil {
return
}
if err = tc.SetKeepAlivePeriod(3 * time.Minute); err != nil {
return
}
return tc, nil
}
@@ -594,7 +602,9 @@ func WriteTextResponse(w http.ResponseWriter, status int, body string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(status)
w.Write([]byte(body))
if _, err := w.Write([]byte(body)); err != nil {
log.Println("[Error] failed to write body: ", err)
}
}
// SafePath joins siteRoot and reqPath and converts it to a path that can

View File

@@ -19,6 +19,7 @@ import (
"crypto/rand"
"fmt"
"io/ioutil"
"log"
mathrand "math/rand"
"net"
"net/http"
@@ -420,7 +421,9 @@ func (c Context) RandomString(minLen, maxLen int) string {
// secureRandomBytes returns a number of bytes using crypto/rand.
secureRandomBytes := func(numBytes int) []byte {
randomBytes := make([]byte, numBytes)
rand.Read(randomBytes)
if _, err := rand.Read(randomBytes); err != nil {
log.Println("[ERROR] failed to read bytes: ", err)
}
return randomBytes
}

View File

@@ -15,6 +15,7 @@
package httpserver
import (
"log"
"net/http"
"net/http/httptest"
"testing"
@@ -103,7 +104,9 @@ func populateTestTrie(trie *vhostTrie, keys []string) {
func(key string) {
site := &SiteConfig{
middlewareChain: HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
w.Write([]byte(key))
if _, err := w.Write([]byte(key)); err != nil {
log.Println("[ERROR] failed to write bytes: ", err)
}
return 0, nil
}),
}
@@ -139,7 +142,9 @@ func assertTestTrie(t *testing.T, trie *vhostTrie, tests []vhostTrieTest, hasWil
// And it must be the correct value
resp := httptest.NewRecorder()
site.middlewareChain.ServeHTTP(resp, nil)
if _, err := site.middlewareChain.ServeHTTP(resp, nil); err != nil {
log.Println("[ERROR] failed to serve HTTP: ", err)
}
actualHandlerKey := resp.Body.String()
if actualHandlerKey != test.expectedKey {
t.Errorf("Test %d: Expected match '%s' but matched '%s'",