diff options
Diffstat (limited to 'test/mock-server.lua')
| -rw-r--r-- | test/mock-server.lua | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/test/mock-server.lua b/test/mock-server.lua new file mode 100644 index 00000000..797e2bc5 --- /dev/null +++ b/test/mock-server.lua | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | #!/usr/bin/env lua | ||
| 2 | |||
| 3 | --- A simple LuaRocks mock-server for testing. | ||
| 4 | local restserver = require("restserver") | ||
| 5 | local server = restserver:new():port(8080) | ||
| 6 | |||
| 7 | server:add_resource("api/tool_version", { | ||
| 8 | { | ||
| 9 | method = "GET", | ||
| 10 | path = "/", | ||
| 11 | produces = "application/json", | ||
| 12 | handler = function(query) | ||
| 13 | local json = { version = query.current } | ||
| 14 | return restserver.response():status(200):entity(json) | ||
| 15 | end | ||
| 16 | } | ||
| 17 | }) | ||
| 18 | |||
| 19 | server:add_resource("api/1/{id:[0-9]+}/status", { | ||
| 20 | { | ||
| 21 | method = "GET", | ||
| 22 | path = "/", | ||
| 23 | produces = "application/json", | ||
| 24 | handler = function(query) | ||
| 25 | local json = { user_id = "123", created_at = "29.1.1993" } | ||
| 26 | return restserver.response():status(200):entity(json) | ||
| 27 | end | ||
| 28 | } | ||
| 29 | }) | ||
| 30 | |||
| 31 | server:add_resource("/api/1/{id:[0-9]+}/check_rockspec", { | ||
| 32 | { | ||
| 33 | method = "GET", | ||
| 34 | path = "/", | ||
| 35 | produces = "application/json", | ||
| 36 | handler = function(query) | ||
| 37 | local json = {} | ||
| 38 | return restserver.response():status(200):entity(json) | ||
| 39 | end | ||
| 40 | } | ||
| 41 | }) | ||
| 42 | |||
| 43 | server:add_resource("/api/1/{id:[0-9]+}/upload", { | ||
| 44 | { | ||
| 45 | method = "POST", | ||
| 46 | path = "/", | ||
| 47 | produces = "application/json", | ||
| 48 | handler = function(query) | ||
| 49 | local json = {module = "luasocket", version = {id = "1.0"}, module_url = "http://localhost/luasocket", manifests = "root", is_new = "is_new"} | ||
| 50 | return restserver.response():status(200):entity(json) | ||
| 51 | end | ||
| 52 | } | ||
| 53 | }) | ||
| 54 | |||
| 55 | server:add_resource("/api/1/{id:[0-9]+}/upload_rock/{id:[0-9]+}", { | ||
| 56 | { | ||
| 57 | method = "POST", | ||
| 58 | path = "/", | ||
| 59 | produces = "application/json", | ||
| 60 | handler = function(query) | ||
| 61 | local json = {"rock","module_url"} | ||
| 62 | return restserver.response():status(200):entity(json) | ||
| 63 | end | ||
| 64 | } | ||
| 65 | }) | ||
| 66 | |||
| 67 | -- SHUTDOWN this mock-server | ||
| 68 | server:add_resource("/shutdown", { | ||
| 69 | { | ||
| 70 | method = "GET", | ||
| 71 | path = "/", | ||
| 72 | handler = function(query) | ||
| 73 | os.exit() | ||
| 74 | return restserver.response():status(200):entity() | ||
| 75 | end | ||
| 76 | } | ||
| 77 | }) | ||
| 78 | |||
| 79 | -- This loads the restserver.xavante plugin | ||
| 80 | server:enable("restserver.xavante"):start() \ No newline at end of file | ||
