Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed May 18, 2018
1 parent f4ebe7b commit b27d2d7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
39 changes: 25 additions & 14 deletions app_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package hime

import (
"crypto/tls"
"html/template"
"log"
"net"
"net/http"
"os"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -36,20 +40,6 @@ func TestApp(t *testing.T) {
assert.Len(t, app.templateFuncs, 2)
})

t.Run("Routes", func(t *testing.T) {
app.Routes(Routes{"a": "/b", "b": "/cd"})
assert.Len(t, app.routes, 2)
assert.Equal(t, "/b", app.Route("a"))
assert.Equal(t, "/cd", app.Route("b"))
})

t.Run("Globals", func(t *testing.T) {
app.Globals(Globals{"a": 12, "b": "34"})
assert.Len(t, app.globals, 2)
assert.Equal(t, 12, app.Global("a"))
assert.Equal(t, "34", app.Global("b"))
})

t.Run("GracefulShutdown", func(t *testing.T) {
assert.Nil(t, app.gracefulShutdown)
gs := app.GracefulShutdown()
Expand All @@ -72,3 +62,24 @@ func TestApp(t *testing.T) {
assert.Len(t, gs.notiFns, 3)
})
}

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

app := &App{
TLSConfig: &tls.Config{},
ReadTimeout: 5 * time.Second,
ReadHeaderTimeout: 6 * time.Second,
WriteTimeout: 7 * time.Second,
IdleTimeout: 2 * time.Minute,
MaxHeaderBytes: 1024,
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
ConnState: func(net.Conn, http.ConnState) {},
ErrorLog: log.New(os.Stderr, "", log.LstdFlags),
}

assert.Empty(t, &app.srv)
app.configServer(":8080")
assert.NotEmpty(t, &app.srv)
assert.Equal(t, ":8080", app.srv.Addr)
}
26 changes: 26 additions & 0 deletions request_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hime

import (
"testing"

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

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

cases := []struct {
Input string
Output string
}{
{"", ""},
{"123", "123"},
{"12,345", "12345"},
{" 12, ,, 34,5, ,,", " 12 345 "},
{"12,345.67", "12345.67"},
}

for _, c := range cases {
assert.Equal(t, c.Output, trimComma(c.Input))
}
}
24 changes: 24 additions & 0 deletions route_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package hime

import (
"testing"

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

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

app := New()
assert.Panics(t, func() { app.Route("empty") })

app.Routes(Routes{"a": "/b", "b": "/cd"})
assert.Len(t, app.routes, 2)
assert.Equal(t, "/b", app.Route("a"))
assert.Equal(t, "/cd", app.Route("b"))

assert.Panics(t, func() { app.Route("notexists") })

ctx := appContext{app: app}
assert.Equal(t, "/b", ctx.Route("a"))
}

0 comments on commit b27d2d7

Please sign in to comment.