Skip to content

Commit 7f32a0b

Browse files
committed
Added unit test cases for api
1 parent 9a0056e commit 7f32a0b

File tree

3 files changed

+112
-14
lines changed

3 files changed

+112
-14
lines changed

api_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*!
2+
* rest-a-framework
3+
* Copyright(c) 2019 Roshan Gade
4+
* MIT Licensed
5+
*/
6+
package rest
7+
8+
import (
9+
"testing"
10+
)
11+
12+
var a API
13+
var h Handler
14+
15+
func validateRoute(fun string, method string, url string, t *testing.T) {
16+
flag := true
17+
for _, route := range a.routes {
18+
if route.method == method && route.pattern == url {
19+
flag = false
20+
break
21+
}
22+
}
23+
24+
if flag {
25+
t.Error("API: " + fun + " is not working properly")
26+
}
27+
}
28+
29+
func TestAPI_Use(t *testing.T) {
30+
a.Use(handle)
31+
32+
if len(a.interceptors) == 0 {
33+
t.Error("API: Use is not working properly")
34+
}
35+
}
36+
37+
func TestAPI_All(t *testing.T) {
38+
a.All("/:uid", handle)
39+
40+
validateRoute("All", "", "/:uid", t)
41+
}
42+
43+
func TestAPI_Get(t *testing.T) {
44+
a.Get("/:uid", handle)
45+
46+
validateRoute("Get", "GET", "/:uid", t)
47+
}
48+
49+
func TestAPI_Post(t *testing.T) {
50+
a.Post("/:uid", handle)
51+
52+
validateRoute("Post", "POST", "/:uid", t)
53+
}
54+
55+
func TestAPI_Put(t *testing.T) {
56+
a.Put("/:uid", handle)
57+
58+
validateRoute("Put", "PUT", "/:uid", t)
59+
}
60+
61+
func TestAPI_Delete(t *testing.T) {
62+
a.Delete("/:uid", handle)
63+
64+
validateRoute("Delete", "DELETE", "/:uid", t)
65+
}
66+
67+
func TestAPI_Options(t *testing.T) {
68+
a.Options("/:uid", handle)
69+
70+
validateRoute("Optioa", "OPTIONS", "/:uid", t)
71+
}
72+
73+
func TestAPI_Head(t *testing.T) {
74+
a.Head("/:uid", handle)
75+
76+
validateRoute("Head", "HEAD", "/:uid", t)
77+
}
78+
79+
func TestAPI_Patch(t *testing.T) {
80+
a.Patch("/:uid", handle)
81+
82+
validateRoute("Patch", "PATCH", "/:uid", t)
83+
}
84+
85+
func TestAPI_Exception(t *testing.T) {
86+
a.Exception("UID_NOT_FOUND", handle)
87+
88+
flag := true
89+
for _, route := range a.exceptions {
90+
if route.message == "UID_NOT_FOUND" {
91+
flag = false
92+
break
93+
}
94+
}
95+
96+
if flag {
97+
t.Error("API: Exception is not working properly")
98+
}
99+
}

context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestContext_JSON2(t *testing.T) {
139139
t.Error("JSON is not working")
140140
}
141141

142-
if !ctx.end {
142+
if ctx.end {
143143
t.Error("JSON is no executing successfully")
144144
}
145145
ctx.destroy()

namespace_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright(c) 2019 Roshan Gade
44
* MIT Licensed
55
*/
6-
76
package rest
87

98
import (
@@ -22,7 +21,7 @@ func TestNamespace_Set(t *testing.T) {
2221
}
2322
}
2423

25-
func validateRoute(fun string, method string, url string, t *testing.T) {
24+
func validateNsRoute(fun string, method string, url string, t *testing.T) {
2625
flag := true
2726
for _, route := range api.routes {
2827
if route.method == method && route.pattern == url {
@@ -32,62 +31,62 @@ func validateRoute(fun string, method string, url string, t *testing.T) {
3231
}
3332

3433
if flag {
35-
t.Error("Namespace " + fun + " is not working properly")
34+
t.Error("Namespace: " + fun + " is not working properly")
3635
}
3736
}
3837

3938
func TestNamespace_Use(t *testing.T) {
4039
ns.Use(handle)
4140

42-
validateRoute("Use", "", "/test/*", t)
41+
validateNsRoute("Use", "", "/test/*", t)
4342
}
4443

4544
func TestNamespace_All(t *testing.T) {
4645
ns.All("/:uid", handle)
4746

48-
validateRoute("All", "", "/test/:uid", t)
47+
validateNsRoute("All", "", "/test/:uid", t)
4948
}
5049

5150
func TestNamespace_Get(t *testing.T) {
5251
ns.Get("/:uid", handle)
5352

54-
validateRoute("Get", "GET", "/test/:uid", t)
53+
validateNsRoute("Get", "GET", "/test/:uid", t)
5554
}
5655

5756
func TestNamespace_Post(t *testing.T) {
5857
ns.Post("/:uid", handle)
5958

60-
validateRoute("Post", "POST", "/test/:uid", t)
59+
validateNsRoute("Post", "POST", "/test/:uid", t)
6160
}
6261

6362
func TestNamespace_Put(t *testing.T) {
6463
ns.Put("/:uid", handle)
6564

66-
validateRoute("Put", "PUT", "/test/:uid", t)
65+
validateNsRoute("Put", "PUT", "/test/:uid", t)
6766
}
6867

6968
func TestNamespace_Delete(t *testing.T) {
7069
ns.Delete("/:uid", handle)
7170

72-
validateRoute("Delete", "DELETE", "/test/:uid", t)
71+
validateNsRoute("Delete", "DELETE", "/test/:uid", t)
7372
}
7473

7574
func TestNamespace_Options(t *testing.T) {
7675
ns.Options("/:uid", handle)
7776

78-
validateRoute("Options", "OPTIONS", "/test/:uid", t)
77+
validateNsRoute("Options", "OPTIONS", "/test/:uid", t)
7978
}
8079

8180
func TestNamespace_Head(t *testing.T) {
8281
ns.Head("/:uid", handle)
8382

84-
validateRoute("Head", "HEAD", "/test/:uid", t)
83+
validateNsRoute("Head", "HEAD", "/test/:uid", t)
8584
}
8685

8786
func TestNamespace_Patch(t *testing.T) {
8887
ns.Patch("/:uid", handle)
8988

90-
validateRoute("Patch", "PATCH", "/test/:uid", t)
89+
validateNsRoute("Patch", "PATCH", "/test/:uid", t)
9190
}
9291

9392
func TestNamespace_Exception(t *testing.T) {
@@ -102,6 +101,6 @@ func TestNamespace_Exception(t *testing.T) {
102101
}
103102

104103
if flag {
105-
t.Error("Namespace Exception is not working properly")
104+
t.Error("Namespace: Exception is not working properly")
106105
}
107106
}

0 commit comments

Comments
 (0)