Skip to content

Instantly share code, notes, and snippets.

@novalagung
Last active May 13, 2020 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save novalagung/b741ffa10cb5aca412c4d70c03cb41e9 to your computer and use it in GitHub Desktop.
Save novalagung/b741ffa10cb5aca412c4d70c03cb41e9 to your computer and use it in GitHub Desktop.
knotv1-http-test-100-coverage
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>&nbsp;"
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"}
}
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>&nbsp; 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