Skip to content

Commit e2b4dc5

Browse files
committed
Add test cases for mutation of request/responses
for mirage#38
1 parent a503fa7 commit e2b4dc5

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
0.9.12 (trunk):
2+
* Expose Fieldslib setters and getters for most of the `Cohttp` types (#38).
23
* `Cohttp.Set_cookie.t` is no longer an abstract type to make it easier to update (#38).
34
* [Lwt] ignore SIGPIPE unconditionally if using the Lwt/Unix module (#37).
45
* Rename `Cookie` creation parameters for consistency (interface breaking, see #44).

lib_test/client.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
while true; do curl localhost:8080; done
2+
while true; do curl localhost:8080 > /dev/null 2>&1; done

lib_test/test_parser.ml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,14 @@ let get_substring oc buf =
219219
let b = String.create len in
220220
Lwt_bytes.blit_bytes_string buf 0 b 0 len;
221221
b
222-
223-
let make_simple_req () =
222+
223+
let write_req expected req =
224224
let open Cohttp in
225225
let open Cohttp_lwt_unix in
226-
let expected = "GET /foo/bar HTTP/1.1\r\nfoo: bar\r\nhost: localhost\r\ntransfer-encoding: chunked\r\n\r\n6\r\nfoobar\r\n0\r\n\r\n" in
227226
(* Use the low-level write_header/footer API *)
228227
let buf = Lwt_bytes.create 4096 in
229228
let oc = oc_of_buffer buf in
230229
let body = Cohttp_lwt_body.body_of_string "foobar" in
231-
let req = Request.make ~encoding:Transfer.Chunked ~headers:(Header.init_with "foo" "bar") (Uri.of_string "/foo/bar") in
232230
Request.write (fun req oc ->
233231
Cohttp_lwt_body.write_body (Request.write_body req oc) body
234232
) req oc >>= fun () ->
@@ -237,9 +235,24 @@ let make_simple_req () =
237235
* by re-using it *)
238236
let buf = Lwt_bytes.create 4096 in
239237
let oc = oc_of_buffer buf in
240-
Request.write (fun req oc -> Request.write_body req oc "foobar") req oc >>= fun () ->
241-
assert_equal expected (get_substring oc buf);
242-
return ()
238+
Request.write (fun req oc -> Request.write_body req oc "foobar") req oc
239+
>|= fun () ->
240+
assert_equal expected (get_substring oc buf)
241+
242+
let make_simple_req () =
243+
let open Cohttp in
244+
let open Cohttp_lwt_unix in
245+
let expected = "GET /foo/bar HTTP/1.1\r\nfoo: bar\r\nhost: localhost\r\ntransfer-encoding: chunked\r\n\r\n6\r\nfoobar\r\n0\r\n\r\n" in
246+
let req = Request.make ~encoding:Transfer.Chunked ~headers:(Header.init_with "foo" "bar") (Uri.of_string "/foo/bar") in
247+
write_req expected req
248+
249+
let mutate_simple_req () =
250+
let open Cohttp in
251+
let open Cohttp_lwt_unix in
252+
let expected = "POST /foo/bar HTTP/1.1\r\nfoo: bar\r\nhost: localhost\r\ntransfer-encoding: chunked\r\n\r\n6\r\nfoobar\r\n0\r\n\r\n" in
253+
let req = Request.make ~encoding:Transfer.Chunked ~headers:(Header.init_with "foo" "bar") (Uri.of_string "/foo/bar") in
254+
Request.set_meth req `POST;
255+
write_req expected req
243256

244257
let make_simple_res () =
245258
let open Cohttp in
@@ -273,6 +286,7 @@ let test_cases =
273286
"basic_res_parse 2", (basic_res_parse basic_res_plus_crlf);
274287
"res_content_parse", res_content_parse;
275288
"make_simple_req", make_simple_req;
289+
"mutate_simple_req", mutate_simple_req;
276290
"make_simple_res", make_simple_res;
277291
] in
278292
List.map (fun (n,x) -> n >:: (fun () -> Lwt_unix.run (x ()))) tests

0 commit comments

Comments
 (0)