Skip to content

Commit

Permalink
Allow header
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Aug 10, 2023
1 parent 96a0214 commit 9b92206
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/wisp.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
])
}
}

Expand Down
15 changes: 14 additions & 1 deletion test/wisp_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 9b92206

Please sign in to comment.