Skip to content

Commit a7e8918

Browse files
authored
Merge pull request #20 from go-labx/feature/context_add_query_bool
feat: add QueryBool method to Context and comprehensive unit tests for Context methods
2 parents 68a0156 + 8be1e16 commit a7e8918

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed

context.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ func (c *Context) QueryString(key string) string {
202202
return c.req.query(key)
203203
}
204204

205+
// QueryBool returns the value of a given query parameter as a bool.
206+
func (c *Context) QueryBool(key string) (bool, error) {
207+
str := c.req.query(key)
208+
if str == "" {
209+
return false, nil
210+
}
211+
value, err := strconv.ParseBool(str)
212+
if err != nil {
213+
return false, err
214+
}
215+
return value, nil
216+
}
217+
205218
// QueryInt returns the value of a given query parameter as an int.
206219
func (c *Context) QueryInt(key string) (int, error) {
207220
str := c.req.query(key)

context_test.go

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,60 @@ func TestContext_QueryString(t *testing.T) {
528528
}
529529
}
530530

531+
func TestContext_QueryBool(t *testing.T) {
532+
req, err := http.NewRequest("GET", "/path?key=true", nil)
533+
if err != nil {
534+
t.Fatal(err)
535+
}
536+
ctx, err := NewContext(httptest.NewRecorder(), req)
537+
if err != nil {
538+
t.Fatal(err)
539+
}
540+
got, err := ctx.QueryBool("key")
541+
if err != nil {
542+
t.Errorf("ctx.QueryBool(\"key\") returned an error: %v", err)
543+
}
544+
want := true
545+
if got != want {
546+
t.Errorf("QueryBool() = %v, want %v", got, want)
547+
}
548+
}
549+
550+
func TestContext_QueryBoolWithException(t *testing.T) {
551+
req, err := http.NewRequest("GET", "/path?key=notabool", nil)
552+
if err != nil {
553+
t.Fatal(err)
554+
}
555+
ctx, err := NewContext(httptest.NewRecorder(), req)
556+
if err != nil {
557+
t.Fatal(err)
558+
}
559+
560+
_, err = ctx.QueryBool("key")
561+
if err == nil {
562+
t.Error("ctx.QueryBool(\"key\") did not return an error")
563+
}
564+
}
565+
566+
func TestContext_QueryBoolWithEmptyKey(t *testing.T) {
567+
req, err := http.NewRequest("GET", "/path?key=", nil)
568+
if err != nil {
569+
t.Fatal(err)
570+
}
571+
ctx, err := NewContext(httptest.NewRecorder(), req)
572+
if err != nil {
573+
t.Fatal(err)
574+
}
575+
got, err := ctx.QueryBool("key")
576+
if err != nil {
577+
t.Errorf("ctx.QueryBool(\"key\") returned an error: %v", err)
578+
}
579+
want := false
580+
if got != want {
581+
t.Errorf("QueryBool() = %v, want %v", got, want)
582+
}
583+
}
584+
531585
func TestContext_QueryInt(t *testing.T) {
532586
req, err := http.NewRequest("GET", "/path?key=123", nil)
533587
if err != nil {
@@ -563,6 +617,25 @@ func TestContext_QueryIntWithException(t *testing.T) {
563617
}
564618
}
565619

620+
func TestContext_QueryIntWithEmptyKey(t *testing.T) {
621+
req, err := http.NewRequest("GET", "/path?key=", nil)
622+
if err != nil {
623+
t.Fatal(err)
624+
}
625+
ctx, err := NewContext(httptest.NewRecorder(), req)
626+
if err != nil {
627+
t.Fatal(err)
628+
}
629+
got, err := ctx.QueryInt("key")
630+
if err != nil {
631+
t.Errorf("ctx.QueryInt(\"key\") returned an error: %v", err)
632+
}
633+
want := 0
634+
if got != want {
635+
t.Errorf("QueryInt() = %d, want %d", got, want)
636+
}
637+
}
638+
566639
func TestContext_QueryUInt(t *testing.T) {
567640
req, err := http.NewRequest("GET", "/path?key=123", nil)
568641
if err != nil {
@@ -598,6 +671,25 @@ func TestContext_QueryUIntWithException(t *testing.T) {
598671
}
599672
}
600673

674+
func TestContext_QueryUIntWithEmptyKey(t *testing.T) {
675+
req, err := http.NewRequest("GET", "/path?key=", nil)
676+
if err != nil {
677+
t.Fatal(err)
678+
}
679+
ctx, err := NewContext(httptest.NewRecorder(), req)
680+
if err != nil {
681+
t.Fatal(err)
682+
}
683+
got, err := ctx.QueryUInt("key")
684+
if err != nil {
685+
t.Errorf("ctx.QueryUInt(\"key\") returned an error: %v", err)
686+
}
687+
want := uint(0)
688+
if got != want {
689+
t.Errorf("QueryUInt() = %d, want %d", got, want)
690+
}
691+
}
692+
601693
func TestContext_QueryInt8(t *testing.T) {
602694
req, err := http.NewRequest("GET", "/path?key=123", nil)
603695
if err != nil {
@@ -633,6 +725,25 @@ func TestContext_QueryInt8WithException(t *testing.T) {
633725
}
634726
}
635727

728+
func TestContext_QueryInt8WithEmptyKey(t *testing.T) {
729+
req, err := http.NewRequest("GET", "/path?key=", nil)
730+
if err != nil {
731+
t.Fatal(err)
732+
}
733+
ctx, err := NewContext(httptest.NewRecorder(), req)
734+
if err != nil {
735+
t.Fatal(err)
736+
}
737+
got, err := ctx.QueryInt8("key")
738+
if err != nil {
739+
t.Errorf("ctx.QueryInt8(\"key\") returned an error: %v", err)
740+
}
741+
want := int8(0)
742+
if got != want {
743+
t.Errorf("QueryInt8() = %d, want %d", got, want)
744+
}
745+
}
746+
636747
func TestContext_QueryUInt8(t *testing.T) {
637748
req, err := http.NewRequest("GET", "/path?key=123", nil)
638749
if err != nil {
@@ -668,6 +779,25 @@ func TestContext_QueryUInt8WithException(t *testing.T) {
668779
}
669780
}
670781

782+
func TestContext_QueryUInt8WithEmptyKey(t *testing.T) {
783+
req, err := http.NewRequest("GET", "/path?key=", nil)
784+
if err != nil {
785+
t.Fatal(err)
786+
}
787+
ctx, err := NewContext(httptest.NewRecorder(), req)
788+
if err != nil {
789+
t.Fatal(err)
790+
}
791+
got, err := ctx.QueryUInt8("key")
792+
if err != nil {
793+
t.Errorf("ctx.QueryUInt8(\"key\") returned an error: %v", err)
794+
}
795+
want := uint8(0)
796+
if got != want {
797+
t.Errorf("QueryUInt8() = %d, want %d", got, want)
798+
}
799+
}
800+
671801
func TestContext_QueryInt32(t *testing.T) {
672802
req, err := http.NewRequest("GET", "/path?key=123", nil)
673803
if err != nil {
@@ -703,6 +833,25 @@ func TestContext_QueryInt32WithException(t *testing.T) {
703833
}
704834
}
705835

836+
func TestContext_QueryInt32WithEmptyKey(t *testing.T) {
837+
req, err := http.NewRequest("GET", "/path?key=", nil)
838+
if err != nil {
839+
t.Fatal(err)
840+
}
841+
ctx, err := NewContext(httptest.NewRecorder(), req)
842+
if err != nil {
843+
t.Fatal(err)
844+
}
845+
got, err := ctx.QueryInt32("key")
846+
if err != nil {
847+
t.Errorf("ctx.QueryInt32(\"key\") returned an error: %v", err)
848+
}
849+
want := int32(0)
850+
if got != want {
851+
t.Errorf("QueryInt32() = %d, want %d", got, want)
852+
}
853+
}
854+
706855
func TestContext_QueryUInt32(t *testing.T) {
707856
req, err := http.NewRequest("GET", "/path?key=123", nil)
708857
if err != nil {
@@ -738,6 +887,25 @@ func TestContext_QueryUInt32WithException(t *testing.T) {
738887
}
739888
}
740889

890+
func TestContext_QueryUInt32WithEmptyKey(t *testing.T) {
891+
req, err := http.NewRequest("GET", "/path?key=", nil)
892+
if err != nil {
893+
t.Fatal(err)
894+
}
895+
ctx, err := NewContext(httptest.NewRecorder(), req)
896+
if err != nil {
897+
t.Fatal(err)
898+
}
899+
got, err := ctx.QueryUInt32("key")
900+
if err != nil {
901+
t.Errorf("ctx.QueryUInt32(\"key\") returned an error: %v", err)
902+
}
903+
want := uint32(0)
904+
if got != want {
905+
t.Errorf("QueryUInt32() = %d, want %d", got, want)
906+
}
907+
}
908+
741909
func TestContext_QueryInt64(t *testing.T) {
742910
req, err := http.NewRequest("GET", "/path?key=123", nil)
743911
if err != nil {
@@ -773,6 +941,25 @@ func TestContext_QueryInt64WithException(t *testing.T) {
773941
}
774942
}
775943

944+
func TestContext_QueryInt64WithEmptyKey(t *testing.T) {
945+
req, err := http.NewRequest("GET", "/path?key=", nil)
946+
if err != nil {
947+
t.Fatal(err)
948+
}
949+
ctx, err := NewContext(httptest.NewRecorder(), req)
950+
if err != nil {
951+
t.Fatal(err)
952+
}
953+
got, err := ctx.QueryInt64("key")
954+
if err != nil {
955+
t.Errorf("ctx.QueryInt64(\"key\") returned an error: %v", err)
956+
}
957+
want := int64(0)
958+
if got != want {
959+
t.Errorf("QueryInt64() = %d, want %d", got, want)
960+
}
961+
}
962+
776963
func TestContext_QueryUInt64(t *testing.T) {
777964
req, err := http.NewRequest("GET", "/path?key=123", nil)
778965
if err != nil {
@@ -808,6 +995,25 @@ func TestContext_QueryUInt64WithException(t *testing.T) {
808995
}
809996
}
810997

998+
func TestContext_QueryUInt64WithEmptyKey(t *testing.T) {
999+
req, err := http.NewRequest("GET", "/path?key=", nil)
1000+
if err != nil {
1001+
t.Fatal(err)
1002+
}
1003+
ctx, err := NewContext(httptest.NewRecorder(), req)
1004+
if err != nil {
1005+
t.Fatal(err)
1006+
}
1007+
got, err := ctx.QueryUInt64("key")
1008+
if err != nil {
1009+
t.Errorf("ctx.QueryUInt64(\"key\") returned an error: %v", err)
1010+
}
1011+
want := uint64(0)
1012+
if got != want {
1013+
t.Errorf("QueryUInt64() = %d, want %d", got, want)
1014+
}
1015+
}
1016+
8111017
func TestContext_QueryFloat32(t *testing.T) {
8121018
req, err := http.NewRequest("GET", "/path?key=3.1415", nil)
8131019
if err != nil {
@@ -843,6 +1049,25 @@ func TestContext_QueryFloat32WithException(t *testing.T) {
8431049
}
8441050
}
8451051

1052+
func TestContext_QueryFloat32WithEmptyKey(t *testing.T) {
1053+
req, err := http.NewRequest("GET", "/path?key=", nil)
1054+
if err != nil {
1055+
t.Fatal(err)
1056+
}
1057+
ctx, err := NewContext(httptest.NewRecorder(), req)
1058+
if err != nil {
1059+
t.Fatal(err)
1060+
}
1061+
got, err := ctx.QueryFloat32("key")
1062+
if err != nil {
1063+
t.Errorf("ctx.QueryFloat32(\"key\") returned an error: %v", err)
1064+
}
1065+
want := float32(0)
1066+
if got != want {
1067+
t.Errorf("QueryFloat32() = %f, want %f", got, want)
1068+
}
1069+
}
1070+
8461071
func TestContext_QueryFloat64(t *testing.T) {
8471072
req, err := http.NewRequest("GET", "/path?key=3.1415", nil)
8481073
if err != nil {
@@ -878,6 +1103,25 @@ func TestContext_QueryFloat64WithException(t *testing.T) {
8781103
}
8791104
}
8801105

1106+
func TestContext_QueryFloat64WithEmptyKey(t *testing.T) {
1107+
req, err := http.NewRequest("GET", "/path?key=", nil)
1108+
if err != nil {
1109+
t.Fatal(err)
1110+
}
1111+
ctx, err := NewContext(httptest.NewRecorder(), req)
1112+
if err != nil {
1113+
t.Fatal(err)
1114+
}
1115+
got, err := ctx.QueryFloat64("key")
1116+
if err != nil {
1117+
t.Errorf("ctx.QueryFloat64(\"key\") returned an error: %v", err)
1118+
}
1119+
want := float64(0)
1120+
if got != want {
1121+
t.Errorf("QueryFloat64() = %f, want %f", got, want)
1122+
}
1123+
}
1124+
8811125
func TestContextQueries(t *testing.T) {
8821126
req, err := http.NewRequest("GET", "/path?foo=bar&baz=qux", nil)
8831127
if err != nil {

0 commit comments

Comments
 (0)