From 4b7d283c9e7bf2c271bd5586e5912947face66ab Mon Sep 17 00:00:00 2001 From: Lafriakh Rachid Date: Sun, 10 Mar 2019 12:42:44 +0000 Subject: [PATCH] Added tests for router group --- README.md | 7 +- kira_router_group.go | 5 ++ tests/kira_router_group_test.go | 130 ++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 tests/kira_router_group_test.go diff --git a/README.md b/README.md index b47e698..76e20e9 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,14 @@ --- -**Kira** web framework. Is simply a minimal web framework try to focus in simplicity. +

+

**Kira** web framework. Simply a minimal web framework.

+

## Features -TODO +- **Simplicity** kira is simple. You will find yourself familiar with it quickly. +- **Fast** simplicity comes with speed. Kira is super fast. ## Installation diff --git a/kira_router_group.go b/kira_router_group.go index 17c2e34..863a955 100644 --- a/kira_router_group.go +++ b/kira_router_group.go @@ -57,6 +57,11 @@ func (g Group) Delete(path string, handler HandlerFunc, middlewares ...Middlewar g.app.Delete(g.path(path), handler, middlewares...) } +// Options is a shortcut for app.Delete with the group prefix. +func (g Group) Options(path string, handler HandlerFunc, middlewares ...Middleware) { + g.app.Options(g.path(path), handler, middlewares...) +} + // ServeFiles is a shortcut for app.ServeFiles with the group prefix. func (g Group) ServeFiles(path string, root http.FileSystem) { g.app.ServeFiles(g.path(path), root) diff --git a/tests/kira_router_group_test.go b/tests/kira_router_group_test.go new file mode 100644 index 0000000..f76d28d --- /dev/null +++ b/tests/kira_router_group_test.go @@ -0,0 +1,130 @@ +package tests + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-kira/kira" +) + +func TestRouterGroup(t *testing.T) { + app := kira.New() + mfs := &mockFileSystem{} + + app.Group("prefix", func(g kira.Group) { + g.Get("/get", func(c *kira.Context) { + c.String("get") + }) + g.Head("/head", func(c *kira.Context) { + // The body should be empty + }) + g.Post("/post", func(c *kira.Context) { + c.String("post") + }) + g.Put("/put", func(c *kira.Context) { + c.String("put") + }) + g.Patch("/patch", func(c *kira.Context) { + c.String("patch") + }) + g.Options("/options", func(c *kira.Context) { + // The body should be empty + }) + + g.ServeFiles("/files/*filepath", mfs) + }) + + // Server + s := httptest.NewServer(app.RegisterRoutes()) + + // === GET === + res, _ := http.Get(url(s, "/prefix/get")) + defer res.Body.Close() + content := contentS(res.Body) + + // Assert + if res.StatusCode != http.StatusOK { + t.Errorf("expect stauts: `202`, have: %d", res.StatusCode) + } + if content != "get" { + t.Errorf("expect: `get`, have: %s", content) + } + + // === HEAD === + resHEAD, _ := http.Head(url(s, "/prefix/head")) + defer resHEAD.Body.Close() + content = contentS(resHEAD.Body) + + // Assert + if resHEAD.StatusCode != http.StatusOK { + t.Errorf("expect stauts: `202`, have: %d", resHEAD.StatusCode) + } + + // === POST === + resPOST, _ := http.Post(url(s, "/prefix/post"), "application/json", nil) + defer resPOST.Body.Close() + content = contentS(resPOST.Body) + + // Assert + if resPOST.StatusCode != http.StatusOK { + t.Errorf("expect stauts: `202`, have: %d", resPOST.StatusCode) + } + if content != "post" { + t.Errorf("expect: `post`, have: %s", content) + } + + // === Put === + reqPut, _ := http.NewRequest(http.MethodPut, url(s, "/prefix/put"), nil) + reqPut.Header.Set("Content-Type", "application/json; charset=utf-8") + resPut, _ := http.DefaultClient.Do(reqPut) + defer resPut.Body.Close() + content = contentS(resPut.Body) + + // Assert + if resPut.StatusCode != http.StatusOK { + t.Errorf("expect stauts: `202`, have: %d", resPut.StatusCode) + } + if content != "put" { + t.Errorf("expect: `put`, have: %s", content) + } + + // === Patch === + reqPatch, _ := http.NewRequest(http.MethodPatch, url(s, "/prefix/patch"), nil) + reqPatch.Header.Set("Content-Type", "application/json; charset=utf-8") + resPatch, _ := http.DefaultClient.Do(reqPatch) + defer resPatch.Body.Close() + content = contentS(resPatch.Body) + + // Assert + if resPatch.StatusCode != http.StatusOK { + t.Errorf("expect stauts: `202`, have: %d", resPatch.StatusCode) + } + if content != "patch" { + t.Errorf("expect: `patch`, have: %s", content) + } + + // === Options === + reqOptions, _ := http.NewRequest(http.MethodOptions, url(s, "/prefix/options"), nil) + reqOptions.Header.Set("Content-Type", "application/json; charset=utf-8") + resOptions, _ := http.DefaultClient.Do(reqOptions) + defer resOptions.Body.Close() + content = contentS(resOptions.Body) + + // Assert + if resOptions.StatusCode != http.StatusOK { + t.Errorf("expect stauts: `202`, have: %d", resOptions.StatusCode) + } + + // === ServeFiles === + resServFiles, _ := http.Get(url(s, "/prefix/files/favicon.ico")) + defer resServFiles.Body.Close() + + // Assert + if resServFiles.StatusCode == http.StatusOK { + t.Errorf("expect stauts: `202`, have: %d", resServFiles.StatusCode) + } + if !mfs.opened { + t.Error("serving file failed") + } +}