mirror of
https://github.com/lune-org/lune.git
synced 2024-12-13 13:30:38 +00:00
Implement test suite for net
This commit is contained in:
parent
9e4ad3fd84
commit
b64990490d
7 changed files with 120 additions and 1 deletions
|
@ -83,7 +83,7 @@ mod tests {
|
|||
.await
|
||||
.unwrap();
|
||||
if let Err(e) = lune.run_with_name(&script, $value).await {
|
||||
panic!("{}", e.to_string())
|
||||
panic!("Test '{}' failed!\n{}", $value, e.to_string())
|
||||
}
|
||||
}
|
||||
)*
|
||||
|
@ -94,5 +94,10 @@ mod tests {
|
|||
process_args: "process/args",
|
||||
process_env: "process/env",
|
||||
process_spawn: "process/spawn",
|
||||
net_request_codes: "net/request/codes",
|
||||
net_request_methods: "net/request/methods",
|
||||
net_request_redirect: "net/request/redirect",
|
||||
net_json_decode: "net/json/decode",
|
||||
net_json_encode: "net/json/encode",
|
||||
}
|
||||
}
|
||||
|
|
50
src/tests/net/json/decode.luau
Normal file
50
src/tests/net/json/decode.luau
Normal file
|
@ -0,0 +1,50 @@
|
|||
type Response = {
|
||||
products: {
|
||||
{
|
||||
id: number,
|
||||
title: string,
|
||||
description: string,
|
||||
price: number,
|
||||
discountPercentage: number,
|
||||
rating: number,
|
||||
stock: number,
|
||||
brand: string,
|
||||
category: string,
|
||||
thumbnail: string,
|
||||
images: { string },
|
||||
}
|
||||
},
|
||||
total: number,
|
||||
skip: number,
|
||||
limit: number,
|
||||
}
|
||||
|
||||
local response = net.request("https://dummyjson.com/products")
|
||||
|
||||
assert(response.ok, "Dummy JSON api returned an error")
|
||||
assert(#response.body > 0, "Dummy JSON api returned empty body")
|
||||
|
||||
local data: Response = net.jsonDecode(response.body)
|
||||
|
||||
assert(type(data.limit) == "number", "Products limit was not a number")
|
||||
assert(type(data.products) == "table", "Products was not a table")
|
||||
assert(#data.products > 0, "Products table was empty")
|
||||
|
||||
local productCount = 0
|
||||
for _, product in data.products do
|
||||
productCount += 1
|
||||
assert(type(product.id) == "number", "Product id was not a number")
|
||||
assert(type(product.title) == "string", "Product title was not a number")
|
||||
assert(type(product.description) == "string", "Product description was not a number")
|
||||
assert(type(product.images) == "table", "Product images was not a table")
|
||||
assert(#product.images > 0, "Product images table was empty")
|
||||
end
|
||||
|
||||
assert(
|
||||
data.limit == productCount,
|
||||
string.format(
|
||||
"Products limit and number of products in array mismatch (expected %d, got %d)",
|
||||
data.limit,
|
||||
productCount
|
||||
)
|
||||
)
|
25
src/tests/net/json/encode.luau
Normal file
25
src/tests/net/json/encode.luau
Normal file
|
@ -0,0 +1,25 @@
|
|||
local payload = [[{
|
||||
"Hello": "World",
|
||||
"Inner": {
|
||||
"Array": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]
|
||||
},
|
||||
"Foo": "Bar"
|
||||
}]]
|
||||
|
||||
local decoded = net.jsonDecode(payload)
|
||||
|
||||
assert(type(decoded) == "table", "Decoded payload was not a table")
|
||||
assert(decoded.Hello == "World", "Decoded payload Hello was not World")
|
||||
assert(type(decoded.Inner) == "table", "Decoded payload Inner was not a table")
|
||||
assert(type(decoded.Inner.Array) == "table", "Decoded payload Inner.Array was not a table")
|
||||
assert(type(decoded.Inner.Array[1]) == "number", "Decoded payload Inner.Array[1] was not a number")
|
||||
assert(type(decoded.Inner.Array[2]) == "number", "Decoded payload Inner.Array[2] was not a number")
|
||||
assert(type(decoded.Inner.Array[3]) == "number", "Decoded payload Inner.Array[3] was not a number")
|
||||
assert(decoded.Foo == "Bar", "Decoded payload Foo was not Bar")
|
||||
|
||||
local encoded = net.jsonEncode(decoded, true)
|
||||
assert(encoded == payload, "JSON round-trip did not produce the same result")
|
6
src/tests/net/request/codes.luau
Normal file
6
src/tests/net/request/codes.luau
Normal file
|
@ -0,0 +1,6 @@
|
|||
local util = require("src/tests/net/request/util")
|
||||
local pass, fail = util.pass, util.fail
|
||||
|
||||
pass("GET", "https://httpbin.org/status/200", "Request status code - 200")
|
||||
fail("GET", "https://httpbin.org/status/400", "Request status code - 400")
|
||||
fail("GET", "https://httpbin.org/status/500", "Request status code - 500")
|
9
src/tests/net/request/methods.luau
Normal file
9
src/tests/net/request/methods.luau
Normal file
|
@ -0,0 +1,9 @@
|
|||
local util = require("src/tests/net/request/util")
|
||||
local pass = util.pass
|
||||
|
||||
-- stylua: ignore start
|
||||
pass("GET", "https://httpbin.org/get", "Request method - GET")
|
||||
pass("POST", "https://httpbin.org/post", "Request method - POST")
|
||||
pass("PATCH", "https://httpbin.org/patch", "Request method - PATCH")
|
||||
pass("PUT", "https://httpbin.org/put", "Request method - PUT")
|
||||
pass("DELETE", "https://httpbin.org/delete", "Request method - DELETE")
|
4
src/tests/net/request/redirect.luau
Normal file
4
src/tests/net/request/redirect.luau
Normal file
|
@ -0,0 +1,4 @@
|
|||
local util = require("src/tests/net/request/util")
|
||||
local pass = util.pass
|
||||
|
||||
pass("GET", "https://httpbin.org/absolute-redirect/3", "Redirect 3 times")
|
20
src/tests/net/request/util.luau
Normal file
20
src/tests/net/request/util.luau
Normal file
|
@ -0,0 +1,20 @@
|
|||
local util = {}
|
||||
|
||||
function util.pass(method, url, message: string)
|
||||
local response = net.request({
|
||||
method = method,
|
||||
url = url,
|
||||
})
|
||||
assert(response.ok, message)
|
||||
end
|
||||
|
||||
function util.fail(method, url, message: string)
|
||||
local response = net.request({
|
||||
method = method,
|
||||
url = url,
|
||||
})
|
||||
console.log(response)
|
||||
assert(not response.ok, message)
|
||||
end
|
||||
|
||||
return util
|
Loading…
Reference in a new issue