From 9b922061fd18bf8359dde54aca1c7c668946930c Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Thu, 10 Aug 2023 12:51:46 +0100 Subject: [PATCH] Allow header --- src/wisp.gleam | 18 ++++++++++++------ test/wisp_test.gleam | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/wisp.gleam b/src/wisp.gleam index a783b35..ed95b0b 100644 --- a/src/wisp.gleam +++ b/src/wisp.gleam @@ -342,18 +342,21 @@ pub fn entity_too_large() -> Response { HttpResponse(413, [], Empty) } -// TODO: test /// Create an empty response with status code 415: Unsupported media type. /// +/// The `allow` header will be set to a comma separated list of the permitted +/// content-types. +/// /// # Examples /// /// ```gleam -/// unsupported_media_type() -/// // -> Response(415, [], Empty) +/// unsupported_media_type(accept: ["application/json", "text/plain"]) +/// // -> Response(415, [#("allow", "application/json, text/plain")], Empty) /// ``` /// -pub fn unsupported_media_type() -> Response { - HttpResponse(415, [], Empty) +pub fn unsupported_media_type(accept acceptable: List(String)) -> Response { + let acceptable = string.join(acceptable, ", ") + HttpResponse(415, [#("accept", acceptable)], Empty) } /// Create an empty response with status code 500: Internal server error. @@ -691,7 +694,10 @@ pub fn require_form( Ok("multipart/form-data") -> bad_request() - _ -> unsupported_media_type() + _ -> + unsupported_media_type([ + "application/x-www-form-urlencoded", "multipart/form-data", + ]) } } diff --git a/test/wisp_test.gleam b/test/wisp_test.gleam index 616a4a0..b87a0f9 100644 --- a/test/wisp_test.gleam +++ b/test/wisp_test.gleam @@ -84,6 +84,15 @@ pub fn method_not_allowed_test() { |> should.equal(Response(405, [#("allow", "DELETE, GET, PATCH")], wisp.Empty)) } +pub fn unsupported_media_type_test() { + wisp.unsupported_media_type(accept: ["application/json", "text/plain"]) + |> should.equal(Response( + 415, + [#("accept", "application/json, text/plain")], + wisp.Empty, + )) +} + pub fn html_response_test() { let body = string_builder.from_string("Hello, world!") let response = wisp.html_response(body, 200) @@ -495,7 +504,11 @@ pub fn form_unknown_content_type_test() { |> wisp.test_request |> request.set_header("content-type", "text/form") |> form_handler(fn(_) { panic as "should be unreachable" }) - |> should.equal(Response(415, [], wisp.Empty)) + |> should.equal(Response( + 415, + [#("accept", "application/x-www-form-urlencoded, multipart/form-data")], + wisp.Empty, + )) } pub fn multipart_form_with_files_test() {