aboutsummaryrefslogtreecommitdiff
path: root/http_test.go
diff options
context:
space:
mode:
authorGravatar Sergey Ponomarev <stokito@gmail.com> 2021-11-08 14:09:35 +0200
committerGravatar GitHub <noreply@github.com> 2021-11-08 13:09:35 +0100
commit8febad07979a5ee428f4b0747581c82af903737e (patch)
treee5dce029486d478e47fc29103b8b84539e8ad525 /http_test.go
parentfix: Status Line parsing and writing (#1135) (diff)
downloadfasthttp-8febad07979a5ee428f4b0747581c82af903737e.tar.gz
fasthttp-8febad07979a5ee428f4b0747581c82af903737e.tar.bz2
fasthttp-8febad07979a5ee428f4b0747581c82af903737e.zip
http.go: Request.SetURI() (Fix #1141) (#1148)
Currently, the only way to set URI for a request is to call SetRequestURI(string). Then when a request performed the string will be parsed into a fasthttp.URI struct. If there are many requests with the same URI then we'll waste CPU for a parsing of the same URI string. With the new SetURI(*URI) method we can once parse a URI string into a fasthttp.URI struct and then reuse it for many requests. Unfortunately the URI will be copied because may be modified inside the request. But anyway this will be more lightweight than parsing.
Diffstat (limited to 'http_test.go')
-rw-r--r--http_test.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/http_test.go b/http_test.go
index 00ce8a4..8bd050b 100644
--- a/http_test.go
+++ b/http_test.go
@@ -515,6 +515,31 @@ tailfoobar`
}
}
+func TestRequestSetURI(t *testing.T) {
+ t.Parallel()
+
+ var r Request
+
+ uri := "/foo/bar?baz"
+ u := &URI{}
+ u.Parse(nil, []byte(uri)) //nolint:errcheck
+ // Set request uri via SetURI()
+ r.SetURI(u) // copies URI
+ // modifying an original URI struct doesn't affect stored URI inside of request
+ u.SetPath("newPath")
+ if string(r.RequestURI()) != uri {
+ t.Fatalf("unexpected request uri %q. Expecting %q", r.RequestURI(), uri)
+ }
+
+ // Set request uri to nil just resets the URI
+ r.Reset()
+ uri = "/"
+ r.SetURI(nil)
+ if string(r.RequestURI()) != uri {
+ t.Fatalf("unexpected request uri %q. Expecting %q", r.RequestURI(), uri)
+ }
+}
+
func TestRequestRequestURI(t *testing.T) {
t.Parallel()