Skip to content

Instantly share code, notes, and snippets.

@genedna
Last active August 29, 2015 14:01
Show Gist options
  • Save genedna/33892bbf4c9f7d8cba45 to your computer and use it in GitHub Desktop.
Save genedna/33892bbf4c9f7d8cba45 to your computer and use it in GitHub Desktop.
Martini With Ginkgo
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"net/http"
"strconv"
)
type Application struct {
Name string `json:"name"`
}
func NewApp(app Application, args martini.Params, r render.Render) {
appId, err := strconv.ParseInt(args["app"], 10, 64)
if err == nil {
r.JSON(http.StatusCreated, map[string]interface{}{"app": appId, "name": app.Name})
} else {
r.JSON(http.StatusBadRequest, map[string]interface{}{"error": "The application's id must be numerical."})
}
}
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
"net/http"
)
func main() {
m := martini.Classic()
m.Use(render.Renderer())
m.Group("/v1/apps", func(r martini.Router) {
r.Post("/:app", binding.Json(Application{}), NewApp)
})
http.ListenAndServe(":3000", m)
}
package main
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestOauth(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Main Suite")
}
package main
import (
"bytes"
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"net/http"
"net/http/httptest"
)
var _ = Describe("Martini", func() {
It("POST '/v1/apps/:app' will returns a 201 status code", func() {
m := martini.Classic()
m.Use(render.Renderer())
m.Group("/v1/apps", func(r martini.Router) {
r.Post("/:app", binding.Json(Application{}), NewApp)
})
response := httptest.NewRecorder()
request, _ := http.NewRequest("POST", "/v1/apps/232", bytes.NewReader([]byte("{\"name\":\"martini\"}")))
request.Header.Set("Content-Type", "application/json")
m.ServeHTTP(response, request)
Expect(response.Code).To(Equal(http.StatusCreated))
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment