Skip to content

Commit

Permalink
support query params
Browse files Browse the repository at this point in the history
  • Loading branch information
rawhat committed Jul 4, 2022
1 parent cfec6e7 commit 9d0971e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/mist/http.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import gleam/otp/process
import gleam/pair
import gleam/result
import gleam/string
import gleam/uri
import glisten/tcp.{LoopState, Socket}
import mist/encoder
import mist/file
Expand Down Expand Up @@ -205,11 +206,22 @@ pub fn parse_request(
path
|> bit_string.to_string
|> result.replace_error(InvalidPath)
let #(path, query) = case string.split(path, "?") {
[path] -> #(path, [])
[path, query_string] -> {
let query =
query_string
|> uri.parse_query
|> result.unwrap([])
#(path, query)
}
}
let req =
request.new()
|> request.set_body(Unread(rest, socket))
|> request.set_method(method)
|> request.set_path(path)
|> request.set_query(query)
Ok(request.Request(..req, headers: map.to_list(headers)))
}
_ -> Error(DiscardPacket)
Expand Down
18 changes: 18 additions & 0 deletions test/http1_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub fn set_up_echo_server_test_() {
it_supports_patch_requests,
it_rejects_large_requests,
it_supports_chunked_encoding,
it_support_query_parameters,
],
)
}
Expand Down Expand Up @@ -154,3 +155,20 @@ pub fn it_supports_chunked_encoding() {

bitstring_response_should_equal(actual, expected)
}

pub fn it_support_query_parameters() {
let req =
make_request("/", "hello, world!")
|> request.set_method(http.Get)
|> request.set_query([
#("something", "123"),
#("another", "true"),
#("a-complicated-one", uri.percent_encode("is the thing")),
])

assert Ok(resp) = hackney.send(req)

let expected = get_default_response()

string_response_should_equal(resp, expected)
}

0 comments on commit 9d0971e

Please sign in to comment.