2015-03-01 19:45:13 +02:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2015-03-09 08:58:10 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-03-01 19:45:13 +02:00
|
|
|
"testing"
|
2015-03-02 20:19:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
user struct {
|
2015-03-09 08:58:10 +02:00
|
|
|
ID string
|
2015-03-02 20:19:30 +02:00
|
|
|
Name string
|
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var u = user{
|
2015-03-09 08:58:10 +02:00
|
|
|
ID: "1",
|
2015-03-01 19:45:13 +02:00
|
|
|
Name: "Joe",
|
|
|
|
}
|
|
|
|
|
2015-03-12 23:03:51 +02:00
|
|
|
func TestMaxParam(t *testing.T) {
|
|
|
|
b := New(MaxParam(8))
|
|
|
|
if b.maxParam != 8 {
|
|
|
|
t.Errorf("max param should be 8, found %d", b.maxParam)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 08:58:10 +02:00
|
|
|
func TestIndex(t *testing.T) {
|
|
|
|
b := New()
|
2015-03-11 02:34:13 +02:00
|
|
|
b.Index("example/public/index.html")
|
2015-03-09 08:58:10 +02:00
|
|
|
r, _ := http.NewRequest("GET", "/", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
b.ServeHTTP(w, r)
|
|
|
|
if w.Code != 200 {
|
|
|
|
t.Errorf("status code should be 200, found %d", w.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatic(t *testing.T) {
|
|
|
|
b := New()
|
2015-03-11 02:34:13 +02:00
|
|
|
b.Static("/js", "example/public/js")
|
|
|
|
r, _ := http.NewRequest("GET", "/js/main.js", nil)
|
2015-03-09 08:58:10 +02:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
b.ServeHTTP(w, r)
|
|
|
|
if w.Code != 200 {
|
|
|
|
t.Errorf("status code should be 200, found %d", w.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-01 19:45:13 +02:00
|
|
|
func verifyUser(rd io.Reader, t *testing.T) {
|
|
|
|
var l int64
|
|
|
|
err := binary.Read(rd, binary.BigEndian, &l) // Body length
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bd := io.LimitReader(rd, l) // Body
|
|
|
|
u2 := new(user)
|
|
|
|
dec := json.NewDecoder(bd)
|
|
|
|
err = dec.Decode(u2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-09 08:58:10 +02:00
|
|
|
if u2.ID != u.ID {
|
|
|
|
t.Errorf("user id should be %s, found %s", u.ID, u2.ID)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
if u2.Name != u.Name {
|
2015-03-09 08:58:10 +02:00
|
|
|
t.Errorf("user name should be %s, found %s", u.Name, u2.Name)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|