Skip to content

Commit 90be8e9

Browse files
committed
added URL helper methods and godoc update
1 parent aad9025 commit 90be8e9

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

essentials.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// aahframework.org/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
// Valuer interface is general purpose to `Set` and `Get` operations.
8+
type Valuer interface {
9+
Get(key string) interface{}
10+
Set(key string, value interface{})
11+
}

guid.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ func readRandomUint32() uint32 {
7777
return (uint32(b[0]) << 0) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24)
7878
}
7979

80+
// To initialize package unexported variable 'guidCounter'.
81+
// This panic would happen at program startup, so no worries at runtime panic.
8082
panic(errors.New("ess - guid: unable to generate random object id"))
8183
}
8284

@@ -97,6 +99,7 @@ func readMachineID() []byte {
9799
return id
98100
}
99101

100-
// return nil, errors.New("guid: unable to get hostname and random bytes")
102+
// To initialize package unexported variable 'machineID'.
103+
// This panic would happen at program startup, so no worries at runtime panic.
101104
panic(errors.New("ess - guid: unable to get hostname and random bytes"))
102105
}

url.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// aahframework.org/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
import "net/url"
8+
9+
// IsVaildURL method returns true if given raw URL gets parsed without any errors
10+
// otherwise false.
11+
func IsVaildURL(rawurl string) bool {
12+
_, err := url.Parse(rawurl)
13+
return err == nil
14+
}
15+
16+
// IsRelativeURL method returns true if given raw URL is relative URL otherwise false.
17+
func IsRelativeURL(rawurl string) bool {
18+
return !IsAbsURL(rawurl)
19+
}
20+
21+
// IsAbsURL method returns true if given raw URL is absolute URL otherwise false.
22+
func IsAbsURL(rawurl string) bool {
23+
u, err := url.Parse(rawurl)
24+
if err != nil {
25+
return false
26+
}
27+
return u.IsAbs()
28+
}

url_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// aahframework.org/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
import (
8+
"testing"
9+
10+
"aahframework.org/test.v0/assert"
11+
)
12+
13+
func TestURLValue(t *testing.T) {
14+
t.Log("Valid URLs")
15+
for _, u := range []string{
16+
"https://aahframework.org",
17+
"/facebook-auth/callback?code=AQD5-i29_Vn7fNg8VgcV-Uzk_QNO1sx6yO-tJvkRiFaGs1n-wnCxUnvLX0V2q25Tx7JRZAiTds2-DIKrDb8jPEdGquMCedQ_mpMZQxsHmPYeg_cP1Xjy2jHooK-1ZDJZQHXtDSL8FA7r3nVA7WcrCuLZrlrgXq8LnOSAil3oMD-RqPix-nI576GvAPGgiXo6ep_AfS2GaF8A8TOTwl2iwEjB74F23yEukNpz5tmDJVfH02qtrGECuDfaAEPc-4u2wVIIWKCWy3oEEeoEr4zBdzsMtUR1FP3X5yUm0_yAYFP3taAPrpM-5UqtJWmUgaOY-U0&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac",
18+
"https://www.facebook.com/dialog/oauth?client_id=182958108394860&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Ffacebook-auth%2Fcallback&response_type=code&scope=public_profile+email&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac",
19+
"https://godoc.org/golang.org/x/oauth2",
20+
"http://godoc.org/",
21+
} {
22+
assert.True(t, IsVaildURL(u))
23+
}
24+
25+
t.Log("Absolute URL")
26+
for _, u := range []string{
27+
"https://aahframework.org",
28+
"https://www.facebook.com/dialog/oauth?client_id=182958108394860&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Ffacebook-auth%2Fcallback&response_type=code&scope=public_profile+email&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac",
29+
"https://godoc.org/golang.org/x/oauth2",
30+
"http://godoc.org/",
31+
} {
32+
assert.True(t, IsAbsURL(u))
33+
}
34+
35+
t.Log("Error - Absolute URL")
36+
for _, u := range []string{
37+
"://aahframework.org",
38+
"://",
39+
"http",
40+
"https",
41+
} {
42+
assert.False(t, IsAbsURL(u))
43+
}
44+
45+
t.Log("Relative URL")
46+
for _, u := range []string{
47+
"/facebook-auth/callback?code=AQD5-i29_Vn7fNg8VgcV-Uzk_QNO1sx6yO-tJvkRiFaGs1n-wnCxUnvLX0V2q25Tx7JRZAiTds2-DIKrDb8jPEdGquMCedQ_mpMZQxsHmPYeg_cP1Xjy2jHooK-1ZDJZQHXtDSL8FA7r3nVA7WcrCuLZrlrgXq8LnOSAil3oMD-RqPix-nI576GvAPGgiXo6ep_AfS2GaF8A8TOTwl2iwEjB74F23yEukNpz5tmDJVfH02qtrGECuDfaAEPc-4u2wVIIWKCWy3oEEeoEr4zBdzsMtUR1FP3X5yUm0_yAYFP3taAPrpM-5UqtJWmUgaOY-U0&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac",
48+
"/dialog/oauth?client_id=182958108394860&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Ffacebook-auth%2Fcallback&response_type=code&scope=public_profile+email&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac",
49+
"/golang.org/x/oauth2",
50+
} {
51+
assert.True(t, IsRelativeURL(u))
52+
}
53+
54+
}

0 commit comments

Comments
 (0)