Skip to content

Commit 1879f55

Browse files
committedSep 30, 2020
added fixed HTTP status code handler
1 parent 55a68aa commit 1879f55

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
 

‎example/main.go

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"log"
78
"net/http"
89

@@ -30,3 +31,13 @@ func main() {
3031
fmt.Println("listening at http://localhost:8080")
3132
log.Fatal(http.ListenAndServe(":8080", nil))
3233
}
34+
35+
// statusCodeHandler is useful for testing the server by returning a
36+
// specific HTTP status code.
37+
// http.Handle("/", statusCodeHandler(http.StatusInternalServerError))
38+
type statusCodeHandler int
39+
40+
func (c statusCodeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
41+
w.WriteHeader(int(c))
42+
io.WriteString(w, http.StatusText(int(c)))
43+
}

0 commit comments

Comments
 (0)
Please sign in to comment.