Skip to content

Commit

Permalink
Added methods constants (#567)
Browse files Browse the repository at this point in the history
* ✨ Added methods constants

* 👌 Fixed methods comment due to review changes
  • Loading branch information
Maxim Lebedev authored and erikdubbelboer committed May 6, 2019
1 parent 15dbe35 commit f544170
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 52 deletions.
12 changes: 6 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions client_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion fasthttpadaptor/adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestNewFastHTTPHandler(t *testing.T) {
expectedMethod := "POST"
expectedMethod := fasthttp.MethodPost
expectedProto := "HTTP/1.1"
expectedProtoMajor := 1
expectedProtoMinor := 1
Expand Down
4 changes: 2 additions & 2 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions header_regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
38 changes: 18 additions & 20 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,15 +702,15 @@ 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()
if strings.Contains(s, "Content-Length: ") {
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: ") {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}

14 changes: 14 additions & 0 deletions methods.go
Original file line number Diff line number Diff line change
@@ -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
)
4 changes: 2 additions & 2 deletions server_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f544170

Please sign in to comment.