Skip to content

Commit f258a21

Browse files
vvelikodnyfjl
authored andcommitted
rpc: use method constants instead of literal strings (ethereum#15652)
1 parent fd777bb commit f258a21

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

rpc/http.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (hc *httpConn) Close() error {
6767

6868
// DialHTTP creates a new RPC clients that connection to an RPC server over HTTP.
6969
func DialHTTP(endpoint string) (*Client, error) {
70-
req, err := http.NewRequest("POST", endpoint, nil)
70+
req, err := http.NewRequest(http.MethodPost, endpoint, nil)
7171
if err != nil {
7272
return nil, err
7373
}
@@ -149,7 +149,7 @@ func NewHTTPServer(cors []string, srv *Server) *http.Server {
149149
// ServeHTTP serves JSON-RPC requests over HTTP.
150150
func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
151151
// Permit dumb empty requests for remote health-checks (AWS)
152-
if r.Method == "GET" && r.ContentLength == 0 && r.URL.RawQuery == "" {
152+
if r.Method == http.MethodGet && r.ContentLength == 0 && r.URL.RawQuery == "" {
153153
return
154154
}
155155
if code, err := validateRequest(r); err != nil {
@@ -169,7 +169,7 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
169169
// validateRequest returns a non-zero response code and error message if the
170170
// request is invalid.
171171
func validateRequest(r *http.Request) (int, error) {
172-
if r.Method == "PUT" || r.Method == "DELETE" {
172+
if r.Method == http.MethodPut || r.Method == http.MethodDelete {
173173
return http.StatusMethodNotAllowed, errors.New("method not allowed")
174174
}
175175
if r.ContentLength > maxHTTPRequestContentLength {
@@ -192,7 +192,7 @@ func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler {
192192

193193
c := cors.New(cors.Options{
194194
AllowedOrigins: allowedOrigins,
195-
AllowedMethods: []string{"POST", "GET"},
195+
AllowedMethods: []string{http.MethodPost, http.MethodGet},
196196
MaxAge: 600,
197197
AllowedHeaders: []string{"*"},
198198
})

rpc/http_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ import (
2424
)
2525

2626
func TestHTTPErrorResponseWithDelete(t *testing.T) {
27-
testHTTPErrorResponse(t, "DELETE", contentType, "", http.StatusMethodNotAllowed)
27+
testHTTPErrorResponse(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed)
2828
}
2929

3030
func TestHTTPErrorResponseWithPut(t *testing.T) {
31-
testHTTPErrorResponse(t, "PUT", contentType, "", http.StatusMethodNotAllowed)
31+
testHTTPErrorResponse(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed)
3232
}
3333

3434
func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
3535
body := make([]rune, maxHTTPRequestContentLength+1)
3636
testHTTPErrorResponse(t,
37-
"POST", contentType, string(body), http.StatusRequestEntityTooLarge)
37+
http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge)
3838
}
3939

4040
func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
41-
testHTTPErrorResponse(t, "POST", "", "", http.StatusUnsupportedMediaType)
41+
testHTTPErrorResponse(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType)
4242
}
4343

4444
func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
45-
testHTTPErrorResponse(t, "POST", contentType, "", 0)
45+
testHTTPErrorResponse(t, http.MethodPost, contentType, "", 0)
4646
}
4747

4848
func testHTTPErrorResponse(t *testing.T, method, contentType, body string, expected int) {

0 commit comments

Comments
 (0)