feat: add UrlNoQuery(), used to return url without query param

This commit is contained in:
weloe
2023-06-06 03:35:21 +08:00
parent e270c268dc
commit f5939dce57
3 changed files with 23 additions and 1 deletions

View File

@@ -253,6 +253,13 @@ func TestHttpRequest_Url(t *testing.T) {
} }
} }
func TestHttpRequest_UrlNoQuery(t *testing.T) {
s := NewTestHttpRequest(t).UrlNoQuery()
if s != "https://baidu.com/api/" {
t.Errorf("Url() = %s ,want https://baidu.com/api/", s)
}
}
func TestHttpResponse_SetHeader(t *testing.T) { func TestHttpResponse_SetHeader(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "/", nil) req, _ := http.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@@ -39,6 +39,19 @@ func (d *HttpRequest) Url() string {
return d.source.URL.String() return d.source.URL.String()
} }
func (d *HttpRequest) UrlNoQuery() string {
var scheme string
if d.source.URL.Scheme != "" {
scheme = d.source.URL.Scheme
}
if d.source.TLS != nil {
scheme = "https"
} else {
scheme = "http"
}
return scheme + "://" + d.source.Host + d.source.URL.Path
}
func (d *HttpRequest) Method() string { func (d *HttpRequest) Method() string {
return d.source.Method return d.source.Method
} }

View File

@@ -10,8 +10,10 @@ type Request interface {
Query(key string) string Query(key string) string
// Path https://example.org/ex?a=1&a=2&b=&=3&&&& Path() return /ex // Path https://example.org/ex?a=1&a=2&b=&=3&&&& Path() return /ex
Path() string Path() string
// Url https://example.org//?a=1&a=2&b=&=3&&&& Url() return https://example.org/?a=1&a=2&b=&=3&&&& // Url https://example.org/?a=1&a=2&b=&=3&&&& Url() return https://example.org/?a=1&a=2&b=&=3&&&&
Url() string Url() string
// UrlNoQuery return Url without query param
UrlNoQuery() string
// Method request method // Method request method
Method() string Method() string
// Cookie get value from cookie // Cookie get value from cookie