packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | // Streamed responses over loopback sockets (native only: the interpreter |
| 2 | // has no sockets). They run when LOUISE_NET_TESTS is set, and are skipped |
| 3 | // otherwise: |
| 4 | // |
| 5 | // LOUISE_NET_TESTS=1 louise test --native |
| 6 | // |
| 7 | // The servers are raw TCP: they write a response in steps the test |
| 8 | // controls, so a test can see a piece arrive before the rest is sent. |
| 9 | |
| 10 | import fetch. |
| 11 | { type BodyStream |
| 12 | , type Options |
| 13 | , type Piece |
| 14 | , AnyAddress |
| 15 | , Data |
| 16 | , End |
| 17 | , Malformed |
| 18 | , Options |
| 19 | , PublicOnly |
| 20 | , Refused |
| 21 | , Streamed |
| 22 | , TimedOut |
| 23 | } |
| 24 | import sse.{Event} |
| 25 | import std/bytes |
| 26 | import std/ffi.{type Resource} |
| 27 | import std/net.{type TcpListener, type TcpStream} |
| 28 | import std/option |
| 29 | import std/os |
| 30 | import std/process.{type Subject, type proc} |
| 31 | import std/result |
| 32 | import std/string |
| 33 | import std/test.{assert, assert_equal} |
| 34 | import std/time.{type clock} |
| 35 | import std/tls.{type TlsConfig} |
| 36 | import url.{type Url} |
| 37 | |
| 38 | fn enabled :: -> Bool / <io> |
| 39 | { |
| 40 | option.is_some (os.env "LOUISE_NET_TESTS") |
| 41 | } |
| 42 | |
| 43 | fn local :: deadline_ms: Int -> Options / <io, clock> |
| 44 | { |
| 45 | // Plain http only: the TLS configuration is never used. |
| 46 | Options( ..fetch.options (tls_defaults ()) |
| 47 | , policy: AnyAddress |
| 48 | , deadline: fetch.deadline_in deadline_ms |
| 49 | ) |
| 50 | } |
| 51 | |
| 52 | fn tls_defaults :: -> Resource<TlsConfig> / <io> |
| 53 | { |
| 54 | result.expect (tls.config (tls.defaults ())) "a TLS configuration" |
| 55 | } |
| 56 | |
| 57 | fn at :: port: Int -> Url |
| 58 | { |
| 59 | result.expect (url.parse "http://127.0.0.1:{port}/events") "url" |
| 60 | } |
| 61 | |
| 62 | fn now_ms :: -> Int / <clock> |
| 63 | { |
| 64 | time.in_milliseconds (time.monotonic ()) |
| 65 | } |
| 66 | |
| 67 | // ---- a server in steps --------------------------------------------------------- |
| 68 | |
| 69 | /// What the server does, in order: write some text, wait for the test's |
| 70 | /// go-ahead, or read until the client closes and report it. |
| 71 | type Step { Write(String), AwaitGo, ReportClose } |
| 72 | |
| 73 | /// A server for one connection that reads the request head, then runs |
| 74 | /// `steps` and closes. Its port, and where to send go-aheads. |
| 75 | fn stepped_server :: |
| 76 | steps: List<Step> |
| 77 | closed: Subject<Bool> |
| 78 | -> (Int, Subject<Subject<()>>) / <io, proc> |
| 79 | { |
| 80 | let listener = result.expect (net.listen "127.0.0.1" 0) "listen" |
| 81 | let port = net.port listener |
| 82 | // The server's go-ahead subject must be its own: it sends it back. |
| 83 | let handoff = process.subject () |
| 84 | let _ = |
| 85 | process.spawn |
| 86 | fn |
| 87 | { |
| 88 | let go = process.subject () |
| 89 | process.send handoff go |
| 90 | serve listener steps go closed |
| 91 | } |
| 92 | (port, handoff) |
| 93 | } |
| 94 | |
| 95 | fn serve :: |
| 96 | listener: Resource<TcpListener> |
| 97 | steps: List<Step> |
| 98 | go: Subject<()> |
| 99 | closed: Subject<Bool> |
| 100 | -> () / <io, proc> |
| 101 | { |
| 102 | match net.accept listener |
| 103 | { Ok(conn) -> |
| 104 | { |
| 105 | let _ = head_from conn (bytes.empty ()) |
| 106 | run conn steps go closed |
| 107 | net.close conn |
| 108 | } |
| 109 | , Error(_) -> () |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn run :: |
| 114 | conn: Resource<TcpStream> |
| 115 | steps: List<Step> |
| 116 | go: Subject<()> |
| 117 | closed: Subject<Bool> |
| 118 | -> () / <io, proc> |
| 119 | { |
| 120 | match steps |
| 121 | { [] -> () |
| 122 | , [Write(text), ..rest] -> |
| 123 | { |
| 124 | let _ = net.write conn (bytes.from_string text) |
| 125 | run conn rest go closed |
| 126 | } |
| 127 | , [AwaitGo, ..rest] -> |
| 128 | { |
| 129 | let _ = process.receive go 10000 |
| 130 | run conn rest go closed |
| 131 | } |
| 132 | , [ReportClose, ..rest] -> |
| 133 | { |
| 134 | process.send closed (until_closed conn) |
| 135 | run conn rest go closed |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /// Reads until the peer closes (True), or gives up after a while (False). |
| 141 | fn until_closed :: conn: Resource<TcpStream> -> Bool / <io> |
| 142 | { |
| 143 | match net.read conn 4096 |
| 144 | { Ok(more) if bytes.length more > 0 -> until_closed conn |
| 145 | , Ok(_) -> True |
| 146 | , Error(_) -> True |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | fn head_from :: conn: Resource<TcpStream> have: Bytes -> Bytes / <io> |
| 151 | { |
| 152 | if string.contains (bytes.to_string_lossy have) "\r\n\r\n" |
| 153 | { |
| 154 | have |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | match net.read conn 4096 |
| 159 | { Ok(more) if bytes.length more > 0 -> |
| 160 | head_from conn (bytes.concat have more) |
| 161 | , _ -> have |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | fn go_ahead :: handoff: Subject<Subject<()>> -> () / <proc> |
| 167 | { |
| 168 | match process.receive handoff 5000 |
| 169 | { Some(go) -> process.send go () |
| 170 | , None -> panic "the server never sent its go-ahead subject" |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | fn start :: port: Int opts: Options -> BodyStream / <proc, clock> |
| 175 | { |
| 176 | match fetch.request_streaming "GET" (at port) [] (bytes.empty ()) opts |
| 177 | { Ok(Streamed(200, _, stream)) -> stream |
| 178 | , Ok(Streamed(status, _, _)) -> panic "status {status}" |
| 179 | , Error(e) -> panic (show e) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /// Every piece until the end: the text and whether it was cut. |
| 184 | fn rest_of :: stream: BodyStream text: String -> (String, Bool) / <proc, clock> |
| 185 | { |
| 186 | match fetch.next stream |
| 187 | { Ok(Data(b)) -> rest_of stream (text <> bytes.to_string_lossy b) |
| 188 | , Ok(End(cut)) -> (text, cut) |
| 189 | , Error(e) -> panic (show e) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // ---- tests --------------------------------------------------------------------- |
| 194 | |
| 195 | @test |
| 196 | fn a_chunked_body_arrives_before_it_is_complete :: -> () / <io, proc, clock> |
| 197 | { |
| 198 | if enabled () |
| 199 | { |
| 200 | let closed = process.subject () |
| 201 | let (port, handoff) = |
| 202 | stepped_server |
| 203 | [ Write( "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nTransfer-Encoding: chunked\r\n\r\n" |
| 204 | <> "15\r\nevent: delta\ndata: Bo\r\n" |
| 205 | ) |
| 206 | , AwaitGo |
| 207 | , Write("5\r\nn\n\nda\r\n") |
| 208 | , Write("a\r\nta: jour\n\n\r\n0\r\n\r\n") |
| 209 | ] |
| 210 | closed |
| 211 | let stream = start port (local 5000) |
| 212 | // The server waits for the go-ahead: this piece came on its own. |
| 213 | assert_equal |
| 214 | (fetch.next stream) |
| 215 | Ok(Data(bytes.from_string "event: delta\ndata: Bo")) |
| 216 | go_ahead handoff |
| 217 | let (text, cut) = rest_of stream "" |
| 218 | assert (!cut) "not cut" |
| 219 | let all = "event: delta\ndata: Bo" <> text |
| 220 | assert_equal |
| 221 | (sse.feed (sse.parser ()) (bytes.from_string all)).1 |
| 222 | [Event("delta", "Bon", ""), Event("message", "jour", "")] |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | @test |
| 227 | fn bodies_by_length_and_until_close_are_capped :: -> () / <io, proc, clock> |
| 228 | { |
| 229 | if enabled () |
| 230 | { |
| 231 | let closed = process.subject () |
| 232 | let (port, _) = |
| 233 | stepped_server |
| 234 | [Write("HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\n0123456789")] |
| 235 | closed |
| 236 | let opts = Options(..local 5000, max_body: 4) |
| 237 | assert_equal (rest_of (start port opts) "") ("0123", True) |
| 238 | let (port, _) = |
| 239 | stepped_server [Write("HTTP/1.1 200 OK\r\n\r\nabc"), Write("def")] closed |
| 240 | assert_equal (rest_of (start port (local 5000)) "") ("abcdef", False) |
| 241 | let (port, _) = |
| 242 | stepped_server [Write("HTTP/1.1 200 OK\r\n\r\nabcdef")] closed |
| 243 | assert_equal (rest_of (start port opts) "") ("abcd", True) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | @test |
| 248 | fn the_deadline_stops_a_stalled_body :: -> () / <io, proc, clock> |
| 249 | { |
| 250 | if enabled () |
| 251 | { |
| 252 | let closed = process.subject () |
| 253 | let (port, _) = |
| 254 | stepped_server |
| 255 | [Write("HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nabc"), AwaitGo] |
| 256 | closed |
| 257 | let started = now_ms () |
| 258 | let stream = start port (local 300) |
| 259 | assert_equal (fetch.next stream) Ok(Data(bytes.from_string "abc")) |
| 260 | assert_equal (fetch.next stream) Error(TimedOut) |
| 261 | assert (now_ms () - started < 3000) "stopped at the deadline" |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @test |
| 266 | fn cancel_closes_the_connection :: -> () / <io, proc, clock> |
| 267 | { |
| 268 | if enabled () |
| 269 | { |
| 270 | let closed = process.subject () |
| 271 | let (port, _) = |
| 272 | stepped_server |
| 273 | [ Write( "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nabc\r\n" |
| 274 | ) |
| 275 | , ReportClose |
| 276 | ] |
| 277 | closed |
| 278 | let stream = start port (local 5000) |
| 279 | assert_equal (fetch.next stream) Ok(Data(bytes.from_string "abc")) |
| 280 | fetch.cancel stream |
| 281 | assert_equal (process.receive closed 3000) Some(True) |
| 282 | // Harmless twice. |
| 283 | fetch.cancel stream |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | @test |
| 288 | fn failures_before_the_head_are_errors :: -> () / <io, proc, clock> |
| 289 | { |
| 290 | if enabled () |
| 291 | { |
| 292 | let closed = process.subject () |
| 293 | let opts = Options(..local 5000, policy: PublicOnly) |
| 294 | match fetch.request_streaming "GET" (at 9) [] (bytes.empty ()) opts |
| 295 | { Error(Refused(_, ip)) -> assert_equal ip "127.0.0.1" |
| 296 | , _ -> panic "a private address was allowed" |
| 297 | } |
| 298 | let (port, _) = stepped_server [Write("SPDY/9 nope\r\n\r\n")] closed |
| 299 | match fetch.request_streaming |
| 300 | "GET" |
| 301 | (at port) |
| 302 | [] |
| 303 | (bytes.empty ()) |
| 304 | (local 5000) |
| 305 | { Error(Malformed(_)) -> (), _ -> panic "a malformed head was accepted" } |
| 306 | let (port, _) = |
| 307 | stepped_server |
| 308 | [Write("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\nzz\r\n")] |
| 309 | closed |
| 310 | let stream = start port (local 5000) |
| 311 | match fetch.next stream |
| 312 | { Error(Malformed(_)) -> () |
| 313 | , other -> panic "a bad chunk was accepted: {show other}" |
| 314 | } |
| 315 | } |
| 316 | } |