Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.85 KB | None | 0 0
  1. func TestReadonlyHandler(t *testing.T) {
  2.     fixture := func(w http.ResponseWriter, req *http.Request) {
  3.         w.WriteHeader(http.StatusOK)
  4.     }
  5.     hdlrFunc := readonlyHandlerFunc(http.HandlerFunc(fixture))
  6.  
  7.     tests := []struct {
  8.         method string
  9.         want   int
  10.     }{
  11.         // GET is only passing method
  12.         {"GET", http.StatusOK},
  13.  
  14.         // everything but GET is StatusNotImplemented
  15.         {"POST", http.StatusNotImplemented},
  16.         {"PUT", http.StatusNotImplemented},
  17.         {"PATCH", http.StatusNotImplemented},
  18.         {"DELETE", http.StatusNotImplemented},
  19.         {"FOO", http.StatusNotImplemented},
  20.     }
  21.  
  22.     for i, tt := range tests {
  23.         req, _ := http.NewRequest(tt.method, "http://example.com", nil)
  24.         rr := httptest.NewRecorder()
  25.         hdlrFunc(rr, req)
  26.  
  27.         if tt.want != rr.Code {
  28.             t.Errorf("#%d: incorrect HTTP status code: method=%s want=%d got=%d", i, tt.method, tt.want, rr.Code)
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement