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