-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjett_test.go
84 lines (61 loc) · 1.46 KB
/
jett_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package jett
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestURLParams(t *testing.T) {
r := New()
r.GET("/home/:param", Home)
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL+"/home/hello", nil)
if err != nil {
t.Fatal(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
// Convert []byte to map
var urlParams map[string]string
json.Unmarshal(body, &urlParams)
if urlParams["param"] != "hello" {
t.Fatalf("URLParams -> Expected : [param] hello")
}
}
func TestGetFullPath(t *testing.T) {
r := New()
expected := "/one"
output := r.getFullPath("/one")
if output != expected {
t.Fatalf("getFullPath -> Expected : %s, Output : %s", expected, output)
}
r.pathPrefix = "/one/"
expected = "/one/two"
output = r.getFullPath("/two")
if output != expected {
t.Fatalf("getFullPath -> Expected : %s, Output : %s", expected, output)
}
}
func TestSubrouter(t *testing.T) {
r := New()
r.GET("/", Home)
sr := r.Subrouter("/about")
sr.GET("/", About)
if sr.pathPrefix != "/about" {
t.Fatalf("Subrouter pathPrefix -> Expected : /about, Output : %s", sr.pathPrefix)
}
}
func Home(w http.ResponseWriter, req *http.Request) {
params := URLParams(req)
JSON(w, params, 200)
}
func About(w http.ResponseWriter, req *http.Request) {
params := QueryParams(req)
JSON(w, params, 200)
}