This commit is contained in:
Gerasimos Maropoulos 2016-07-21 20:33:00 +03:00
parent 6b71452222
commit 85a2e98ba4
3 changed files with 52 additions and 21 deletions

View File

@ -327,23 +327,6 @@ func (ctx *Context) Subdomain() (subdomain string) {
return return
} }
// URLEncode returns the path encoded as url
// useful when you want to pass something to a database and be valid to retrieve it via context.Param
// use it only for special cases, when the default behavior doesn't suits you.
//
// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
/* Credits to Manish Singh @kryptodev for URLEncode by post issue share code */
func URLEncode(path string) string {
if path == "" {
return ""
}
u := fasthttp.AcquireURI()
u.SetPath(path)
encodedPath := u.String()[8:]
fasthttp.ReleaseURI(u)
return encodedPath
}
// ReadJSON reads JSON from request's body // ReadJSON reads JSON from request's body
func (ctx *Context) ReadJSON(jsonObject interface{}) error { func (ctx *Context) ReadJSON(jsonObject interface{}) error {
data := ctx.RequestCtx.Request.Body() data := ctx.RequestCtx.Request.Body()

View File

@ -10,7 +10,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strconv"
"testing" "testing"
"time" "time"
@ -243,12 +242,13 @@ func TestMultiRunningServers_v2(t *testing.T) {
} }
const ( const (
testEnableSubdomain = false testEnableSubdomain = true
testSubdomain = "mysubdomain.com" testSubdomain = "mysubdomain"
) )
func testSubdomainHost() string { func testSubdomainHost() string {
return testSubdomain + strconv.Itoa(Servers.Main().Port()) s := testSubdomain + "." + Servers.Main().Host()
return s
} }
func testSubdomainURL() (subdomainURL string) { func testSubdomainURL() (subdomainURL string) {
@ -433,6 +433,20 @@ func TestMuxPathEscape(t *testing.T) {
Expect().Status(StatusOK).Body().Equal("name=Sakamoto desu ga,highlight=text") Expect().Status(StatusOK).Body().Equal("name=Sakamoto desu ga,highlight=text")
} }
func TestMuxEncodeURL(t *testing.T) {
initDefault()
Get("/encoding/:url", func(ctx *Context) {
url := URLEncode(ctx.Param("url"))
ctx.SetStatusCode(StatusOK)
ctx.Write(url)
})
e := Tester(t)
e.GET("/encoding/http%3A%2F%2Fsome-url.com").Expect().Status(StatusOK).Body().Equal("http://some-url.com")
}
func TestMuxCustomErrors(t *testing.T) { func TestMuxCustomErrors(t *testing.T) {
var ( var (
notFoundMessage = "Iris custom message for 404 not found" notFoundMessage = "Iris custom message for 404 not found"

34
iris.go
View File

@ -54,6 +54,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/url"
"os" "os"
"path" "path"
"reflect" "reflect"
@ -753,6 +754,39 @@ func (s *Framework) Path(routeName string, args ...interface{}) string {
return fmt.Sprintf(r.formattedPath, arguments...) return fmt.Sprintf(r.formattedPath, arguments...)
} }
// URLEncode returns the path encoded as url
// useful when you want to pass something to a database and be valid to retrieve it via context.Param
// use it only for special cases, when the default behavior doesn't suits you.
//
// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
// it uses just the url.QueryUnescape
func URLEncode(path string) string {
if path == "" {
return ""
}
encodedPath, _ := url.QueryUnescape(path)
return encodedPath
}
// URLEncode returns the path encoded as url
// useful when you want to pass something to a database and be valid to retrieve it via context.Param
// use it only for special cases, when the default behavior doesn't suits you.
//
// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
/* Credits to Manish Singh @kryptodev for URLEncode by post issue share code */
// simple things, if URLEncode doesn't gives you the results you waited, use this function
// I know it is not the best way to describe it, but I don't think you will ever need this, it is here for ANY CASE
func URLEncodeFasthttp(path string) string {
if path == "" {
return ""
}
u := fasthttp.AcquireURI()
u.SetPath(path)
encodedPath := u.String()[8:]
fasthttp.ReleaseURI(u)
return encodedPath
}
// URL returns the subdomain+ host + Path(...optional named parameters if route is dynamic) // URL returns the subdomain+ host + Path(...optional named parameters if route is dynamic)
// returns an empty string if parse is failed // returns an empty string if parse is failed
func URL(routeName string, args ...interface{}) (url string) { func URL(routeName string, args ...interface{}) (url string) {