Skip to content

Commit

Permalink
Merge pull request #25 from acoshift/dev
Browse files Browse the repository at this point in the history
add app template, change context, add tests
  • Loading branch information
acoshift committed May 15, 2018
2 parents da1b99c + 80510dd commit 3cf6106
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 7 deletions.
4 changes: 3 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hime
import (
"context"
"crypto/tls"
"html/template"
"log"
"mime"
"net"
Expand Down Expand Up @@ -47,7 +48,8 @@ type App struct {
globals Globals
beforeRender middleware.Middleware

template map[string]*tmpl
template map[string]*tmpl
templateFunc []template.FuncMap

graceful struct {
timeout time.Duration
Expand Down
25 changes: 22 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hime
import (
"context"
"net/http"
"time"
)

// NewContext creates new hime's context
Expand All @@ -19,19 +20,37 @@ func newInternalContext(w http.ResponseWriter, r *http.Request) *appContext {
}

func newContext(app *App, w http.ResponseWriter, r *http.Request) *appContext {
return &appContext{r.Context(), app, r, w, 0}
return &appContext{app, r, w, 0}
}

type appContext struct {
context.Context

app *App
r *http.Request
w http.ResponseWriter

code int
}

// Deadline implements context.Context
func (ctx *appContext) Deadline() (deadline time.Time, ok bool) {
return ctx.r.Context().Deadline()
}

// Done implements context.Context
func (ctx *appContext) Done() <-chan struct{} {
return ctx.r.Context().Done()
}

// Err implements context.Context
func (ctx *appContext) Err() error {
return ctx.r.Context().Err()
}

// Value implements context.Context
func (ctx *appContext) Value(key interface{}) interface{} {
return ctx.r.Context().Value(key)
}

func (ctx *appContext) WithContext(nctx context.Context) {
ctx.r = ctx.r.WithContext(nctx)
}
Expand Down
41 changes: 41 additions & 0 deletions context_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package hime

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestContext(t *testing.T) {
t.Parallel()

app := New()
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
ctx := newContext(app, w, r)

assert.Equal(t, r, ctx.Request())
assert.Equal(t, w, ctx.ResponseWriter())
ctx.Status(500)
assert.Equal(t, 500, ctx.code)
assert.Equal(t, &Param{Name: "a", Value: 1}, ctx.Param("a", 1))

nctx := context.WithValue(ctx, "a", "b")
ctx.WithContext(nctx)
assert.Equal(t, nctx, ctx.Request().Context())
assert.Equal(t, "b", ctx.Value("a"))

ctx.WithValue("t", "p")
assert.Equal(t, "p", ctx.Value("t"))

nr := httptest.NewRequest(http.MethodPost, "/test", nil)
ctx.WithRequest(nr)
assert.Equal(t, nr, ctx.Request())

nw := httptest.NewRecorder()
ctx.WithResponseWriter(nw)
assert.Equal(t, nw, ctx.ResponseWriter())
}
18 changes: 18 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package hime_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/acoshift/hime"
"github.com/stretchr/testify/assert"
)

func TestNewContext(t *testing.T) {
t.Run("NotPassFromApp", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
assert.Panics(t, func() { hime.NewContext(w, r) })
})
}
25 changes: 25 additions & 0 deletions error_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hime

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestErrors(t *testing.T) {
t.Parallel()

var err error

err = newErrRouteNotFound("route123")
assert.IsType(t, &ErrRouteNotFound{}, err)
assert.Contains(t, err.Error(), "route123")

err = newErrTemplateDuplicate("temp123")
assert.IsType(t, &ErrTemplateDuplicate{}, err)
assert.Contains(t, err.Error(), "temp123")

err = newErrTemplateNotFound("temp123")
assert.IsType(t, &ErrTemplateNotFound{}, err)
assert.Contains(t, err.Error(), "temp123")
}
2 changes: 2 additions & 0 deletions global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestGlobal(t *testing.T) {
t.Parallel()

app := hime.New()

assert.Nil(t, app.Global("key1"))
Expand Down
5 changes: 4 additions & 1 deletion hime.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,7 @@ type Param struct {
Value interface{}
}

var _ = Context(&appContext{})
var (
_ = Context(&appContext{})
_ = context.Context(&appContext{})
)
6 changes: 6 additions & 0 deletions path_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestBuildPath(t *testing.T) {
t.Parallel()

cases := []struct {
Input string
Output string
Expand All @@ -30,6 +32,8 @@ func TestBuildPath(t *testing.T) {
}

func TestBuildPathParams(t *testing.T) {
t.Parallel()

cases := []struct {
Base string
Params []interface{}
Expand All @@ -54,6 +58,8 @@ func TestBuildPathParams(t *testing.T) {
}

func TestSafeRedirectPath(t *testing.T) {
t.Parallel()

cases := []struct {
Input string
Output string
Expand Down
10 changes: 8 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ func (app *App) Template() *Template {
}
return &Template{
list: app.template,
funcs: []template.FuncMap{template.FuncMap{
funcs: append([]template.FuncMap{template.FuncMap{
"route": app.Route,
"global": app.Global,
}},
}}, app.templateFunc...),
}
}

// TemplateFuncs registers app's level template funcs
func (app *App) TemplateFuncs(funcs ...template.FuncMap) *App {
app.templateFunc = append(app.templateFunc, funcs...)
return app
}

type tmpl struct {
template.Template
m *minify.M
Expand Down

0 comments on commit 3cf6106

Please sign in to comment.