Skip to content

Commit c460669

Browse files
committed
added template and unit test cases
1 parent d372ebb commit c460669

File tree

8 files changed

+165
-8
lines changed

8 files changed

+165
-8
lines changed

api.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// go-rs/rest-api-framework
2+
// Copyright(c) 2019 Roshan Gade. All rights reserved.
3+
// MIT Licensed
14
package rest
25

36
import (
@@ -53,15 +56,15 @@ func (a *api) Get(pattern string, task Handler) {
5356
}
5457

5558
func (a *api) Post(pattern string, task Handler) {
56-
a.list.route(http.MethodGet, a.prefix+pattern, task)
59+
a.list.route(http.MethodPost, a.prefix+pattern, task)
5760
}
5861

5962
func (a *api) Put(pattern string, task Handler) {
60-
a.list.route(http.MethodGet, a.prefix+pattern, task)
63+
a.list.route(http.MethodPut, a.prefix+pattern, task)
6164
}
6265

6366
func (a *api) Delete(pattern string, task Handler) {
64-
a.list.route(http.MethodGet, a.prefix+pattern, task)
67+
a.list.route(http.MethodDelete, a.prefix+pattern, task)
6568
}
6669

6770
func (a *api) Exception(code string, task ErrorHandler) {

api_test.go

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package rest
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
)
8+
9+
var _api *api
10+
var _list *list
11+
var _handler *handler
12+
var task = func(ctx Context) {}
13+
var errTask = func(err error, ctx Context) {}
14+
15+
func init() {
16+
_list = new(list)
17+
18+
_handler = &handler{
19+
list: _list,
20+
}
21+
22+
_api = &api{
23+
prefix: trim("/"),
24+
list: _list,
25+
handler: _handler,
26+
}
27+
}
28+
29+
func TestApi_Use(t *testing.T) {
30+
_api.Use(task)
31+
32+
if !(len(_list.middlewares) == 1 && len(_list.routes) == 0 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) {
33+
t.Error("api.Use should add middleware in the list")
34+
}
35+
36+
if _list.middlewares[0].pattern == nil || _list.middlewares[0].task == nil {
37+
t.Error("api.Use should add type of middleware with pattern and task")
38+
}
39+
}
40+
41+
func TestApi_Get(t *testing.T) {
42+
_api.Get("/", task)
43+
44+
if !(len(_list.middlewares) == 1 && len(_list.routes) == 1 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) {
45+
t.Error("api.Get should add route in the list")
46+
}
47+
48+
if _list.routes[0].method != http.MethodGet || _list.routes[0].pattern == nil || _list.routes[0].task == nil {
49+
t.Error("api.Get should add type of route with method, pattern and task")
50+
}
51+
}
52+
53+
func TestApi_Post(t *testing.T) {
54+
_api.Post("/", task)
55+
56+
if !(len(_list.middlewares) == 1 && len(_list.routes) == 2 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) {
57+
t.Error("api.Post should add route in the list")
58+
}
59+
60+
if _list.routes[1].method != http.MethodPost || _list.routes[1].pattern == nil || _list.routes[1].task == nil {
61+
t.Error("api.Post should add type of route with method, pattern and task")
62+
}
63+
}
64+
65+
func TestApi_Put(t *testing.T) {
66+
_api.Put("/", task)
67+
68+
if !(len(_list.middlewares) == 1 && len(_list.routes) == 3 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) {
69+
t.Error("api.Put should add route in the list")
70+
}
71+
72+
if _list.routes[2].method != http.MethodPut || _list.routes[2].pattern == nil || _list.routes[2].task == nil {
73+
t.Error("api.Put should add type of route with method, pattern and task")
74+
}
75+
}
76+
77+
func TestApi_Delete(t *testing.T) {
78+
_api.Delete("/", task)
79+
80+
if !(len(_list.middlewares) == 1 && len(_list.routes) == 4 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) {
81+
t.Error("api.Delete should add route in the list")
82+
}
83+
84+
if _list.routes[3].method != http.MethodDelete || _list.routes[3].pattern == nil || _list.routes[3].task == nil {
85+
t.Error("api.Delete should add type of route with method, pattern and task")
86+
}
87+
}
88+
89+
func TestApi_Group(t *testing.T) {
90+
user := _api.Group("/user")
91+
92+
if user == nil {
93+
t.Error("api.Group should return api reference")
94+
}
95+
}
96+
97+
func TestApi_Exception(t *testing.T) {
98+
_api.Exception(ErrCodeNotFound, errTask)
99+
100+
if !(len(_list.middlewares) == 1 && len(_list.routes) == 4 && len(_list.exceptions) == 1 && _list.uncaughtException == nil) {
101+
t.Error("api.Exception should add exception handler in the list")
102+
}
103+
104+
if _list.exceptions[0].code != ErrCodeNotFound || _list.exceptions[0].task == nil {
105+
t.Error("api.Exception should add type of exception with code and task")
106+
}
107+
}
108+
109+
func TestApi_UncaughtException(t *testing.T) {
110+
_api.UncaughtException(errTask)
111+
112+
if !(len(_list.middlewares) == 1 && len(_list.routes) == 4 && len(_list.exceptions) == 1 && _list.uncaughtException != nil) {
113+
t.Error("api.UncaughtException should add uncaught exception handler in the list")
114+
}
115+
116+
if _list.uncaughtException == nil {
117+
t.Error("api.UncaughtException should add type of uncaughtException with task")
118+
}
119+
}
120+
121+
func TestApi_ServeHTTP(t *testing.T) {
122+
r := httptest.NewRequest(http.MethodGet, "/", nil)
123+
w := httptest.NewRecorder()
124+
125+
_api.ServeHTTP(w, r)
126+
127+
if w.Result().StatusCode != 200 {
128+
t.Error("api.ServeHTTP should respond on every request")
129+
}
130+
}

context.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// go-rs/rest-api-framework
2+
// Copyright(c) 2019 Roshan Gade. All rights reserved.
3+
// MIT Licensed
14
package rest
25

36
import (

handler.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// go-rs/rest-api-framework
2+
// Copyright(c) 2019 Roshan Gade. All rights reserved.
3+
// MIT Licensed
14
package rest
25

36
import (

helper.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// go-rs/rest-api-framework
2+
// Copyright(c) 2019 Roshan Gade. All rights reserved.
3+
// MIT Licensed
14
package rest
25

36
import (
47
"encoding/json"
58
"encoding/xml"
6-
"errors"
79
"reflect"
810
"regexp"
911
"strings"
@@ -88,14 +90,11 @@ func trim(str string) string {
8890
func jsonToBytes(data interface{}) ([]byte, error) {
8991
_type := reflect.TypeOf(data).String()
9092

91-
if _type == "int" || _type == "float64" || _type == "bool" {
92-
return nil, errors.New("invalid JSON data")
93-
}
94-
9593
if _type == "string" {
9694
return json.RawMessage(data.(string)).MarshalJSON()
9795
}
9896

97+
//standard JSON as per RFC 7159
9998
return json.Marshal(data)
10099
}
101100

list.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// go-rs/rest-api-framework
2+
// Copyright(c) 2019 Roshan Gade. All rights reserved.
3+
// MIT Licensed
14
package rest
25

36
import "log"

rest.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// go-rs/rest-api-framework
2+
// Copyright(c) 2019 Roshan Gade. All rights reserved.
3+
// MIT Licensed
14
package rest
25

36
func New(prefix string) API {

rest_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package rest
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNew(t *testing.T) {
8+
api := New("/")
9+
10+
if api == nil {
11+
t.Error("New function should return API reference.")
12+
}
13+
}

0 commit comments

Comments
 (0)