Last active
December 24, 2018 13:15
-
-
Save mahummon/90207f32c3247eca57b002ddf00f510b to your computer and use it in GitHub Desktop.
Handle POST with Empty Body, Send 422
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## not specifying a body | |
$ curl -i -XPOST localhost:50010 | |
HTTP/1.1 422 Unprocessable Entity | |
Content-Type: text/plain; charset=utf-8 | |
X-Content-Type-Options: nosniff | |
Date: Mon, 24 Dec 2018 12:34:11 GMT | |
Content-Length: 21 | |
Unprocessable Entity | |
## specifiying an empty body | |
$ curl -i -XPOST localhost:50010 -d '' | |
HTTP/1.1 422 Unprocessable Entity | |
Content-Type: text/plain; charset=utf-8 | |
X-Content-Type-Options: nosniff | |
Date: Mon, 24 Dec 2018 12:34:21 GMT | |
Content-Length: 21 | |
Unprocessable Entity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go test -v | |
=== RUN TestBody | |
=== RUN TestBody/Send_POST_with_empty_body,_expect_422 | |
--- PASS: TestBody (0.00s) | |
--- PASS: TestBody/Send_POST_with_empty_body,_expect_422 (0.00s) | |
PASS | |
ok examples/requestBody 0.002s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
) | |
func main() { | |
server := NewServer() | |
log.Println("starting server...") | |
addr := fmt.Sprintf(":%s", "50010") | |
log.Printf("server started 0.0.0.0%s\n", addr) | |
if err := http.ListenAndServe(addr, server); err != nil { | |
log.Fatalf("could not listen on %s, %v\n", addr, err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"net/http" | |
"github.com/gorilla/mux" | |
) | |
type Server struct { | |
http.Handler | |
} | |
func handleBody(w http.ResponseWriter, r *http.Request) { | |
body, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} | |
if len(body) == 0 { | |
http.Error(w, http.StatusText(http.StatusUnprocessableEntity), | |
http.StatusUnprocessableEntity) | |
} | |
} | |
func NewServer() *Server { | |
router := mux.NewRouter() | |
router.Handle("/", http.HandlerFunc(handleBody)).Methods("POST") | |
server := Server{} | |
server.Handler = router | |
return &server | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"io" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
) | |
var ( | |
emptyBody = []byte(``) | |
) | |
func TestBody(t *testing.T) { | |
server := NewServer() | |
t.Run("Send POST with empty body, expect 422", func(t *testing.T) { | |
request := newRequest(t, http.MethodPost, "/", bytes.NewBuffer(emptyBody)) | |
response := httptest.NewRecorder() | |
server.ServeHTTP(response, request) | |
got := response.Code | |
assertResponseStatus(t, got, http.StatusUnprocessableEntity) | |
}) | |
} | |
func newRequest(t *testing.T, method, path string, r io.Reader) (req *http.Request) { | |
req, err := http.NewRequest(method, path, r) | |
if err != nil { | |
t.Fatalf("Failed to create new request: %v", err) | |
} | |
return | |
} | |
func assertResponseStatus(t *testing.T, got, expected int) { | |
if got != expected { | |
t.Errorf("wrong status code: got %v :: want %v", got, expected) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment