-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathramchi_test.go
83 lines (66 loc) · 1.82 KB
/
ramchi_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
package ramchi
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/Etwodev/ramchi/router"
)
func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io.Reader) (*http.Response, string) {
req, err := http.NewRequest(method, ts.URL+path, body)
if err != nil {
t.Fatal(err)
return nil, ""
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
return nil, ""
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
return nil, ""
}
defer resp.Body.Close()
return resp, string(respBody)
}
func TestBasicServer(t *testing.T) {
const ERROR_STATUS_CODE = 418
const ERROR_MESSAGE = "Example error has occurred"
const ERROR_RESPONSE = "test error pass-through"
ts := New()
pingAll := func(w http.ResponseWriter, r *http.Request) {
res, _ := json.Marshal(map[string]string{"success": "ping"})
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(201)
if _, err := w.Write(res); err != nil {
t.Fatal(err)
}
}
errorAll := func(w http.ResponseWriter, r *http.Request) {
Handle(w, "errorAll", errors.New(ERROR_RESPONSE), ERROR_MESSAGE, ERROR_STATUS_CODE)
}
testRoutes := func() []router.Route {
return []router.Route{
router.NewGetRoute("/ping", true, false, pingAll),
router.NewGetRoute("/error", true, false, errorAll),
}
}
testRouters := func() []router.Router {
return []router.Router{
router.NewRouter(testRoutes(), true),
}
}
ts.LoadRouter(testRouters())
instance := httptest.NewServer(ts.handler())
defer instance.Close()
if _, body := testRequest(t, instance, http.MethodGet, "/ping", nil); body != `{"success":"ping"}` {
t.Fatalf(body)
}
if _, body := testRequest(t, instance, http.MethodGet, "/error", nil); body != "I'm a teapot\u000a" {
t.Fatalf(body)
}
}