Skip to content

Commit 4cc6ea4

Browse files
committed
Added test cases for ServeHTTP method
1 parent 7f32a0b commit 4cc6ea4

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

api_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package rest
77

88
import (
9+
"io/ioutil"
10+
"net/http"
11+
"net/http/httptest"
912
"testing"
1013
)
1114

@@ -26,6 +29,12 @@ func validateRoute(fun string, method string, url string, t *testing.T) {
2629
}
2730
}
2831

32+
func TestAPI_Route(t *testing.T) {
33+
a.Route("GET", "/greeting", handle)
34+
35+
validateRoute("Route", "GET", "/greeting", t)
36+
}
37+
2938
func TestAPI_Use(t *testing.T) {
3039
a.Use(handle)
3140

@@ -97,3 +106,30 @@ func TestAPI_Exception(t *testing.T) {
97106
t.Error("API: Exception is not working properly")
98107
}
99108
}
109+
110+
func TestAPI_ServeHTTP(t *testing.T) {
111+
var _api API
112+
113+
_api.Get("/", func(ctx *Context) {
114+
ctx.JSON(`{"message": "Hello World!"}`)
115+
})
116+
117+
dummy := httptest.NewServer(_api)
118+
defer dummy.Close()
119+
120+
res, err := http.Get(dummy.URL)
121+
122+
if err != nil {
123+
t.Error("ServeHTTP error")
124+
}
125+
126+
greeting, err := ioutil.ReadAll(res.Body)
127+
res.Body.Close()
128+
if err != nil {
129+
t.Error("ServeHTTP error")
130+
}
131+
132+
if string(greeting) != `{"message": "Hello World!"}` {
133+
t.Error("Response does not match")
134+
}
135+
}

0 commit comments

Comments
 (0)