🧚 A practical web framework for Gleam

Test method override

+78 -7
+11 -3
src/wisp.gleam
··· 237 237 } 238 238 } 239 239 240 - // TODO: test 241 - // TODO: document 240 + /// Return the non-empty segments of a request path. 241 + /// 242 + /// # Examples 243 + /// 244 + /// ```gleam 245 + /// > request.new() 246 + /// > |> request.set_path("/one/two/three") 247 + /// > |> wisp.path_segments 248 + /// ["one", "two", "three"] 249 + /// ``` 250 + /// 242 251 pub const path_segments = request.path_segments 243 252 244 - // TODO: test 245 253 /// This function overrides an incoming POST request with a method given in 246 254 /// the request's `_method` query paramerter. This is useful as web browsers 247 255 /// typically only support GET and POST requests, but our application may
+67 -4
test/wisp_test.gleam
··· 1 + import gleam/http 2 + import gleam/http/request 3 + import gleam/http/response.{Response} 4 + import gleam/list 5 + import gleam/string_builder 1 6 import gleeunit 2 7 import gleeunit/should 3 8 import wisp 4 - import gleam/http 5 - import gleam/http/response.{Response} 6 - import gleam/http/request 7 - import gleam/string_builder 8 9 9 10 pub fn main() { 10 11 gleeunit.main() ··· 105 106 |> wisp.get_read_chunk_size 106 107 |> should.equal(10) 107 108 } 109 + 110 + pub fn path_segments_test() { 111 + request.new() 112 + |> request.set_path("/one/two/three") 113 + |> wisp.path_segments 114 + |> should.equal(["one", "two", "three"]) 115 + } 116 + 117 + pub fn method_override_test() { 118 + // These methods can be overridden to 119 + use method <- list.each([http.Put, http.Delete, http.Patch]) 120 + 121 + let request = 122 + request.new() 123 + |> request.set_method(method) 124 + |> request.set_query([#("_method", http.method_to_string(method))]) 125 + request 126 + |> wisp.method_override 127 + |> should.equal(request.set_method(request, method)) 128 + } 129 + 130 + pub fn method_override_unacceptable_unoriginal_method_test() { 131 + // These methods are not allowed to be overridden 132 + use method <- list.each([ 133 + http.Head, 134 + http.Put, 135 + http.Delete, 136 + http.Trace, 137 + http.Connect, 138 + http.Options, 139 + http.Patch, 140 + http.Other("MYSTERY"), 141 + ]) 142 + 143 + let request = 144 + request.new() 145 + |> request.set_method(method) 146 + |> request.set_query([#("_method", "DELETE")]) 147 + request 148 + |> wisp.method_override 149 + |> should.equal(request) 150 + } 151 + 152 + pub fn method_override_unacceptable_target_method_test() { 153 + // These methods are not allowed to be overridden to 154 + use method <- list.each([ 155 + http.Get, 156 + http.Head, 157 + http.Trace, 158 + http.Connect, 159 + http.Options, 160 + http.Other("MYSTERY"), 161 + ]) 162 + 163 + let request = 164 + request.new() 165 + |> request.set_method(http.Post) 166 + |> request.set_query([#("_method", http.method_to_string(method))]) 167 + request 168 + |> wisp.method_override 169 + |> should.equal(request) 170 + }