Skip to content

Commit 983fc56

Browse files
committed
Added check in JSON render for int, float64 & bool
Added unit test cases for render functions
1 parent 56fb737 commit 983fc56

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

render/json.go

Lines changed: 4 additions & 1 deletion
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)

render/json_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*!
2+
* rest-api-framework
3+
* Copyright(c) 2019 Roshan Gade
4+
* MIT Licensed
5+
*/
6+
package render
7+
8+
import (
9+
"net/http/httptest"
10+
"testing"
11+
)
12+
13+
func TestJSON_Write1(t *testing.T) {
14+
json := JSON{
15+
Body: "Hello World",
16+
}
17+
w := httptest.NewRecorder()
18+
_, err := json.Write(w)
19+
20+
if err != invalidJson {
21+
t.Error("Should not render text")
22+
}
23+
24+
if w.Header().Get("Content-Type") != "" {
25+
t.Error("Content-Type Header is not set.")
26+
}
27+
}
28+
29+
func TestJSON_Write2(t *testing.T) {
30+
json := JSON{
31+
Body: "{\"Message\":\"Hello World\"}",
32+
}
33+
w := httptest.NewRecorder()
34+
_, err := json.Write(w)
35+
36+
if err != nil {
37+
t.Error("Should not throw an error")
38+
}
39+
40+
if w.Header().Get("Content-Type") != "application/json" {
41+
t.Error("Content-Type Header is not set.")
42+
}
43+
}
44+
45+
func TestJSON_Write3(t *testing.T) {
46+
json := JSON{
47+
Body: make(map[string]string),
48+
}
49+
w := httptest.NewRecorder()
50+
_, err := json.Write(w)
51+
52+
if err != nil {
53+
t.Error("Should not throw an error")
54+
}
55+
56+
if w.Header().Get("Content-Type") != "application/json" {
57+
t.Error("Content-Type Header is not set.")
58+
}
59+
}
60+
61+
func TestJSON_Write4(t *testing.T) {
62+
json := JSON{
63+
Body: true,
64+
}
65+
w := httptest.NewRecorder()
66+
_, err := json.Write(w)
67+
68+
if err != invalidJson {
69+
t.Error("Should not throw an error")
70+
}
71+
72+
if w.Header().Get("Content-Type") != "" {
73+
t.Error("Content-Type Header is not set.")
74+
}
75+
}

render/text_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*!
2+
* rest-api-framework
3+
* Copyright(c) 2019 Roshan Gade
4+
* MIT Licensed
5+
*/
6+
package render
7+
8+
import (
9+
"net/http/httptest"
10+
"testing"
11+
)
12+
13+
func TestText_Write(t *testing.T) {
14+
txt := Text{
15+
Body: "Hello World",
16+
}
17+
w := httptest.NewRecorder()
18+
body, err := txt.Write(w)
19+
20+
if err != nil || string(body) != txt.Body {
21+
t.Error("Render text is not valid")
22+
}
23+
24+
if w.Header().Get("Content-Type") != "text/plain" {
25+
t.Error("Content-Type Header is not set.")
26+
}
27+
}

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.1-alpha.2
1+
0.0.1-alpha.3

0 commit comments

Comments
 (0)