Skip to content

Commit 9b91836

Browse files
authored
Merge pull request #3 from go-rs/develop
0.0.1-alpha.3
2 parents 74cf23c + 7f32a0b commit 9b91836

10 files changed

+679
-6
lines changed

api_test.go

+99
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.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
type Context struct {
1818
Request *http.Request
1919
Response http.ResponseWriter
20-
Params *map[string]string
20+
Params map[string]string
2121
data map[string]interface{}
2222
err error
2323
status int

context_test.go

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*!
2+
* rest-api-framework
3+
* Copyright(c) 2019 Roshan Gade
4+
* MIT Licensed
5+
*/
6+
package rest
7+
8+
import (
9+
"errors"
10+
"net/http/httptest"
11+
"testing"
12+
)
13+
14+
var ctx Context
15+
16+
func TestContext_Set(t *testing.T) {
17+
ctx.init()
18+
ctx.Set("foo", "bar")
19+
20+
if ctx.data["foo"] != "bar" {
21+
t.Error("Set is not working")
22+
}
23+
}
24+
25+
func TestContext_Get1(t *testing.T) {
26+
bar, exists := ctx.Get("foo")
27+
28+
if !exists && bar != "bar" {
29+
t.Error("Get is not working")
30+
}
31+
}
32+
33+
func TestContext_Get2(t *testing.T) {
34+
_, exists := ctx.Get("bar")
35+
36+
if exists {
37+
t.Error("Get is not working")
38+
}
39+
}
40+
41+
func TestContext_Status(t *testing.T) {
42+
_ctx := ctx.Status(200)
43+
44+
if ctx.status != 200 {
45+
t.Error("Status is not working")
46+
}
47+
48+
if _ctx != &ctx {
49+
t.Error("Status is not returning ctx object")
50+
}
51+
}
52+
53+
func TestContext_Throw(t *testing.T) {
54+
err := errors.New("test error")
55+
ctx.Throw(err)
56+
57+
if ctx.err != err {
58+
t.Error("Throw is not working")
59+
}
60+
61+
ctx.err = nil
62+
}
63+
64+
func TestContext_GetError(t *testing.T) {
65+
err := errors.New("test error")
66+
ctx.Throw(err)
67+
68+
if ctx.GetError() != err {
69+
t.Error("GetError is not working")
70+
}
71+
72+
ctx.err = nil
73+
}
74+
75+
func TestContext_End(t *testing.T) {
76+
ctx.End()
77+
78+
if !ctx.end {
79+
t.Error("End is not working")
80+
}
81+
82+
ctx.end = false
83+
}
84+
85+
func TestContext_Write1(t *testing.T) {
86+
ctx.init()
87+
ctx.Response = httptest.NewRecorder()
88+
data := []byte("Hello World")
89+
ctx.Write(data)
90+
91+
if ctx.err != nil {
92+
t.Error("Write is not working")
93+
}
94+
95+
if !ctx.end {
96+
t.Error("Write is no executing successfully")
97+
}
98+
ctx.destroy()
99+
}
100+
101+
func TestContext_Write2(t *testing.T) {
102+
ctx.init()
103+
ctx.Response = httptest.NewRecorder()
104+
ctx.end = true
105+
data := []byte("Hello World")
106+
ctx.Write(data)
107+
108+
if ctx.err != nil {
109+
t.Error("Write is not working")
110+
}
111+
112+
if !ctx.end {
113+
t.Error("Write is no executing successfully")
114+
}
115+
ctx.destroy()
116+
}
117+
118+
func TestContext_JSON1(t *testing.T) {
119+
ctx.init()
120+
ctx.Response = httptest.NewRecorder()
121+
ctx.JSON([]string{"Hello", "World"})
122+
123+
if ctx.err != nil {
124+
t.Error("JSON is not working")
125+
}
126+
127+
if !ctx.end {
128+
t.Error("JSON is no executing successfully")
129+
}
130+
ctx.destroy()
131+
}
132+
133+
func TestContext_JSON2(t *testing.T) {
134+
ctx.init()
135+
ctx.Response = httptest.NewRecorder()
136+
ctx.JSON("Hello World")
137+
138+
if ctx.err.Error() != "INVALID_JSON_RESPONSE" {
139+
t.Error("JSON is not working")
140+
}
141+
142+
if ctx.end {
143+
t.Error("JSON is no executing successfully")
144+
}
145+
ctx.destroy()
146+
}
147+
148+
func TestContext_Text(t *testing.T) {
149+
ctx.init()
150+
ctx.Response = httptest.NewRecorder()
151+
ctx.Text("Hello World")
152+
153+
if ctx.err != nil {
154+
t.Error("Text is not working")
155+
}
156+
157+
if !ctx.end {
158+
t.Error("Text is no executing successfully")
159+
}
160+
ctx.destroy()
161+
}

namespace_test.go

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*!
2+
* rest-api-framework
3+
* Copyright(c) 2019 Roshan Gade
4+
* MIT Licensed
5+
*/
6+
package rest
7+
8+
import (
9+
"testing"
10+
)
11+
12+
var api API
13+
var ns Namespace
14+
var handle Handler
15+
16+
func TestNamespace_Set(t *testing.T) {
17+
ns.Set("/test", &api)
18+
19+
if ns.prefix != "/test" {
20+
t.Error("Prefix is not set.")
21+
}
22+
}
23+
24+
func validateNsRoute(fun string, method string, url string, t *testing.T) {
25+
flag := true
26+
for _, route := range api.routes {
27+
if route.method == method && route.pattern == url {
28+
flag = false
29+
break
30+
}
31+
}
32+
33+
if flag {
34+
t.Error("Namespace: " + fun + " is not working properly")
35+
}
36+
}
37+
38+
func TestNamespace_Use(t *testing.T) {
39+
ns.Use(handle)
40+
41+
validateNsRoute("Use", "", "/test/*", t)
42+
}
43+
44+
func TestNamespace_All(t *testing.T) {
45+
ns.All("/:uid", handle)
46+
47+
validateNsRoute("All", "", "/test/:uid", t)
48+
}
49+
50+
func TestNamespace_Get(t *testing.T) {
51+
ns.Get("/:uid", handle)
52+
53+
validateNsRoute("Get", "GET", "/test/:uid", t)
54+
}
55+
56+
func TestNamespace_Post(t *testing.T) {
57+
ns.Post("/:uid", handle)
58+
59+
validateNsRoute("Post", "POST", "/test/:uid", t)
60+
}
61+
62+
func TestNamespace_Put(t *testing.T) {
63+
ns.Put("/:uid", handle)
64+
65+
validateNsRoute("Put", "PUT", "/test/:uid", t)
66+
}
67+
68+
func TestNamespace_Delete(t *testing.T) {
69+
ns.Delete("/:uid", handle)
70+
71+
validateNsRoute("Delete", "DELETE", "/test/:uid", t)
72+
}
73+
74+
func TestNamespace_Options(t *testing.T) {
75+
ns.Options("/:uid", handle)
76+
77+
validateNsRoute("Options", "OPTIONS", "/test/:uid", t)
78+
}
79+
80+
func TestNamespace_Head(t *testing.T) {
81+
ns.Head("/:uid", handle)
82+
83+
validateNsRoute("Head", "HEAD", "/test/:uid", t)
84+
}
85+
86+
func TestNamespace_Patch(t *testing.T) {
87+
ns.Patch("/:uid", handle)
88+
89+
validateNsRoute("Patch", "PATCH", "/test/:uid", t)
90+
}
91+
92+
func TestNamespace_Exception(t *testing.T) {
93+
ns.Exception("UID_NOT_FOUND", handle)
94+
95+
flag := true
96+
for _, route := range api.exceptions {
97+
if route.message == "UID_NOT_FOUND" {
98+
flag = false
99+
break
100+
}
101+
}
102+
103+
if flag {
104+
t.Error("Namespace: Exception is not working properly")
105+
}
106+
}

render/json.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ var (
2929
* JSON Write
3030
*/
3131
func (j JSON) Write(w http.ResponseWriter) (data []byte, err error) {
32-
if reflect.TypeOf(j.Body).String() == "string" {
32+
_type := reflect.TypeOf(j.Body).String()
33+
if _type == "int" || _type == "float64" || _type == "bool" {
34+
err = invalidJson
35+
} else if _type == "string" {
3336
data, err = json.RawMessage(j.Body.(string)).MarshalJSON()
3437
} else {
3538
data, err = json.Marshal(j.Body)

0 commit comments

Comments
 (0)