mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
proxy: add readonly handler
This commit is contained in:
parent
9cc114df36
commit
fd1f46313a
@ -31,3 +31,19 @@ func NewHandler(endpoints []string) (http.Handler, error) {
|
|||||||
|
|
||||||
return &rp, nil
|
return &rp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readonlyHandlerFunc(next http.Handler) func(http.ResponseWriter, *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if req.Method != "GET" {
|
||||||
|
w.WriteHeader(http.StatusNotImplemented)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(w, req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReadonlyHandler(hdlr http.Handler) http.Handler {
|
||||||
|
readonly := readonlyHandlerFunc(hdlr)
|
||||||
|
return http.HandlerFunc(readonly)
|
||||||
|
}
|
||||||
|
39
proxy/proxy_test.go
Normal file
39
proxy/proxy_test.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestReadonlyHandler(t *testing.T) {
|
||||||
|
fixture := func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
hdlrFunc := readonlyHandlerFunc(http.HandlerFunc(fixture))
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
method string
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
// GET is only passing method
|
||||||
|
{"GET", http.StatusOK},
|
||||||
|
|
||||||
|
// everything but GET is StatusNotImplemented
|
||||||
|
{"POST", http.StatusNotImplemented},
|
||||||
|
{"PUT", http.StatusNotImplemented},
|
||||||
|
{"PATCH", http.StatusNotImplemented},
|
||||||
|
{"DELETE", http.StatusNotImplemented},
|
||||||
|
{"FOO", http.StatusNotImplemented},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
req, _ := http.NewRequest(tt.method, "http://example.com", nil)
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
hdlrFunc(rr, req)
|
||||||
|
|
||||||
|
if tt.want != rr.Code {
|
||||||
|
t.Errorf("#%d: incorrect HTTP status code: method=%s want=%d got=%d", i, tt.method, tt.want, rr.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user