Last active
May 13, 2020 04:23
-
-
Save novalagung/b741ffa10cb5aca412c4d70c03cb41e9 to your computer and use it in GitHub Desktop.
knotv1-http-test-100-coverage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import ( | |
"github.com/eaciit/knot/knot.v1" | |
"github.com/eaciit/toolkit" | |
) | |
type WorldController struct { | |
} | |
func (w *WorldController) Say(r *knot.WebContext) interface{} { | |
r.Config.OutputType = knot.OutputHtml | |
s := "<b>Hello World</b> " | |
name := r.Query("name") | |
if name != "" { | |
s = s + " " + name | |
} | |
s += "</br>" | |
return s | |
} | |
func (w *WorldController) TestForm1(r *knot.WebContext) interface{} { | |
r.Config.OutputType = knot.OutputHtml | |
m := toolkit.M{} | |
r.GetPayload(&m) | |
return toolkit.JsonString(m) | |
} | |
func (w *WorldController) TestForm2(r *knot.WebContext) interface{} { | |
r.Config.OutputType = knot.OutputHtml | |
data := r.Request.FormValue("data") | |
return data | |
} | |
func (w *WorldController) Json(r *knot.WebContext) interface{} { | |
r.Config.OutputType = knot.OutputJson | |
return struct{ ID, Title string }{"JsonID 00001", "Json Row 01 - Title"} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import ( | |
"bytes" | |
"net/http/httptest" | |
"testing" | |
"github.com/eaciit/toolkit" | |
"github.com/eaciit/knot/knot.v1" | |
. "github.com/smartystreets/goconvey/convey" | |
) | |
func TestSay(t *testing.T) { | |
Convey("testing /say endpoint", t, func() { | |
request := httptest.NewRequest("GET", "/bebas?name=value", nil) | |
ctx := new(knot.WebContext) | |
ctx.Request = request | |
ctx.Writer = httptest.NewRecorder() | |
ctx.Config = knot.NewResponseConfig() | |
ctx.Server = new(knot.Server) | |
result := new(WorldController).Say(ctx) | |
So(result, ShouldEqual, `<b>Hello World</b> value</br>`) | |
}) | |
} | |
func TestTestForm1(t *testing.T) { | |
Convey("testing /testform1 endpoint", t, func() { | |
payload := []byte(`{"some": "payload"}`) | |
request := httptest.NewRequest("POST", "/losss-ga-rewel", bytes.NewBuffer(payload)) | |
request.Header.Set("Content-Type", "application/json") | |
ctx := new(knot.WebContext) | |
ctx.Request = request | |
ctx.Writer = httptest.NewRecorder() | |
ctx.Config = knot.NewResponseConfig() | |
ctx.Server = new(knot.Server) | |
result := new(WorldController).TestForm1(ctx) | |
So(result, ShouldEqual, `{"some":"payload"}`) | |
}) | |
} | |
func TestTestForm2(t *testing.T) { | |
Convey("testing /testform2 endpoint", t, func() { | |
payload := []byte(`data=somevalue`) | |
request := httptest.NewRequest("POST", "/losss-ga-rewel", bytes.NewBuffer(payload)) | |
request.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
ctx := new(knot.WebContext) | |
ctx.Request = request | |
ctx.Writer = httptest.NewRecorder() | |
ctx.Config = knot.NewResponseConfig() | |
ctx.Server = new(knot.Server) | |
result := new(WorldController).TestForm2(ctx) | |
So(result, ShouldEqual, `somevalue`) | |
}) | |
} | |
func TestJson(t *testing.T) { | |
Convey("testing /json endpoint", t, func() { | |
request := httptest.NewRequest("GET", "/losss-ga-rewel", nil) | |
ctx := new(knot.WebContext) | |
ctx.Request = request | |
ctx.Writer = httptest.NewRecorder() | |
ctx.Config = knot.NewResponseConfig() | |
ctx.Server = new(knot.Server) | |
result := new(WorldController).Json(ctx) | |
resultJSONString := toolkit.JsonString(result) | |
So(resultJSONString, ShouldEqual, `{"ID":"JsonID 00001","Title":"Json Row 01 - Title"}`) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment