|
| 1 | +/*! |
| 2 | + * rest-api-framework |
| 3 | + * Copyright(c) 2019 Roshan Gade |
| 4 | + * MIT Licensed |
| 5 | + */ |
| 6 | +package rest |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +var api API |
| 13 | +var ns Namespace |
| 14 | +var handle Handler |
| 15 | + |
| 16 | +func TestNamespace_Set(t *testing.T) { |
| 17 | + ns.Set("/test", &api) |
| 18 | + |
| 19 | + if ns.prefix != "/test" { |
| 20 | + t.Error("Prefix is not set.") |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func validateNsRoute(fun string, method string, url string, t *testing.T) { |
| 25 | + flag := true |
| 26 | + for _, route := range api.routes { |
| 27 | + if route.method == method && route.pattern == url { |
| 28 | + flag = false |
| 29 | + break |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if flag { |
| 34 | + t.Error("Namespace: " + fun + " is not working properly") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestNamespace_Use(t *testing.T) { |
| 39 | + ns.Use(handle) |
| 40 | + |
| 41 | + validateNsRoute("Use", "", "/test/*", t) |
| 42 | +} |
| 43 | + |
| 44 | +func TestNamespace_All(t *testing.T) { |
| 45 | + ns.All("/:uid", handle) |
| 46 | + |
| 47 | + validateNsRoute("All", "", "/test/:uid", t) |
| 48 | +} |
| 49 | + |
| 50 | +func TestNamespace_Get(t *testing.T) { |
| 51 | + ns.Get("/:uid", handle) |
| 52 | + |
| 53 | + validateNsRoute("Get", "GET", "/test/:uid", t) |
| 54 | +} |
| 55 | + |
| 56 | +func TestNamespace_Post(t *testing.T) { |
| 57 | + ns.Post("/:uid", handle) |
| 58 | + |
| 59 | + validateNsRoute("Post", "POST", "/test/:uid", t) |
| 60 | +} |
| 61 | + |
| 62 | +func TestNamespace_Put(t *testing.T) { |
| 63 | + ns.Put("/:uid", handle) |
| 64 | + |
| 65 | + validateNsRoute("Put", "PUT", "/test/:uid", t) |
| 66 | +} |
| 67 | + |
| 68 | +func TestNamespace_Delete(t *testing.T) { |
| 69 | + ns.Delete("/:uid", handle) |
| 70 | + |
| 71 | + validateNsRoute("Delete", "DELETE", "/test/:uid", t) |
| 72 | +} |
| 73 | + |
| 74 | +func TestNamespace_Options(t *testing.T) { |
| 75 | + ns.Options("/:uid", handle) |
| 76 | + |
| 77 | + validateNsRoute("Options", "OPTIONS", "/test/:uid", t) |
| 78 | +} |
| 79 | + |
| 80 | +func TestNamespace_Head(t *testing.T) { |
| 81 | + ns.Head("/:uid", handle) |
| 82 | + |
| 83 | + validateNsRoute("Head", "HEAD", "/test/:uid", t) |
| 84 | +} |
| 85 | + |
| 86 | +func TestNamespace_Patch(t *testing.T) { |
| 87 | + ns.Patch("/:uid", handle) |
| 88 | + |
| 89 | + validateNsRoute("Patch", "PATCH", "/test/:uid", t) |
| 90 | +} |
| 91 | + |
| 92 | +func TestNamespace_Exception(t *testing.T) { |
| 93 | + ns.Exception("UID_NOT_FOUND", handle) |
| 94 | + |
| 95 | + flag := true |
| 96 | + for _, route := range api.exceptions { |
| 97 | + if route.message == "UID_NOT_FOUND" { |
| 98 | + flag = false |
| 99 | + break |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if flag { |
| 104 | + t.Error("Namespace: Exception is not working properly") |
| 105 | + } |
| 106 | +} |
0 commit comments