馃 A practical web framework for Gleam
1import app/router
2import gleam/json
3import gleeunit
4import gleeunit/should
5import wisp/testing
6
7pub fn main() {
8 gleeunit.main()
9}
10
11pub fn get_test() {
12 let response = router.handle_request(testing.get("/", []))
13
14 response.status
15 |> should.equal(405)
16}
17
18pub fn submit_wrong_content_type_test() {
19 let response = router.handle_request(testing.post("/", [], ""))
20
21 response.status
22 |> should.equal(415)
23
24 response.headers
25 |> should.equal([#("accept", "application/json")])
26}
27
28pub fn submit_missing_parameters_test() {
29 let json = json.object([#("name", json.string("Joe"))])
30
31 // The `METHOD_json` functions are used to create a request with a JSON body,
32 // with the appropriate `content-type` header.
33 let response =
34 testing.post_json("/", [], json)
35 |> router.handle_request()
36
37 response.status
38 |> should.equal(422)
39}
40
41pub fn submit_successful_test() {
42 let json =
43 json.object([#("name", json.string("Joe")), #("is-cool", json.bool(True))])
44 let response =
45 testing.post_json("/", [], json)
46 |> router.handle_request()
47
48 response.status
49 |> should.equal(201)
50
51 response.headers
52 |> should.equal([#("content-type", "application/json; charset=utf-8")])
53
54 response
55 |> testing.string_body
56 |> should.equal("{\"name\":\"Joe\",\"is-cool\":true,\"saved\":true}")
57}