Browse Source

Fixes by lint utility hints

Volodymyr Tkach 2 years ago
parent
commit
535f0201df
5 changed files with 80 additions and 30 deletions
  1. 7 3
      main.go
  2. 1 1
      session/bool.go
  3. 1 1
      session/clean.go
  4. 1 1
      session/session.go
  5. 70 24
      session/session_test.go

+ 7 - 3
main.go

@@ -31,13 +31,17 @@ func main() {
 
 			w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 			w.Header().Set("Content-Type", "text/html")
-			w.Write([]byte(`
+			if _, err := w.Write([]byte(`
 				<div>Hello World!</div>
 				<div>Counter: ` + fmt.Sprintf("%d", counter) + `</div>
-			`))
+			`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`<div>Error 404!</div>`))
+			if _, err := w.Write([]byte(`<div>Error 404!</div>`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	})
 

+ 1 - 1
session/bool.go

@@ -19,7 +19,7 @@ func (this *Session) GetBool(name string, def bool) bool {
 func (this *Session) SetBool(name string, value bool) {
 	isset := this.IsSetBool(name)
 	this.v.Bool[name] = value
-	if isset || value != false {
+	if isset || value {
 		this.c = true
 	}
 }

+ 1 - 1
session/clean.go

@@ -13,7 +13,7 @@ func Clean(tmpdir string) error {
 	}
 	for _, file := range files {
 		if len(file.Name()) == 40 {
-			if diff := time.Now().Sub(file.ModTime()); diff > 24*time.Hour {
+			if diff := time.Since(file.ModTime()); diff > 24*time.Hour {
 				err = os.Remove(tmpdir + string(os.PathSeparator) + file.Name())
 				if err != nil {
 					return err

+ 1 - 1
session/session.go

@@ -45,7 +45,7 @@ func New(w http.ResponseWriter, r *http.Request, tmpdir string) *Session {
 
 			// Update file last modify time if needs
 			if info, err := os.Stat(fname); err == nil {
-				if time.Now().Sub(info.ModTime()) > 30*time.Minute {
+				if time.Since(info.ModTime()) > 30*time.Minute {
 					_ = os.Chtimes(fname, time.Now(), time.Now())
 				}
 			}

+ 70 - 24
session/session_test.go

@@ -23,10 +23,14 @@ func TestSessionBool(t *testing.T) {
 		defer sess.Close()
 		if r.URL.Path == "/set" {
 			sess.SetBool("some_bool", true)
-			w.Write([]byte(`ok`))
+			if _, err := w.Write([]byte(`ok`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "ok" {
@@ -49,10 +53,14 @@ func TestSessionBool(t *testing.T) {
 		sess := New(w, r, "./../tmp")
 		defer sess.Close()
 		if r.URL.Path == "/get" {
-			w.Write([]byte(fmt.Sprintf("%v", sess.GetBool("some_bool", false))))
+			if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.GetBool("some_bool", false)))); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "true" {
@@ -73,10 +81,14 @@ func TestSessionInt(t *testing.T) {
 		defer sess.Close()
 		if r.URL.Path == "/set" {
 			sess.SetInt("some_int", 5)
-			w.Write([]byte(`ok`))
+			if _, err := w.Write([]byte(`ok`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "ok" {
@@ -99,10 +111,14 @@ func TestSessionInt(t *testing.T) {
 		sess := New(w, r, "./../tmp")
 		defer sess.Close()
 		if r.URL.Path == "/get" {
-			w.Write([]byte(fmt.Sprintf("%d", sess.GetInt("some_int", 0))))
+			if _, err := w.Write([]byte(fmt.Sprintf("%d", sess.GetInt("some_int", 0)))); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "5" {
@@ -123,10 +139,14 @@ func TestSessionString(t *testing.T) {
 		defer sess.Close()
 		if r.URL.Path == "/set" {
 			sess.SetString("some_str", "test")
-			w.Write([]byte(`ok`))
+			if _, err := w.Write([]byte(`ok`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "ok" {
@@ -149,10 +169,14 @@ func TestSessionString(t *testing.T) {
 		sess := New(w, r, "./../tmp")
 		defer sess.Close()
 		if r.URL.Path == "/get" {
-			w.Write([]byte(fmt.Sprintf("%s", sess.GetString("some_str", ""))))
+			if _, err := w.Write([]byte(sess.GetString("some_str", ""))); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "test" {
@@ -192,10 +216,14 @@ func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
 			sess.SetBool("some_bool", false)
 			sess.SetInt("some_int", 0)
 			sess.SetString("some_str", "")
-			w.Write([]byte(`ok`))
+			if _, err := w.Write([]byte(`ok`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "ok" {
@@ -222,15 +250,19 @@ func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
 		sess := New(w, r, "./../tmp")
 		defer sess.Close()
 		if r.URL.Path == "/get" {
-			w.Write([]byte(fmt.Sprintf(
+			if _, err := w.Write([]byte(fmt.Sprintf(
 				"(%v)(%v)(%v)",
 				sess.GetBool("some_bool", false),
 				sess.GetInt("some_int", 0),
 				sess.GetString("some_str", ""),
-			)))
+			))); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "(false)(0)()" {
@@ -258,10 +290,14 @@ func TestSessionDestroy(t *testing.T) {
 		defer sess.Close()
 		if r.URL.Path == "/set" {
 			sess.SetInt("some_var", 1)
-			w.Write([]byte(`ok`))
+			if _, err := w.Write([]byte(`ok`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "ok" {
@@ -288,10 +324,14 @@ func TestSessionDestroy(t *testing.T) {
 		sess := New(w, r, "./../tmp")
 		defer sess.Close()
 		if r.URL.Path == "/get" {
-			w.Write([]byte(fmt.Sprintf("%v", sess.GetInt("some_var", 0))))
+			if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.GetInt("some_var", 0)))); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "1" {
@@ -312,13 +352,19 @@ func TestSessionDestroy(t *testing.T) {
 			sess.SetInt("some_var", 2)
 			err := sess.Destroy()
 			if err == nil {
-				w.Write([]byte(`OK`))
+				if _, err := w.Write([]byte(`OK`)); err != nil {
+					fmt.Printf("%s\n", err.Error())
+				}
 			} else {
-				w.Write([]byte(`ERROR`))
+				if _, err := w.Write([]byte(`ERROR`)); err != nil {
+					fmt.Printf("%s\n", err.Error())
+				}
 			}
 		} else {
 			w.WriteHeader(http.StatusNotFound)
-			w.Write([]byte(`404`))
+			if _, err := w.Write([]byte(`404`)); err != nil {
+				fmt.Printf("%s\n", err.Error())
+			}
 		}
 	}).ServeHTTP(recorder, request)
 	if recorder.Body.String() != "OK" {