Repository: valyala/fasthttp
Pull request #567 (2019-04-30 11:11:05)

Added methods constants
It can little bit reduce compile time and help avoid typos.


Patch:
diff --git a/methods.go b/methods.go
new file mode 100644
index 0000000..a614584
--- /dev/null
+++ b/methods.go
@@ -0,0 +1,14 @@
+package fasthttp
+
+// HTTP methods were copied from net/http.
+const (
+	MethodGet     = "GET"     // RFC 7231, 4.3.1
+	MethodHead    = "HEAD"    // RFC 7231, 4.3.2
+	MethodPost    = "POST"    // RFC 7231, 4.3.3
+	MethodPut     = "PUT"     // RFC 7231, 4.3.4
+	MethodPatch   = "PATCH"   // RFC 5789
+	MethodDelete  = "DELETE"  // RFC 7231, 4.3.5
+	MethodConnect = "CONNECT" // RFC 7231, 4.3.6
+	MethodOptions = "OPTIONS" // RFC 7231, 4.3.7
+	MethodTrace   = "TRACE"   // RFC 7231, 4.3.8
+)
diff --git a/strings.go b/strings.go
index 6d83231..17e5602 100644
--- a/strings.go
+++ b/strings.go
@@ -22,15 +22,15 @@ var (
 
 	strResponseContinue = []byte("HTTP/1.1 100 Continue\r\n\r\n")
 
-	strGet     = []byte("GET")
-	strHead    = []byte("HEAD")
-	strPost    = []byte("POST")
-	strPut     = []byte("PUT")
-	strDelete  = []byte("DELETE")
-	strConnect = []byte("CONNECT")
-	strOptions = []byte("OPTIONS")
-	strTrace   = []byte("TRACE")
-	strPatch   = []byte("PATCH")
+	strGet     = []byte(MethodGet)
+	strHead    = []byte(MethodHead)
+	strPost    = []byte(MethodPost)
+	strPut     = []byte(MethodPut)
+	strDelete  = []byte(MethodDelete)
+	strConnect = []byte(MethodConnect)
+	strOptions = []byte(MethodOptions)
+	strTrace   = []byte(MethodTrace)
+	strPatch   = []byte(MethodPatch)
 
 	strExpect           = []byte("Expect")
 	strConnection       = []byte("Connection")


Test patch:
diff --git a/client_test.go b/client_test.go
index 86eaca2..d602b7d 100644
--- a/client_test.go
+++ b/client_test.go
@@ -32,7 +32,7 @@ func TestClientNilResp(t *testing.T) {
 		},
 	}
 	req := AcquireRequest()
-	req.Header.SetMethod("GET")
+	req.Header.SetMethod(MethodGet)
 	req.SetRequestURI("http://example.com")
 	if err := c.Do(req, nil); err != nil {
 		t.Fatal(err)
@@ -99,7 +99,7 @@ func TestClientPostArgs(t *testing.T) {
 	args := req.PostArgs()
 	args.Add("addhttp2", "support")
 	args.Add("fast", "http")
-	req.Header.SetMethod("POST")
+	req.Header.SetMethod(MethodPost)
 	req.SetRequestURI("http://make.fasthttp.great?again")
 	err := c.Do(req, res)
 	if err != nil {
@@ -490,8 +490,8 @@ func TestClientDoWithCustomHeaders(t *testing.T) {
 			ch <- fmt.Errorf("cannot read client request: %s", err)
 			return
 		}
-		if string(req.Header.Method()) != "POST" {
-			ch <- fmt.Errorf("unexpected request method: %q. Expecting %q", req.Header.Method(), "POST")
+		if string(req.Header.Method()) != MethodPost {
+			ch <- fmt.Errorf("unexpected request method: %q. Expecting %q", req.Header.Method(), MethodPost)
 			return
 		}
 		reqURI := req.RequestURI()
@@ -532,7 +532,7 @@ func TestClientDoWithCustomHeaders(t *testing.T) {
 	}()
 
 	var req Request
-	req.Header.SetMethod("POST")
+	req.Header.SetMethod(MethodPost)
 	req.SetRequestURI(uri)
 	for k, v := range headers {
 		req.Header.Set(k, v)
@@ -847,7 +847,7 @@ func TestHostClientMaxConnsWithDeadline(t *testing.T) {
 
 			req := AcquireRequest()
 			req.SetRequestURI("http://foobar/baz")
-			req.Header.SetMethod("POST")
+			req.Header.SetMethod(MethodPost)
 			req.SetBodyString("bar")
 			resp := AcquireResponse()
 
diff --git a/client_timing_test.go b/client_timing_test.go
index 2a5363a..8cc72a2 100644
--- a/client_timing_test.go
+++ b/client_timing_test.go
@@ -157,7 +157,7 @@ func BenchmarkNetHTTPClientDoFastServer(b *testing.B) {
 
 	nn := uint32(0)
 	b.RunParallel(func(pb *testing.PB) {
-		req, err := http.NewRequest("GET", fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1)), nil)
+		req, err := http.NewRequest(MethodGet, fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1)), nil)
 		if err != nil {
 			b.Fatalf("unexpected error: %s", err)
 		}
@@ -542,7 +542,7 @@ func benchmarkNetHTTPClientEndToEndBigResponseInmemory(b *testing.B, parallelism
 	url := "http://unused.host" + requestURI
 	b.SetParallelism(parallelism)
 	b.RunParallel(func(pb *testing.PB) {
-		req, err := http.NewRequest("GET", url, nil)
+		req, err := http.NewRequest(MethodGet, url, nil)
 		if err != nil {
 			b.Fatalf("unexpected error: %s", err)
 		}
diff --git a/fasthttpadaptor/adaptor_test.go b/fasthttpadaptor/adaptor_test.go
index 801f21f..96a1093 100644
--- a/fasthttpadaptor/adaptor_test.go
+++ b/fasthttpadaptor/adaptor_test.go
@@ -13,7 +13,7 @@ import (
 )
 
 func TestNewFastHTTPHandler(t *testing.T) {
-	expectedMethod := "POST"
+	expectedMethod := fasthttp.MethodPost
 	expectedProto := "HTTP/1.1"
 	expectedProtoMajor := 1
 	expectedProtoMinor := 1
diff --git a/fs_test.go b/fs_test.go
index cc348b5..9d56d48 100644
--- a/fs_test.go
+++ b/fs_test.go
@@ -101,7 +101,7 @@ func TestPathNotFoundFunc(t *testing.T) {
 func TestServeFileHead(t *testing.T) {
 	var ctx RequestCtx
 	var req Request
-	req.Header.SetMethod("HEAD")
+	req.Header.SetMethod(MethodHead)
 	req.SetRequestURI("http://foobar.com/baz")
 	ctx.Init(&req, nil, nil)
 
@@ -686,7 +686,7 @@ func testFileExtension(t *testing.T, path string, compressed bool, compressedFil
 func TestServeFileContentType(t *testing.T) {
 	var ctx RequestCtx
 	var req Request
-	req.Header.SetMethod("GET")
+	req.Header.SetMethod(MethodGet)
 	req.SetRequestURI("http://foobar.com/baz")
 	ctx.Init(&req, nil, nil)
 
diff --git a/header_regression_test.go b/header_regression_test.go
index 69a5775..ea2615c 100644
--- a/header_regression_test.go
+++ b/header_regression_test.go
@@ -41,10 +41,10 @@ func TestIssue28ResponseWithoutBodyNoContentType(t *testing.T) {
 }
 
 func TestIssue6RequestHeaderSetContentType(t *testing.T) {
-	testIssue6RequestHeaderSetContentType(t, "GET")
-	testIssue6RequestHeaderSetContentType(t, "POST")
-	testIssue6RequestHeaderSetContentType(t, "PUT")
-	testIssue6RequestHeaderSetContentType(t, "PATCH")
+	testIssue6RequestHeaderSetContentType(t, MethodGet)
+	testIssue6RequestHeaderSetContentType(t, MethodPost)
+	testIssue6RequestHeaderSetContentType(t, MethodPut)
+	testIssue6RequestHeaderSetContentType(t, MethodPatch)
 }
 
 func testIssue6RequestHeaderSetContentType(t *testing.T, method string) {
@@ -77,7 +77,7 @@ func issue6VerifyRequestHeader(t *testing.T, h *RequestHeader, contentType strin
 	if string(h.Method()) != method {
 		t.Fatalf("unexpected method: %q. Expecting %q", h.Method(), method)
 	}
-	if method != "GET" {
+	if method != MethodGet {
 		if h.ContentLength() != contentLength {
 			t.Fatalf("unexpected content-length: %d. Expecting %d. method=%q", h.ContentLength(), contentLength, method)
 		}
diff --git a/header_test.go b/header_test.go
index 8dd767a..1ff9ccf 100644
--- a/header_test.go
+++ b/header_test.go
@@ -1603,10 +1603,10 @@ func TestRequestHeaderCookieIssue313(t *testing.T) {
 
 func TestRequestHeaderMethod(t *testing.T) {
 	// common http methods
-	testRequestHeaderMethod(t, "GET")
-	testRequestHeaderMethod(t, "POST")
-	testRequestHeaderMethod(t, "HEAD")
-	testRequestHeaderMethod(t, "DELETE")
+	testRequestHeaderMethod(t, MethodGet)
+	testRequestHeaderMethod(t, MethodPost)
+	testRequestHeaderMethod(t, MethodHead)
+	testRequestHeaderMethod(t, MethodDelete)
 
 	// non-http methods
 	testRequestHeaderMethod(t, "foobar")
@@ -1636,7 +1636,7 @@ func testRequestHeaderMethod(t *testing.T, expectedMethod string) {
 func TestRequestHeaderSetGet(t *testing.T) {
 	h := &RequestHeader{}
 	h.SetRequestURI("/aa/bbb")
-	h.SetMethod("POST")
+	h.SetMethod(MethodPost)
 	h.Set("foo", "bar")
 	h.Set("host", "12345")
 	h.Set("content-type", "aaa/bbb")
diff --git a/http_test.go b/http_test.go
index 653142c..cd31f87 100644
--- a/http_test.go
+++ b/http_test.go
@@ -702,7 +702,7 @@ func TestResponseSkipBody(t *testing.T) {
 func TestRequestNoContentLength(t *testing.T) {
 	var r Request
 
-	r.Header.SetMethod("HEAD")
+	r.Header.SetMethod(MethodHead)
 	r.Header.SetHost("foobar")
 
 	s := r.String()
@@ -710,7 +710,7 @@ func TestRequestNoContentLength(t *testing.T) {
 		t.Fatalf("unexpected content-length in HEAD request %q", s)
 	}
 
-	r.Header.SetMethod("POST")
+	r.Header.SetMethod(MethodPost)
 	fmt.Fprintf(r.BodyWriter(), "foobar body")
 	s = r.String()
 	if !strings.Contains(s, "Content-Length: ") {
@@ -1253,7 +1253,7 @@ func TestSetResponseBodyStreamChunked(t *testing.T) {
 func testSetRequestBodyStream(t *testing.T, body string, chunked bool) {
 	var req Request
 	req.Header.SetHost("foobar.com")
-	req.Header.SetMethod("POST")
+	req.Header.SetMethod(MethodPost)
 
 	bodySize := len(body)
 	if chunked {
@@ -1416,22 +1416,22 @@ func testResponseReadWithoutBody(t *testing.T, resp *Response, s string, skipBod
 
 func TestRequestSuccess(t *testing.T) {
 	// empty method, user-agent and body
-	testRequestSuccess(t, "", "/foo/bar", "google.com", "", "", "GET")
+	testRequestSuccess(t, "", "/foo/bar", "google.com", "", "", MethodGet)
 
 	// non-empty user-agent
-	testRequestSuccess(t, "GET", "/foo/bar", "google.com", "MSIE", "", "GET")
+	testRequestSuccess(t, MethodGet, "/foo/bar", "google.com", "MSIE", "", MethodGet)
 
 	// non-empty method
-	testRequestSuccess(t, "HEAD", "/aaa", "fobar", "", "", "HEAD")
+	testRequestSuccess(t, MethodHead, "/aaa", "fobar", "", "", MethodHead)
 
 	// POST method with body
-	testRequestSuccess(t, "POST", "/bbb", "aaa.com", "Chrome aaa", "post body", "POST")
+	testRequestSuccess(t, MethodPost, "/bbb", "aaa.com", "Chrome aaa", "post body", MethodPost)
 
 	// PUT method with body
-	testRequestSuccess(t, "PUT", "/aa/bb", "a.com", "ome aaa", "put body", "PUT")
+	testRequestSuccess(t, MethodPut, "/aa/bb", "a.com", "ome aaa", "put body", MethodPut)
 
 	// only host is set
-	testRequestSuccess(t, "", "", "gooble.com", "", "", "GET")
+	testRequestSuccess(t, "", "", "gooble.com", "", "", MethodGet)
 }
 
 func TestResponseSuccess(t *testing.T) {
@@ -1501,7 +1501,7 @@ func TestRequestWriteError(t *testing.T) {
 	testRequestWriteError(t, "", "/foo/bar", "", "", "")
 
 	// get with body
-	testRequestWriteError(t, "GET", "/foo/bar", "aaa.com", "", "foobar")
+	testRequestWriteError(t, MethodGet, "/foo/bar", "aaa.com", "", "foobar")
 }
 
 func testRequestWriteError(t *testing.T, method, requestURI, host, userAgent, body string) {
@@ -1531,7 +1531,7 @@ func testRequestSuccess(t *testing.T, method, requestURI, host, userAgent, body,
 	req.SetBody([]byte(body))
 
 	contentType := "foobar"
-	if method == "POST" {
+	if method == MethodPost {
 		req.Header.Set("Content-Type", contentType)
 	}
 
@@ -1569,7 +1569,7 @@ func testRequestSuccess(t *testing.T, method, requestURI, host, userAgent, body,
 		t.Fatalf("Unexpected body: %q. Expected %q", req1.Body(), body)
 	}
 
-	if method == "POST" && string(req1.Header.Peek("Content-Type")) != contentType {
+	if method == MethodPost && string(req1.Header.Peek("Content-Type")) != contentType {
 		t.Fatalf("Unexpected content-type: %q. Expected %q", req1.Header.Peek("Content-Type"), contentType)
 	}
 }
@@ -1892,34 +1892,32 @@ Content-Type: application/json
 	}
 }
 
-
 func TestResponseRawBodySet(t *testing.T) {
 	var resp Response
-	
+
 	expectedS := "test"
 	body := []byte(expectedS)
 	resp.SetBodyRaw(body)
-	
+
 	testBodyWriteTo(t, &resp, expectedS, true)
 }
 
 func TestResponseRawBodyReset(t *testing.T) {
 	var resp Response
-	
+
 	body := []byte("test")
 	resp.SetBodyRaw(body)
 	resp.ResetBody()
-	
+
 	testBodyWriteTo(t, &resp, "", true)
 }
 
 func TestResponseRawBodyCopyTo(t *testing.T) {
 	var resp Response
-	
+
 	expectedS := "test"
 	body := []byte(expectedS)
 	resp.SetBodyRaw(body)
-	
+
 	testResponseCopyTo(t, &resp)
 }
-
diff --git a/server_timing_test.go b/server_timing_test.go
index 5c935e8..e4318a7 100644
--- a/server_timing_test.go
+++ b/server_timing_test.go
@@ -347,7 +347,7 @@ func benchmarkNetHTTPServerGet(b *testing.B, clientsCount, requestsPerConn int)
 	ch := make(chan struct{}, b.N)
 	s := &http.Server{
 		Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
-			if req.Method != "GET" {
+			if req.Method != MethodGet {
 				b.Fatalf("Unexpected request method: %s", req.Method)
 			}
 			h := w.Header()
@@ -390,7 +390,7 @@ func benchmarkNetHTTPServerPost(b *testing.B, clientsCount, requestsPerConn int)
 	ch := make(chan struct{}, b.N)
 	s := &http.Server{
 		Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
-			if req.Method != "POST" {
+			if req.Method != MethodPost {
 				b.Fatalf("Unexpected request method: %s", req.Method)
 			}
 			body, err := ioutil.ReadAll(req.Body)