Skip to content

Commit

Permalink
Added tests for router group
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriakh committed Mar 10, 2019
1 parent 8a6443f commit 4b7d283
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

---

**Kira** web framework. Is simply a minimal web framework try to focus in simplicity.
<p align="center">
<p align="center">**Kira** web framework. Simply a minimal web framework.</p>
</p>

## Features

TODO
- **Simplicity** kira is simple. You will find yourself familiar with it quickly.
- **Fast** simplicity comes with speed. Kira is super fast.

## Installation

Expand Down
5 changes: 5 additions & 0 deletions kira_router_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
130 changes: 130 additions & 0 deletions tests/kira_router_group_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 4b7d283

Please sign in to comment.