Skip to content
Snippets Groups Projects
Commit fac33456 authored by John McCabe's avatar John McCabe Committed by Alex Ellis
Browse files

Use http package consts for http methods


This commit replaces occurences of http method strings with the
corresponding consts from the http package.

*Note* UPDATE is not strictly speaking a valid method and as such isn't
part of the http package (should be a PUT or PATCH?)

Signed-off-by: default avatarJohn McCabe <john@johnmccabe.net>
parent 0c7e59fe
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,7 @@ type CORSHandler struct { ...@@ -11,7 +11,7 @@ type CORSHandler struct {
func (c CORSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (c CORSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// https://raw.githubusercontent.com/openfaas/store/master/store.json // https://raw.githubusercontent.com/openfaas/store/master/store.json
w.Header().Set("Access-Control-Allow-Headers", "Content-Type") w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "GET") w.Header().Set("Access-Control-Allow-Methods", http.MethodGet)
w.Header().Set("Access-Control-Allow-Origin", c.AllowedHost) w.Header().Set("Access-Control-Allow-Origin", c.AllowedHost)
(*c.Upstream).ServeHTTP(w, r) (*c.Upstream).ServeHTTP(w, r)
......
...@@ -94,15 +94,15 @@ func main() { ...@@ -94,15 +94,15 @@ func main() {
r.HandleFunc("/system/alert", faasHandlers.Alert) r.HandleFunc("/system/alert", faasHandlers.Alert)
r.HandleFunc("/system/function/{name:[-a-zA-Z_0-9]+}", queryFunction).Methods("GET") r.HandleFunc("/system/function/{name:[-a-zA-Z_0-9]+}", queryFunction).Methods(http.MethodGet)
r.HandleFunc("/system/functions", listFunctions).Methods("GET") r.HandleFunc("/system/functions", listFunctions).Methods(http.MethodGet)
r.HandleFunc("/system/functions", faasHandlers.DeployFunction).Methods("POST") r.HandleFunc("/system/functions", faasHandlers.DeployFunction).Methods(http.MethodPost)
r.HandleFunc("/system/functions", faasHandlers.DeleteFunction).Methods("DELETE") r.HandleFunc("/system/functions", faasHandlers.DeleteFunction).Methods(http.MethodDelete)
r.HandleFunc("/system/functions", faasHandlers.UpdateFunction).Methods("PUT") r.HandleFunc("/system/functions", faasHandlers.UpdateFunction).Methods(http.MethodPut)
if faasHandlers.QueuedProxy != nil { if faasHandlers.QueuedProxy != nil {
r.HandleFunc("/async-function/{name:[-a-zA-Z_0-9]+}/", faasHandlers.QueuedProxy).Methods("POST") r.HandleFunc("/async-function/{name:[-a-zA-Z_0-9]+}/", faasHandlers.QueuedProxy).Methods(http.MethodPost)
r.HandleFunc("/async-function/{name:[-a-zA-Z_0-9]+}", faasHandlers.QueuedProxy).Methods("POST") r.HandleFunc("/async-function/{name:[-a-zA-Z_0-9]+}", faasHandlers.QueuedProxy).Methods(http.MethodPost)
r.HandleFunc("/system/async-report", faasHandlers.AsyncReport) r.HandleFunc("/system/async-report", faasHandlers.AsyncReport)
} }
...@@ -113,13 +113,13 @@ func main() { ...@@ -113,13 +113,13 @@ func main() {
allowedCORSHost := "raw.githubusercontent.com" allowedCORSHost := "raw.githubusercontent.com"
fsCORS := handlers.DecorateWithCORS(fs, allowedCORSHost) fsCORS := handlers.DecorateWithCORS(fs, allowedCORSHost)
r.PathPrefix("/ui/").Handler(http.StripPrefix("/ui", fsCORS)).Methods("GET") r.PathPrefix("/ui/").Handler(http.StripPrefix("/ui", fsCORS)).Methods(http.MethodGet)
r.HandleFunc("/", faasHandlers.RoutelessProxy).Methods("POST") r.HandleFunc("/", faasHandlers.RoutelessProxy).Methods(http.MethodPost)
metricsHandler := metrics.PrometheusHandler() metricsHandler := metrics.PrometheusHandler()
r.Handle("/metrics", metricsHandler) r.Handle("/metrics", metricsHandler)
r.Handle("/", http.RedirectHandler("/ui/", http.StatusMovedPermanently)).Methods("GET") r.Handle("/", http.RedirectHandler("/ui/", http.StatusMovedPermanently)).Methods(http.MethodGet)
tcpPort := 8080 tcpPort := 8080
......
...@@ -29,8 +29,8 @@ func Test_HeadersAdded(t *testing.T) { ...@@ -29,8 +29,8 @@ func Test_HeadersAdded(t *testing.T) {
} }
actualMethods := rr.Header().Get("Access-Control-Allow-Methods") actualMethods := rr.Header().Get("Access-Control-Allow-Methods")
if actualMethods != "GET" { if actualMethods != http.MethodGet {
t.Errorf("Access-Control-Allow-Methods: want: %s got: %s", "GET", actualMethods) t.Errorf("Access-Control-Allow-Methods: want: %s got: %s", http.MethodGet, actualMethods)
} }
} }
...@@ -277,7 +277,7 @@ func removeLockFile() error { ...@@ -277,7 +277,7 @@ func removeLockFile() error {
func makeHealthHandler() func(http.ResponseWriter, *http.Request) { func makeHealthHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case "GET": case http.MethodGet:
if lockFilePresent() == false { if lockFilePresent() == false {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
...@@ -298,11 +298,11 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http. ...@@ -298,11 +298,11 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case case
"POST", http.MethodPost,
"PUT", http.MethodPut,
"DELETE", http.MethodDelete,
"UPDATE", "UPDATE",
"GET": http.MethodGet:
pipeRequest(config, w, r, r.Method) pipeRequest(config, w, r, r.Method)
break break
default: default:
......
...@@ -29,7 +29,7 @@ func TestHandler_HasCustomHeaderInFunction_WithCgi_Mode(t *testing.T) { ...@@ -29,7 +29,7 @@ func TestHandler_HasCustomHeaderInFunction_WithCgi_Mode(t *testing.T) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
body := "" body := ""
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBufferString(body))
req.Header.Add("custom-header", "value") req.Header.Add("custom-header", "value")
if err != nil { if err != nil {
...@@ -68,7 +68,7 @@ func TestHandler_HasCustomHeaderInFunction_WithCgiMode_AndBody(t *testing.T) { ...@@ -68,7 +68,7 @@ func TestHandler_HasCustomHeaderInFunction_WithCgiMode_AndBody(t *testing.T) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
body := "test" body := "test"
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBufferString(body))
req.Header.Add("custom-header", "value") req.Header.Add("custom-header", "value")
if err != nil { if err != nil {
...@@ -110,7 +110,7 @@ func TestHandler_StderrWritesToStderr_CombinedOutput_False(t *testing.T) { ...@@ -110,7 +110,7 @@ func TestHandler_StderrWritesToStderr_CombinedOutput_False(t *testing.T) {
log.SetOutput(b) log.SetOutput(b)
body := "" body := ""
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBufferString(body))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -151,7 +151,7 @@ func TestHandler_StderrWritesToResponse_CombinedOutput_True(t *testing.T) { ...@@ -151,7 +151,7 @@ func TestHandler_StderrWritesToResponse_CombinedOutput_True(t *testing.T) {
log.SetOutput(b) log.SetOutput(b)
body := "" body := ""
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBufferString(body))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -200,7 +200,7 @@ func TestHandler_DoesntHaveCustomHeaderInFunction_WithoutCgi_Mode(t *testing.T) ...@@ -200,7 +200,7 @@ func TestHandler_DoesntHaveCustomHeaderInFunction_WithoutCgi_Mode(t *testing.T)
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
body := "" body := ""
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBufferString(body))
req.Header.Add("custom-header", "value") req.Header.Add("custom-header", "value")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -236,7 +236,7 @@ func TestHandler_HasXDurationSecondsHeader(t *testing.T) { ...@@ -236,7 +236,7 @@ func TestHandler_HasXDurationSecondsHeader(t *testing.T) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
body := "hello" body := "hello"
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBufferString(body))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -262,7 +262,7 @@ func TestHandler_HasXDurationSecondsHeader(t *testing.T) { ...@@ -262,7 +262,7 @@ func TestHandler_HasXDurationSecondsHeader(t *testing.T) {
func TestHandler_RequestTimeoutFailsForExceededDuration(t *testing.T) { func TestHandler_RequestTimeoutFailsForExceededDuration(t *testing.T) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
verbs := []string{"POST"} verbs := []string{http.MethodPost}
for _, verb := range verbs { for _, verb := range verbs {
body := "hello" body := "hello"
...@@ -290,7 +290,7 @@ func TestHandler_RequestTimeoutFailsForExceededDuration(t *testing.T) { ...@@ -290,7 +290,7 @@ func TestHandler_RequestTimeoutFailsForExceededDuration(t *testing.T) {
func TestHandler_StatusOKAllowed_ForWriteableVerbs(t *testing.T) { func TestHandler_StatusOKAllowed_ForWriteableVerbs(t *testing.T) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
verbs := []string{"POST", "PUT", "UPDATE", "DELETE"} verbs := []string{http.MethodPost, http.MethodPut, "UPDATE", http.MethodDelete}
for _, verb := range verbs { for _, verb := range verbs {
body := "hello" body := "hello"
...@@ -341,7 +341,7 @@ func TestHandler_StatusMethodNotAllowed_ForUnknown(t *testing.T) { ...@@ -341,7 +341,7 @@ func TestHandler_StatusMethodNotAllowed_ForUnknown(t *testing.T) {
func TestHandler_StatusOKForGETAndNoBody(t *testing.T) { func TestHandler_StatusOKForGETAndNoBody(t *testing.T) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil) req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -370,7 +370,7 @@ func TestHealthHandler_SatusOK_LockFilePresent(t *testing.T) { ...@@ -370,7 +370,7 @@ func TestHealthHandler_SatusOK_LockFilePresent(t *testing.T) {
} }
} }
req, err := http.NewRequest("GET", "/_/health", nil) req, err := http.NewRequest(http.MethodGet, "/_/health", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -393,7 +393,7 @@ func TestHealthHandler_StatusInternalServerError_LockFileNotPresent(t *testing.T ...@@ -393,7 +393,7 @@ func TestHealthHandler_StatusInternalServerError_LockFileNotPresent(t *testing.T
} }
} }
req, err := http.NewRequest("GET", "/_/health", nil) req, err := http.NewRequest(http.MethodGet, "/_/health", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -409,7 +409,7 @@ func TestHealthHandler_StatusInternalServerError_LockFileNotPresent(t *testing.T ...@@ -409,7 +409,7 @@ func TestHealthHandler_StatusInternalServerError_LockFileNotPresent(t *testing.T
func TestHealthHandler_SatusMethoNotAllowed_ForWriteableVerbs(t *testing.T) { func TestHealthHandler_SatusMethoNotAllowed_ForWriteableVerbs(t *testing.T) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
verbs := []string{"POST", "PUT", "UPDATE", "DELETE"} verbs := []string{http.MethodPost, http.MethodPut, "UPDATE", http.MethodDelete}
for _, verb := range verbs { for _, verb := range verbs {
req, err := http.NewRequest(verb, "/_/health", nil) req, err := http.NewRequest(verb, "/_/health", nil)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment