Skip to content

Commit

Permalink
Testing functions support query strings
Browse files Browse the repository at this point in the history
Closes #35
  • Loading branch information
lpil committed Aug 21, 2023
1 parent 57d793c commit 474fb05
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.4.0 - Unreleased

- The request construction functions in the `wisp/testing` module now support
query strings. e.g. `get("/users?limit=10", [])`.

## v0.3.0 - 2023-08-21

- The `mist_service` function has been renamed to `mist_handler`.
Expand Down
9 changes: 7 additions & 2 deletions src/wisp/testing.gleam
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import wisp.{Empty, File, Request, Response, Text}
import gleam/string_builder
import gleam/string
import gleam/bit_builder
import gleam/uri
import gleam/http/request
import gleam/http
import gleam/option.{None}
import gleam/option.{None, Some}
import simplifile

/// The default secret key base used for test requests.
Expand All @@ -28,6 +29,10 @@ pub fn request(
headers: List(http.Header),
body: BitString,
) -> Request {
let #(path, query) = case string.split(path, "?") {
[path, query] -> #(path, Some(query))
_ -> #(path, None)
}
request.Request(
method: method,
headers: headers,
Expand All @@ -36,7 +41,7 @@ pub fn request(
host: "localhost",
port: None,
path: path,
query: None,
query: query,
)
|> request.set_body(wisp.create_canned_connection(
body,
Expand Down
30 changes: 29 additions & 1 deletion test/wisp/testing_test.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gleam/http
import gleam/http/response
import gleam/option.{None}
import gleam/option.{None, Some}
import gleam/string_builder
import gleeunit/should
import wisp
Expand Down Expand Up @@ -418,3 +418,31 @@ pub fn bit_string_body_text_test() {
|> testing.bit_string_body
|> should.equal(<<"Hello, Joe!":utf8>>)
}

pub fn request_query_string_test() {
let request =
testing.request(
http.Patch,
"/wibble/woo?one=two&three=four",
[#("content-type", "application/json")],
<<"wubwub":utf8>>,
)

request.method
|> should.equal(http.Patch)
request.headers
|> should.equal([#("content-type", "application/json")])
request.scheme
|> should.equal(http.Https)
request.host
|> should.equal("localhost")
request.port
|> should.equal(None)
request.path
|> should.equal("/wibble/woo")
request.query
|> should.equal(Some("one=two&three=four"))
request
|> wisp.read_body_to_bitstring
|> should.equal(Ok(<<"wubwub":utf8>>))
}

0 comments on commit 474fb05

Please sign in to comment.