🧚 A practical web framework for Gleam

Redirects

+51
+32
src/wisp.gleam
··· 305 305 HttpResponse(202, [], Empty) 306 306 } 307 307 308 + /// Create an empty response with status code 303: See Other, and the `location` 309 + /// header set to the given URL. Used to redirect the client to another page. 310 + /// 311 + /// # Examples 312 + /// 313 + /// ```gleam 314 + /// redirect(to: "https://example.com") 315 + /// // -> Response(303, [#("location", "https://example.com")], Empty) 316 + /// ``` 317 + /// 318 + pub fn redirect(to url: String) -> Response { 319 + HttpResponse(303, [#("location", url)], Empty) 320 + } 321 + 322 + /// Create an empty response with status code 308: Moved Permanently, and the 323 + /// `location` header set to the given URL. Used to redirect the client to 324 + /// another page. 325 + /// 326 + /// This redirect is permanent and the client is expected to cache the new 327 + /// location, using it for future requests. 328 + /// 329 + /// # Examples 330 + /// 331 + /// ```gleam 332 + /// moved_permanently(to: "https://example.com") 333 + /// // -> Response(308, [#("location", "https://example.com")], Empty) 334 + /// ``` 335 + /// 336 + pub fn moved_permanently(to url: String) -> Response { 337 + HttpResponse(308, [#("location", url)], Empty) 338 + } 339 + 308 340 /// Create an empty response with status code 204: No content. 309 341 /// 310 342 /// # Examples
+19
test/wisp_test.gleam
··· 32 32 |> should.equal(Response(204, [], wisp.Empty)) 33 33 } 34 34 35 + pub fn redirect_test() { 36 + wisp.redirect(to: "https://example.com/wibble") 37 + |> should.equal(Response( 38 + 303, 39 + [#("location", "https://example.com/wibble")], 40 + wisp.Empty, 41 + )) 42 + } 43 + 44 + pub fn moved_permanently_test() { 45 + wisp.moved_permanently(to: "https://example.com/wobble") 46 + |> should.equal(Response( 47 + 308, 48 + [#("location", "https://example.com/wobble")], 49 + wisp.Empty, 50 + )) 51 + } 52 + 35 53 pub fn internal_server_error_test() { 36 54 wisp.internal_server_error() 37 55 |> should.equal(Response(500, [], wisp.Empty)) ··· 245 263 } 246 264 247 265 pub fn rescue_crashes_error_test() { 266 + // TODO: Determine how to silence the logger for this test. 248 267 { 249 268 use <- wisp.rescue_crashes 250 269 panic as "we need to crash to test the middleware"