Skip to content

Commit

Permalink
issue #21 context.Query().Has(param)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmentor committed Dec 16, 2020
1 parent 6a684d4 commit 96be6df
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ func newQuery(req *http.Request) Query {
return Query(param)
}

func (p Query) Has(name string) bool {
_, has := p[name]
return has
}

func (p Query) GetInt64(name string) int64 {
data, has := p[name]

Expand Down
47 changes: 47 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package serv

import (
"net/http/httptest"
"testing"
)

func TestQuery(t *testing.T) {

req := httptest.NewRequest("GET", "/123/13241234?test=1&login=monster&pt=true&pt=p2&wsdl&f=false", nil)
if req == nil {
t.Fatal("test request create failed")
}

qw := newQuery(req)
if qw == nil {
t.Fatal("newQuery failed")
}

if qw.Has("unknown") {
t.Fatal("Has return true for unknown param")
}

if !qw.Has("test") || !qw.Has("login") || !qw.Has("wsdl") {
t.Fatal("Not found all fields")
}

if qw.GetInt64("unknown") != 0 || qw.GetInt64("test") != 1 || qw.GetInt64("wsdl") != 0 {
t.Fatal("GetInt64 failed")
}

if qw.GetInt("unknown") != 0 || qw.GetInt("test") != 1 || qw.GetInt("wsdl") != 0 {
t.Fatal("GetInt failed")
}

if qw.GetFloat("unknown") != 0 || qw.GetFloat("test") != 1 || qw.GetFloat("wsdl") != 0 {
t.Fatal("GetFloat failed")
}

if qw.GetString("unknown") != "" || qw.GetString("test") != "1" || qw.GetString("wsdl") != "" || qw.GetString("f") != "false" {
t.Fatal("GetFloat failed")
}

if qw.GetBool("unknown") || !qw.GetBool("test") || qw.GetBool("wsdl") || qw.GetBool("f") || !qw.GetBool("test") || !qw.GetBool("pt") {
t.Fatal("GetBool failed")
}
}

0 comments on commit 96be6df

Please sign in to comment.