|
| 1 | +package rest |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +var _api *api |
| 10 | +var _list *list |
| 11 | +var _handler *handler |
| 12 | +var task = func(ctx Context) {} |
| 13 | +var errTask = func(err error, ctx Context) {} |
| 14 | + |
| 15 | +func init() { |
| 16 | + _list = new(list) |
| 17 | + |
| 18 | + _handler = &handler{ |
| 19 | + list: _list, |
| 20 | + } |
| 21 | + |
| 22 | + _api = &api{ |
| 23 | + prefix: trim("/"), |
| 24 | + list: _list, |
| 25 | + handler: _handler, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestApi_Use(t *testing.T) { |
| 30 | + _api.Use(task) |
| 31 | + |
| 32 | + if !(len(_list.middlewares) == 1 && len(_list.routes) == 0 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) { |
| 33 | + t.Error("api.Use should add middleware in the list") |
| 34 | + } |
| 35 | + |
| 36 | + if _list.middlewares[0].pattern == nil || _list.middlewares[0].task == nil { |
| 37 | + t.Error("api.Use should add type of middleware with pattern and task") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestApi_Get(t *testing.T) { |
| 42 | + _api.Get("/", task) |
| 43 | + |
| 44 | + if !(len(_list.middlewares) == 1 && len(_list.routes) == 1 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) { |
| 45 | + t.Error("api.Get should add route in the list") |
| 46 | + } |
| 47 | + |
| 48 | + if _list.routes[0].method != http.MethodGet || _list.routes[0].pattern == nil || _list.routes[0].task == nil { |
| 49 | + t.Error("api.Get should add type of route with method, pattern and task") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestApi_Post(t *testing.T) { |
| 54 | + _api.Post("/", task) |
| 55 | + |
| 56 | + if !(len(_list.middlewares) == 1 && len(_list.routes) == 2 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) { |
| 57 | + t.Error("api.Post should add route in the list") |
| 58 | + } |
| 59 | + |
| 60 | + if _list.routes[1].method != http.MethodPost || _list.routes[1].pattern == nil || _list.routes[1].task == nil { |
| 61 | + t.Error("api.Post should add type of route with method, pattern and task") |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestApi_Put(t *testing.T) { |
| 66 | + _api.Put("/", task) |
| 67 | + |
| 68 | + if !(len(_list.middlewares) == 1 && len(_list.routes) == 3 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) { |
| 69 | + t.Error("api.Put should add route in the list") |
| 70 | + } |
| 71 | + |
| 72 | + if _list.routes[2].method != http.MethodPut || _list.routes[2].pattern == nil || _list.routes[2].task == nil { |
| 73 | + t.Error("api.Put should add type of route with method, pattern and task") |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestApi_Delete(t *testing.T) { |
| 78 | + _api.Delete("/", task) |
| 79 | + |
| 80 | + if !(len(_list.middlewares) == 1 && len(_list.routes) == 4 && len(_list.exceptions) == 0 && _list.uncaughtException == nil) { |
| 81 | + t.Error("api.Delete should add route in the list") |
| 82 | + } |
| 83 | + |
| 84 | + if _list.routes[3].method != http.MethodDelete || _list.routes[3].pattern == nil || _list.routes[3].task == nil { |
| 85 | + t.Error("api.Delete should add type of route with method, pattern and task") |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestApi_Group(t *testing.T) { |
| 90 | + user := _api.Group("/user") |
| 91 | + |
| 92 | + if user == nil { |
| 93 | + t.Error("api.Group should return api reference") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestApi_Exception(t *testing.T) { |
| 98 | + _api.Exception(ErrCodeNotFound, errTask) |
| 99 | + |
| 100 | + if !(len(_list.middlewares) == 1 && len(_list.routes) == 4 && len(_list.exceptions) == 1 && _list.uncaughtException == nil) { |
| 101 | + t.Error("api.Exception should add exception handler in the list") |
| 102 | + } |
| 103 | + |
| 104 | + if _list.exceptions[0].code != ErrCodeNotFound || _list.exceptions[0].task == nil { |
| 105 | + t.Error("api.Exception should add type of exception with code and task") |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestApi_UncaughtException(t *testing.T) { |
| 110 | + _api.UncaughtException(errTask) |
| 111 | + |
| 112 | + if !(len(_list.middlewares) == 1 && len(_list.routes) == 4 && len(_list.exceptions) == 1 && _list.uncaughtException != nil) { |
| 113 | + t.Error("api.UncaughtException should add uncaught exception handler in the list") |
| 114 | + } |
| 115 | + |
| 116 | + if _list.uncaughtException == nil { |
| 117 | + t.Error("api.UncaughtException should add type of uncaughtException with task") |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestApi_ServeHTTP(t *testing.T) { |
| 122 | + r := httptest.NewRequest(http.MethodGet, "/", nil) |
| 123 | + w := httptest.NewRecorder() |
| 124 | + |
| 125 | + _api.ServeHTTP(w, r) |
| 126 | + |
| 127 | + if w.Result().StatusCode != 200 { |
| 128 | + t.Error("api.ServeHTTP should respond on every request") |
| 129 | + } |
| 130 | +} |
0 commit comments