Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for HTTP request methods other than GET (POST, PUT, PATCH and DELETE) #8

Merged
merged 2 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Flags:
-i, --interval=250ms Interval for statistics calculation (reqlog mode)
--preallocate=1000 Number of requests in req log to preallocate memory for per
connection (reqlog mode)
--method=GET The HTTP request method (GET, POST, PUT, PATCH or DELETE)
--version Show application version.

Args:
Expand Down
1 change: 1 addition & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
preallocate = kingpin.Flag("preallocate", "Number of requests in req log to preallocate memory for per connection (reqlog mode)").
Default("1000").
Int()
method = kingpin.Flag("method", "The HTTP request method (GET, POST, PUT, PATCH or DELETE)").Default("GET").Enum("GET", "POST", "PUT", "PATCH", "DELETE")
target = kingpin.Arg("target", "HTTP target URL").Required().String()
)

Expand Down
2 changes: 1 addition & 1 deletion gocannon.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func runGocannon() error {
for connectionID := 0; connectionID < n; connectionID++ {
go func(c *fasthttp.HostClient, cid int) {
for {
code, start, end := performRequest(c, *target)
code, start, end := performRequest(c, *target, *method)
if end >= stop {
break
}
Expand Down
7 changes: 3 additions & 4 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ func newHTTPClient(
timeout time.Duration,
connections int,
) (*fasthttp.HostClient, error) {
c := new(fasthttp.HostClient)
u, err := url.ParseRequestURI(target)
if err != nil {
return nil, ErrWrongTarget
}
if u.Scheme != "http" {
return nil, ErrUnsupportedProtocol
}
c = &fasthttp.HostClient{
c := &fasthttp.HostClient{
Addr: u.Host,
MaxConns: int(connections),
ReadTimeout: timeout,
Expand All @@ -40,14 +39,14 @@ func newHTTPClient(
return c, nil
}

func performRequest(c *fasthttp.HostClient, target string) (
func performRequest(c *fasthttp.HostClient, target string, method string) (
code int, start int64, end int64,
) {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()

req.URI().SetScheme("http")
req.Header.SetMethod("GET")
req.Header.SetMethod(method)
req.SetRequestURI(target)

start = makeTimestamp()
Expand Down
8 changes: 5 additions & 3 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ func TestPerformRequest(t *testing.T) {

c, _ := newHTTPClient("http://localhost:3000/", timeout, 10)

codeOk, _, _ := performRequest(c, "http://localhost:3000/")
codeISE, _, _ := performRequest(c, "http://localhost:3000/error")
codeTimeout, _, _ := performRequest(c, "http://localhost:3000/timeout")
codeOk, _, _ := performRequest(c, "http://localhost:3000/", "GET")
codePost, _, _ := performRequest(c, "http://localhost:3000/postonly", "POST")
codeISE, _, _ := performRequest(c, "http://localhost:3000/error", "GET")
codeTimeout, _, _ := performRequest(c, "http://localhost:3000/timeout", "GET")

assert.Equal(t, 200, codeOk)
assert.Equal(t, 200, codePost)
assert.Equal(t, http.StatusInternalServerError, codeISE)
assert.Equal(t, 0, codeTimeout)
}
2 changes: 1 addition & 1 deletion integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGocannon(t *testing.T) {
for connectionID := 0; connectionID < conns; connectionID++ {
go func(c *fasthttp.HostClient, cid int) {
for {
code, start, end := performRequest(c, target)
code, start, end := performRequest(c, target, "GET")
if end >= stop {
break
}
Expand Down
9 changes: 9 additions & 0 deletions target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ func TestMain(m *testing.M) {
fmt.Fprintf(w, "Hello")
})

http.HandleFunc("/postonly", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Wrong method")
} else {
fmt.Fprintf(w, "Ok")
}
})

http.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Oooops...")
Expand Down