From 2feb5f467c7e23245f50bfd911e1eb8ee7c65427 Mon Sep 17 00:00:00 2001 From: nanmu42 Date: Sat, 18 Sep 2021 14:55:23 +0800 Subject: [PATCH] feature: add example for http status 204 and 304 --- examples/gin/gin.go | 8 ++++++++ examples/http/http.go | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/examples/gin/gin.go b/examples/gin/gin.go index 66f8295..632a45a 100644 --- a/examples/gin/gin.go +++ b/examples/gin/gin.go @@ -41,6 +41,14 @@ func main() { }) }) + g.GET("/204", func(c *gin.Context) { + c.Status(http.StatusNoContent) + }) + + g.GET("/304", func(c *gin.Context) { + c.Status(http.StatusNotModified) + }) + const port = 3000 log.Printf("Service is litsenning on port %d...", port) diff --git a/examples/http/http.go b/examples/http/http.go index 240cca0..8dc2459 100644 --- a/examples/http/http.go +++ b/examples/http/http.go @@ -22,6 +22,24 @@ func main() { writeString(w, fmt.Sprintf("This content is compressed: l%sng!", strings.Repeat("o", 1000))) }) + mux.HandleFunc("/204", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNoContent) + }) + + mux.HandleFunc("/wrong204", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNoContent) + writeString(w, fmt.Sprintf("This content is compressed: l%sng!", strings.Repeat("o", 1000))) + }) + + mux.HandleFunc("/304", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotModified) + }) + + mux.HandleFunc("/wrong304", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotModified) + writeString(w, fmt.Sprintf("This content is compressed: l%sng!", strings.Repeat("o", 1000))) + }) + const port = 3001 log.Printf("Service is litsenning on port %d...", port) @@ -30,5 +48,8 @@ func main() { func writeString(w http.ResponseWriter, payload string) { w.Header().Set("Content-Type", "text/plain; charset=utf8") - _, _ = io.WriteString(w, payload+"\n") + _, err := io.WriteString(w, payload+"\n") + if err != nil { + fmt.Printf("wrting body: %s\n", err) + } }