Skip to content

Commit

Permalink
Merge branch 'release-0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Aug 19, 2014
2 parents ed4063e + c20fb73 commit 12e2f88
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

/*
Package router 0.1.0 provides fast HTTP request router.
Package router 0.1.1 provides fast HTTP request router.
The router matches incoming requests by the request method and the path.
If a handle is registered for this path and method, the router delegates the
Expand Down
92 changes: 89 additions & 3 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,46 @@
package router

import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"testing"
)

func TestRouterSimplestUsing(t *testing.T) {
func TestRouterRegisterHandlers(t *testing.T) {
r := New()
r.GET("/hello", func(c *Control) {
c.Body("Hello")
})
r.GET("/hello/:name", func(c *Control) {
c.Body("Hello " + c.Get(":name"))
})
r.GET("/users/:name", func(c *Control) {
c.Body("Users: " + c.Get(":name") + " " + c.Get("name"))
})
r.POST("/users", func(c *Control) {
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
t.Error(err)
}
var values map[string]string
if err := json.Unmarshal(body, &values); err != nil {
t.Error(err)
}
c.Body("User: " + values["name"])
})
r.PUT("/users", func(c *Control) {
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
t.Error(err)
}
var values map[string]string
if err := json.Unmarshal(body, &values); err != nil {
t.Error(err)
}
c.Body("Users: " + values["name1"] + " " + values["name2"])
})
go r.Listen(":8888")
if handle, params, ok := r.Lookup("GET", "/hello"); ok {
if handle == nil {
Expand Down Expand Up @@ -45,6 +72,9 @@ func TestRouterSimplestUsing(t *testing.T) {
} else {
t.Error("Path not fouund: /hello/John")
}
}

func TestRouterGetStatic(t *testing.T) {
response, err := http.Get("http://localhost:8888/hello")
if err != nil {
t.Error(err)
Expand All @@ -57,11 +87,14 @@ func TestRouterSimplestUsing(t *testing.T) {
if string(body) != "Hello" {
t.Error("Expected", "Hello", "got", string(body))
}
response, err = http.Get("http://localhost:8888/hello/John")
}

func TestRouterGetParameter(t *testing.T) {
response, err := http.Get("http://localhost:8888/hello/John")
if err != nil {
t.Error(err)
}
body, err = ioutil.ReadAll(response.Body)
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
t.Error(err)
Expand All @@ -70,3 +103,56 @@ func TestRouterSimplestUsing(t *testing.T) {
t.Error("Expected", "Hello John", "got", string(body))
}
}

func TestRouterGetParameterFromClassicUrl(t *testing.T) {
response, err := http.Get("http://localhost:8888/users/Jane/?name=Joe")
if err != nil {
t.Error(err)
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
t.Error(err)
}
if string(body) != "Users: Jane Joe" {
t.Error("Expected", "Users: Jane Joe", "got", string(body))
}
}

func TestRouterPostJSONData(t *testing.T) {
reader := strings.NewReader(`{"name": "Tom"}`)
response, err := http.Post("http://localhost:8888/users/", MIMEJSON, reader)
if err != nil {
t.Error(err)
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
t.Error(err)
}
if string(body) != "User: Tom" {
t.Error("Expected", "User: Tom", "got", string(body))
}

}

func TestRouterPutJSONData(t *testing.T) {
reader := strings.NewReader(`{"name1": "user1", "name2": "user2"}`)
client := new(http.Client)
req, err := http.NewRequest("PUT", "http://localhost:8888/users/", reader)
if err != nil {
t.Error(err)
}
response, err := client.Do(req)
if err != nil {
t.Error(err)
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
t.Error(err)
}
if string(body) != "Users: user1 user2" {
t.Error("Expected", "Users: user1 user2", "got", string(body))
}
}

0 comments on commit 12e2f88

Please sign in to comment.