diff --git a/src/lib/lib.rs b/src/lib/lib.rs index 25448ea..f098c8c 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -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", } } diff --git a/src/tests/net/json/decode.luau b/src/tests/net/json/decode.luau new file mode 100644 index 0000000..3472051 --- /dev/null +++ b/src/tests/net/json/decode.luau @@ -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 + ) +) diff --git a/src/tests/net/json/encode.luau b/src/tests/net/json/encode.luau new file mode 100644 index 0000000..19fa1fa --- /dev/null +++ b/src/tests/net/json/encode.luau @@ -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") diff --git a/src/tests/net/request/codes.luau b/src/tests/net/request/codes.luau new file mode 100644 index 0000000..72a0c12 --- /dev/null +++ b/src/tests/net/request/codes.luau @@ -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") diff --git a/src/tests/net/request/methods.luau b/src/tests/net/request/methods.luau new file mode 100644 index 0000000..96dc5c9 --- /dev/null +++ b/src/tests/net/request/methods.luau @@ -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") diff --git a/src/tests/net/request/redirect.luau b/src/tests/net/request/redirect.luau new file mode 100644 index 0000000..c3ec663 --- /dev/null +++ b/src/tests/net/request/redirect.luau @@ -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") diff --git a/src/tests/net/request/util.luau b/src/tests/net/request/util.luau new file mode 100644 index 0000000..4c64d8c --- /dev/null +++ b/src/tests/net/request/util.luau @@ -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