-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in phstsai/VX-55 (pull request #7)
DEV VX-55: List handler for network Approved-by: Hung-Wei Chiu <[email protected]>
- Loading branch information
Showing
5 changed files
with
286 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package query | ||
|
||
import ( | ||
"net/url" | ||
"strconv" | ||
) | ||
|
||
type QueryUrl struct { | ||
Url url.Values `json:"url"` | ||
} | ||
|
||
func New(url url.Values) *QueryUrl { | ||
return &QueryUrl{ | ||
Url: url, | ||
} | ||
} | ||
|
||
func (query *QueryUrl) Int(key string, defaultValue int) (int, error) { | ||
values := query.Url[key] | ||
|
||
if len(values) == 0 { | ||
return defaultValue, nil | ||
} | ||
|
||
val, err := strconv.Atoi(values[0]) | ||
if err != nil { | ||
return defaultValue, err | ||
} | ||
|
||
return int(val), nil | ||
} | ||
|
||
func (query *QueryUrl) Int64(key string, defaultValue int64) (int64, error) { | ||
values := query.Url[key] | ||
|
||
if len(values) == 0 { | ||
return defaultValue, nil | ||
} | ||
|
||
val, err := strconv.ParseInt(values[0], 10, 64) | ||
if err != nil { | ||
return defaultValue, err | ||
} | ||
|
||
return int64(val), nil | ||
} | ||
|
||
func (query *QueryUrl) Str(key string) (string, bool) { | ||
values := query.Url[key] | ||
|
||
if len(values) == 0 { | ||
return "", false | ||
} | ||
|
||
return values[0], true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package query | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestQueryInt(t *testing.T) { | ||
expectedInt := 123 | ||
req, err := http.NewRequest("GET", "/test?iamint="+strconv.Itoa(expectedInt), nil) | ||
assert.NoError(t, err) | ||
|
||
q := New(req.URL.Query()) | ||
|
||
v, err := q.Int("iamint", 0) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedInt, v) | ||
} | ||
|
||
func TestQueryIntByDefault(t *testing.T) { | ||
defaultInt := 10 | ||
req, err := http.NewRequest("GET", "/test", nil) | ||
assert.NoError(t, err) | ||
|
||
q := New(req.URL.Query()) | ||
|
||
v, err := q.Int("notfound", defaultInt) | ||
assert.NoError(t, err) | ||
assert.Equal(t, defaultInt, v) | ||
} | ||
|
||
func TestQueryIntFail(t *testing.T) { | ||
defaultInt := 10 | ||
req, err := http.NewRequest("GET", "/test?iamint=abc", nil) | ||
assert.NoError(t, err) | ||
|
||
q := New(req.URL.Query()) | ||
|
||
v, err := q.Int("iamint", defaultInt) | ||
assert.Error(t, err) | ||
assert.Equal(t, defaultInt, v) | ||
} | ||
|
||
func TestInt64(t *testing.T) { | ||
expectedInt := int64(42949672951) | ||
req, err := http.NewRequest("GET", "/test?iamint="+strconv.FormatInt(expectedInt, 10), nil) | ||
assert.NoError(t, err) | ||
|
||
q := New(req.URL.Query()) | ||
|
||
v, err := q.Int64("iamint", 0) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedInt, v) | ||
} | ||
|
||
func TestInt64ByDefault(t *testing.T) { | ||
defaultInt := int64(21474836471) | ||
req, err := http.NewRequest("GET", "/test", nil) | ||
assert.NoError(t, err) | ||
|
||
q := New(req.URL.Query()) | ||
|
||
v, err := q.Int64("notfound", defaultInt) | ||
assert.NoError(t, err) | ||
assert.Equal(t, defaultInt, v) | ||
} | ||
|
||
func TestInt64Fail(t *testing.T) { | ||
defaultInt := int64(21474836471) | ||
req, err := http.NewRequest("GET", "/test?iamint=abc", nil) | ||
assert.NoError(t, err) | ||
|
||
q := New(req.URL.Query()) | ||
|
||
v, err := q.Int64("iamint", defaultInt) | ||
assert.Error(t, err) | ||
assert.Equal(t, defaultInt, v) | ||
} | ||
|
||
func TestStr(t *testing.T) { | ||
expectedStr := "YoYo" | ||
req, err := http.NewRequest("GET", "/test?Hey="+expectedStr, nil) | ||
assert.NoError(t, err) | ||
|
||
q := New(req.URL.Query()) | ||
|
||
v, ok := q.Str("Hey") | ||
assert.True(t, ok) | ||
assert.Equal(t, expectedStr, v) | ||
} | ||
|
||
func TestStrByDefault(t *testing.T) { | ||
req, err := http.NewRequest("GET", "/test", nil) | ||
assert.NoError(t, err) | ||
|
||
q := New(req.URL.Query()) | ||
|
||
v, ok := q.Str("notfound") | ||
assert.False(t, ok) | ||
assert.Equal(t, "", v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters