diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2018-01-08 16:26:48 -0200 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-01-08 16:26:48 -0200 |
| commit | d80124af9cd929b395b4d4c353b51ccc902a3f9e (patch) | |
| tree | a738a509039b286697819db762916cfda0384413 /spec/util/mock-server.lua | |
| parent | 98a0bd9a97190be8ba10f14a3d67e42de827b04a (diff) | |
| download | luarocks-d80124af9cd929b395b4d4c353b51ccc902a3f9e.tar.gz luarocks-d80124af9cd929b395b4d4c353b51ccc902a3f9e.tar.bz2 luarocks-d80124af9cd929b395b4d4c353b51ccc902a3f9e.zip | |
Reorganize test suite files
Diffstat (limited to 'spec/util/mock-server.lua')
| -rw-r--r-- | spec/util/mock-server.lua | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/spec/util/mock-server.lua b/spec/util/mock-server.lua new file mode 100644 index 00000000..244aceae --- /dev/null +++ b/spec/util/mock-server.lua | |||
| @@ -0,0 +1,97 @@ | |||
| 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 | server:add_resource("/file/{name:[^/]+}", { | ||
| 68 | { | ||
| 69 | method = "GET", | ||
| 70 | path = "/", | ||
| 71 | produces = "text/plain", | ||
| 72 | handler = function(query, name) | ||
| 73 | local fd = io.open("spec/fixtures/"..name, "r") | ||
| 74 | if not fd then | ||
| 75 | return restserver.response():status(404) | ||
| 76 | end | ||
| 77 | local data = fd:read("*a") | ||
| 78 | fd:close() | ||
| 79 | return restserver.response():status(200):entity(data) | ||
| 80 | end | ||
| 81 | } | ||
| 82 | }) | ||
| 83 | |||
| 84 | -- SHUTDOWN this mock-server | ||
| 85 | server:add_resource("/shutdown", { | ||
| 86 | { | ||
| 87 | method = "GET", | ||
| 88 | path = "/", | ||
| 89 | handler = function(query) | ||
| 90 | os.exit() | ||
| 91 | return restserver.response():status(200):entity() | ||
| 92 | end | ||
| 93 | } | ||
| 94 | }) | ||
| 95 | |||
| 96 | -- This loads the restserver.xavante plugin | ||
| 97 | server:enable("restserver.xavante"):start() \ No newline at end of file | ||
