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 | // Fetches over loopback sockets (native only: the interpreter has no |
| 2 | // 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 std/http, a raw TCP server that answers what it is |
| 8 | // given, and a TLS server with the certificates of test/fixtures/tls |
| 9 | // (`localhost` and `127.0.0.1`, from the "Louise test CA"). |
| 10 | |
| 11 | import fetch. |
| 12 | { type Options |
| 13 | , type Response |
| 14 | , AnyAddress |
| 15 | , BadHeader |
| 16 | , BadUrl |
| 17 | , Connect |
| 18 | , Malformed |
| 19 | , NoAddress |
| 20 | , Options |
| 21 | , PublicOnly |
| 22 | , Refused |
| 23 | , Response |
| 24 | , TimedOut |
| 25 | , Tls |
| 26 | } |
| 27 | import std/bytes |
| 28 | import std/ffi.{type Resource} |
| 29 | import std/http.{type Request, Settings, Streamed, Whole} |
| 30 | import std/list |
| 31 | import std/net.{type TcpListener, type TcpStream} |
| 32 | import std/option |
| 33 | import std/os |
| 34 | import std/process.{type proc} |
| 35 | import std/result |
| 36 | import std/string |
| 37 | import std/test.{assert, assert_equal} |
| 38 | import std/time.{type clock} |
| 39 | import std/tls.{type TlsConfig, type TlsServerConfig} |
| 40 | import url.{type Url, Url} |
| 41 | |
| 42 | fn enabled :: -> Bool / <io> |
| 43 | { |
| 44 | option.is_some (os.env "LOUISE_NET_TESTS") |
| 45 | } |
| 46 | |
| 47 | fn fixture :: name: String -> String |
| 48 | { |
| 49 | "test/fixtures/tls/" <> name |
| 50 | } |
| 51 | |
| 52 | fn client_tls :: -> Resource<TlsConfig> / <io> |
| 53 | { |
| 54 | result.expect |
| 55 | (tls.config (tls.with_ca_file (tls.defaults ()) (fixture "ca.pem"))) |
| 56 | "the client's TLS configuration" |
| 57 | } |
| 58 | |
| 59 | fn local :: -> Options / <io, clock> |
| 60 | { |
| 61 | Options( ..fetch.options (client_tls ()) |
| 62 | , policy: AnyAddress |
| 63 | , deadline: fetch.deadline_in 5000 |
| 64 | ) |
| 65 | } |
| 66 | |
| 67 | fn at :: text: String -> Url |
| 68 | { |
| 69 | result.expect (url.parse text) text |
| 70 | } |
| 71 | |
| 72 | fn now_ms :: -> Int / <clock> |
| 73 | { |
| 74 | time.in_milliseconds (time.monotonic ()) |
| 75 | } |
| 76 | |
| 77 | // ---- servers ------------------------------------------------------------------- |
| 78 | |
| 79 | /// A std/http server for `connections` connections; its port. |
| 80 | fn http_server :: connections: Int -> Int / <io, proc> |
| 81 | { |
| 82 | let server = result.expect (http.listen "127.0.0.1" 0) "listen" |
| 83 | let port = net.port server |
| 84 | let _ = |
| 85 | process.spawn |
| 86 | fn |
| 87 | { |
| 88 | http.serve_with |
| 89 | server |
| 90 | Settings(..http.defaults (), connections: connections) |
| 91 | fn req { handler req } |
| 92 | } |
| 93 | port |
| 94 | } |
| 95 | |
| 96 | fn handler :: req: Request -> http.Response |
| 97 | { |
| 98 | match http.path req |
| 99 | { "/hello" -> http.text 200 "hello" |
| 100 | , "/no-content" -> no_body_response 204 |
| 101 | , "/not-modified" -> no_body_response 304 |
| 102 | , "/chunked" -> |
| 103 | http.Response( 200 |
| 104 | , [("Content-Type", "text/plain")] |
| 105 | , Streamed(fn w { pieces w ["hel", "lo"] }) |
| 106 | ) |
| 107 | , "/big" -> http.response 200 "text/plain" (filled 102400) |
| 108 | , "/big-chunked" -> |
| 109 | http.Response( 200 |
| 110 | , [("Content-Type", "text/plain")] |
| 111 | , Streamed( fn w |
| 112 | { |
| 113 | pieces |
| 114 | w |
| 115 | (list.map (list.range 1 10) fn _ { string.repeat "x" 10240 }) |
| 116 | } |
| 117 | ) |
| 118 | ) |
| 119 | , "/redirect" -> |
| 120 | http.Response(302, [("Location", "/hello")], Whole(bytes.empty ())) |
| 121 | // what came: the method, the length it said, the body |
| 122 | , "/echo" -> |
| 123 | http.text |
| 124 | 200 |
| 125 | "{req.method} {option.unwrap_or (http.header req "Content-Length") "-"} {bytes.to_string_lossy req.body}" |
| 126 | , _ -> http.not_found () |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | fn no_body_response :: status: Int -> http.Response |
| 131 | { |
| 132 | http.Response( status |
| 133 | , [("ETag", "\"v1\""), ("Cache-Control", "max-age=60")] |
| 134 | , Whole(bytes.from_string "not sent") |
| 135 | ) |
| 136 | } |
| 137 | |
| 138 | fn pieces :: w: http.Writer texts: List<String> -> () / <io, proc> |
| 139 | { |
| 140 | match texts |
| 141 | { [] -> () |
| 142 | , [t, ..rest] -> match http.write_string w t |
| 143 | { Ok(_) -> pieces w rest, Error(_) -> () } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn filled :: n: Int -> Bytes |
| 148 | { |
| 149 | bytes.from_string (string.repeat "x" n) |
| 150 | } |
| 151 | |
| 152 | /// A server for one connection: it reads the request head, writes what |
| 153 | /// `answer` makes of it, and closes. |
| 154 | fn raw_server :: answer: send fn(Bytes) -> Bytes -> Int / <io, proc> |
| 155 | { |
| 156 | let listener = result.expect (net.listen "127.0.0.1" 0) "listen" |
| 157 | let port = net.port listener |
| 158 | let _ = process.spawn fn { answer_once listener answer } |
| 159 | port |
| 160 | } |
| 161 | |
| 162 | fn answer_once :: |
| 163 | listener: Resource<TcpListener> |
| 164 | answer: fn(Bytes) -> Bytes |
| 165 | -> () / <io> |
| 166 | { |
| 167 | match net.accept listener |
| 168 | { Ok(conn) -> |
| 169 | { |
| 170 | let request = head_from conn (bytes.empty ()) |
| 171 | let _ = net.write conn (answer request) |
| 172 | net.close conn |
| 173 | } |
| 174 | , Error(_) -> () |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | fn head_from :: conn: Resource<TcpStream> have: Bytes -> Bytes / <io> |
| 179 | { |
| 180 | if string.contains (bytes.to_string_lossy have) "\r\n\r\n" |
| 181 | { |
| 182 | have |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | match net.read conn 4096 |
| 187 | { Ok(more) if bytes.length more > 0 -> |
| 188 | head_from conn (bytes.concat have more) |
| 189 | , _ -> have |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | fn answering :: text: String -> Int / <io, proc> |
| 195 | { |
| 196 | raw_server fn _ { bytes.from_string text } |
| 197 | } |
| 198 | |
| 199 | /// A server that accepts and never answers. |
| 200 | fn stalling_server :: -> Int / <io, proc> |
| 201 | { |
| 202 | let listener = result.expect (net.listen "127.0.0.1" 0) "listen" |
| 203 | let port = net.port listener |
| 204 | let _ = |
| 205 | process.spawn |
| 206 | fn |
| 207 | { |
| 208 | match net.accept listener |
| 209 | { Ok(conn) -> |
| 210 | { |
| 211 | process.sleep 10000 |
| 212 | net.close conn |
| 213 | } |
| 214 | , Error(_) -> () |
| 215 | } |
| 216 | } |
| 217 | port |
| 218 | } |
| 219 | |
| 220 | /// A TLS server for one connection with `cert`; it answers `text`, then |
| 221 | /// ends the TLS stream properly or (`clean` false) just closes the socket. |
| 222 | fn tls_server :: cert: String text: String clean: Bool -> Int / <io, proc> |
| 223 | { |
| 224 | let config = |
| 225 | result.expect |
| 226 | ( tls.server_config |
| 227 | ( tls.server_options |
| 228 | (fixture (cert <> ".pem")) |
| 229 | (fixture (cert <> ".key")) |
| 230 | ) |
| 231 | ) |
| 232 | "the server's TLS configuration" |
| 233 | let listener = result.expect (net.listen "127.0.0.1" 0) "listen" |
| 234 | let port = net.port listener |
| 235 | let _ = process.spawn fn { tls_once listener config text clean } |
| 236 | port |
| 237 | } |
| 238 | |
| 239 | fn tls_once :: |
| 240 | listener: Resource<TcpListener> |
| 241 | config: Resource<TlsServerConfig> |
| 242 | text: String |
| 243 | clean: Bool |
| 244 | -> () / <io> |
| 245 | { |
| 246 | match net.accept listener |
| 247 | { Ok(conn) -> match tls.accept conn config |
| 248 | { Ok(stream) -> |
| 249 | { |
| 250 | let _ = tls.read stream 65536 |
| 251 | let _ = tls.write_string stream text |
| 252 | if clean |
| 253 | { |
| 254 | tls.close stream |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | net.close conn |
| 259 | } |
| 260 | } |
| 261 | , Error(_) -> net.close conn |
| 262 | } |
| 263 | , Error(_) -> () |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | fn body_text :: |
| 268 | answer: Result<Response, fetch.Error> |
| 269 | -> Result<(Int, String, Bool), fetch.Error> |
| 270 | { |
| 271 | answer |
| 272 | |> result.map fn r { (r.status, bytes.to_string_lossy r.body, r.truncated) } |
| 273 | } |
| 274 | |
| 275 | // ---- bodies -------------------------------------------------------------------- |
| 276 | |
| 277 | @test |
| 278 | fn a_body_by_content_length :: -> () / <io, proc, clock> |
| 279 | { |
| 280 | if enabled () |
| 281 | { |
| 282 | let port = http_server 1 |
| 283 | let answer = fetch.get (at "http://127.0.0.1:{port}/hello") [] (local ()) |
| 284 | assert_equal (body_text answer) Ok((200, "hello", False)) |
| 285 | match answer |
| 286 | { Ok(resp) -> assert_equal (fetch.header resp "CONTENT-LENGTH") Some("5") |
| 287 | , Error(_) -> () |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | @test |
| 293 | fn final_statuses_without_bodies_reach_the_client :: -> () / <io, proc, clock> |
| 294 | { |
| 295 | if enabled () |
| 296 | { |
| 297 | let port = http_server 2 |
| 298 | let no_content = |
| 299 | fetch.get (at "http://127.0.0.1:{port}/no-content") [] (local ()) |
| 300 | assert_equal (body_text no_content) Ok((204, "", False)) |
| 301 | match no_content |
| 302 | { Ok(resp) -> |
| 303 | { |
| 304 | assert_equal (fetch.header resp "Content-Length") None |
| 305 | assert_equal (fetch.header resp "Transfer-Encoding") None |
| 306 | assert_equal (fetch.header resp "ETag") Some("\"v1\"") |
| 307 | assert_equal (fetch.header resp "Cache-Control") Some("max-age=60") |
| 308 | } |
| 309 | , Error(e) -> panic (show e) |
| 310 | } |
| 311 | let not_modified = |
| 312 | fetch.get (at "http://127.0.0.1:{port}/not-modified") [] (local ()) |
| 313 | assert_equal (body_text not_modified) Ok((304, "", False)) |
| 314 | match not_modified |
| 315 | { Ok(resp) -> |
| 316 | { |
| 317 | assert_equal (fetch.header resp "Content-Length") None |
| 318 | assert_equal (fetch.header resp "Transfer-Encoding") None |
| 319 | assert_equal (fetch.header resp "ETag") Some("\"v1\"") |
| 320 | assert_equal (fetch.header resp "Cache-Control") Some("max-age=60") |
| 321 | } |
| 322 | , Error(e) -> panic (show e) |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | @test |
| 328 | fn a_chunked_body :: -> () / <io, proc, clock> |
| 329 | { |
| 330 | if enabled () |
| 331 | { |
| 332 | let port = http_server 1 |
| 333 | assert_equal |
| 334 | ( body_text |
| 335 | (fetch.get (at "http://127.0.0.1:{port}/chunked") [] (local ())) |
| 336 | ) |
| 337 | Ok((200, "hello", False)) |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | @test |
| 342 | fn a_body_until_the_server_closes :: -> () / <io, proc, clock> |
| 343 | { |
| 344 | if enabled () |
| 345 | { |
| 346 | let port = |
| 347 | answering "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nall of it" |
| 348 | assert_equal |
| 349 | (body_text (fetch.get (at "http://127.0.0.1:{port}/") [] (local ()))) |
| 350 | Ok((200, "all of it", False)) |
| 351 | let port = answering "HTTP/1.0 200 OK\r\n\r\nall of it" |
| 352 | assert_equal |
| 353 | ( body_text |
| 354 | ( fetch.get |
| 355 | (at "http://127.0.0.1:{port}/") |
| 356 | [] |
| 357 | Options(..local (), max_body: 3) |
| 358 | ) |
| 359 | ) |
| 360 | Ok((200, "all", True)) |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | @test |
| 365 | fn bodies_are_cut_at_max_body :: -> () / <io, proc, clock> |
| 366 | { |
| 367 | if enabled () |
| 368 | { |
| 369 | let port = http_server 2 |
| 370 | let opts = Options(..local (), max_body: 1000) |
| 371 | assert_equal |
| 372 | (body_text (fetch.get (at "http://127.0.0.1:{port}/big") [] opts)) |
| 373 | Ok((200, string.repeat "x" 1000, True)) |
| 374 | assert_equal |
| 375 | (body_text (fetch.get (at "http://127.0.0.1:{port}/big-chunked") [] opts)) |
| 376 | Ok((200, string.repeat "x" 1000, True)) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | @test |
| 381 | fn informational_responses_are_skipped :: -> () / <io, proc, clock> |
| 382 | { |
| 383 | if enabled () |
| 384 | { |
| 385 | let port = |
| 386 | answering |
| 387 | "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 103 Early Hints\r\nLink: </a.css>\r\n\r\nHTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok" |
| 388 | assert_equal |
| 389 | (body_text (fetch.get (at "http://127.0.0.1:{port}/") [] (local ()))) |
| 390 | Ok((200, "ok", False)) |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | @test |
| 395 | fn a_redirect_is_returned_not_followed :: -> () / <io, proc, clock> |
| 396 | { |
| 397 | if enabled () |
| 398 | { |
| 399 | let port = http_server 1 |
| 400 | match fetch.get (at "http://127.0.0.1:{port}/redirect") [] (local ()) |
| 401 | { Ok(resp) -> |
| 402 | { |
| 403 | assert_equal resp.status 302 |
| 404 | assert_equal (fetch.header resp "location") Some("/hello") |
| 405 | } |
| 406 | , Error(e) -> panic (show e) |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | @test |
| 412 | fn what_is_sent :: -> () / <io, proc, clock> |
| 413 | { |
| 414 | if enabled () |
| 415 | { |
| 416 | let port = |
| 417 | raw_server |
| 418 | fn request |
| 419 | { |
| 420 | bytes.concat |
| 421 | ( bytes.from_string |
| 422 | "HTTP/1.1 200 OK\r\nContent-Length: {bytes.length request}\r\n\r\n" |
| 423 | ) |
| 424 | request |
| 425 | } |
| 426 | let headers = |
| 427 | [ ("User-Agent", "test/1") |
| 428 | , ("Host", "evil.example") |
| 429 | , ("accept-encoding", "gzip") |
| 430 | , ("Connection", "keep-alive") |
| 431 | , ("Accept", "text/html") |
| 432 | ] |
| 433 | let answer = |
| 434 | fetch.get (at "http://127.0.0.1:{port}/a b/é?q=1#frag") headers (local ()) |
| 435 | assert_equal |
| 436 | (body_text answer) |
| 437 | Ok( ( 200 |
| 438 | , "GET /a%20b/%C3%A9?q=1 HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nConnection: close\r\nAccept-Encoding: identity\r\nUser-Agent: test/1\r\nAccept: text/html\r\n\r\n" |
| 439 | , False |
| 440 | ) |
| 441 | ) |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // ---- requests with a body -------------------------------------------------------- |
| 446 | |
| 447 | @test |
| 448 | fn a_post_sends_its_body_and_its_length :: -> () / <io, proc, clock> |
| 449 | { |
| 450 | if enabled () |
| 451 | { |
| 452 | let port = http_server 3 |
| 453 | let post = fn headers body |
| 454 | { |
| 455 | fetch.request |
| 456 | "POST" |
| 457 | (at "http://127.0.0.1:{port}/echo") |
| 458 | headers |
| 459 | (bytes.from_string body) |
| 460 | (local ()) |
| 461 | |> body_text |
| 462 | } |
| 463 | assert_equal |
| 464 | (post [("Content-Type", "application/json")] "\{\"a\":\"é\"\}") |
| 465 | Ok((200, "POST 10 \{\"a\":\"é\"\}", False)) |
| 466 | // the caller's own length is dropped for the body's |
| 467 | assert_equal |
| 468 | (post [("Content-Length", "999")] "abc") |
| 469 | Ok((200, "POST 3 abc", False)) |
| 470 | // an empty POST still says its length |
| 471 | assert_equal (post [] "") Ok((200, "POST 0 ", False)) |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | @test |
| 476 | fn a_method_that_is_not_a_token_is_refused :: -> () / <io, proc, clock> |
| 477 | { |
| 478 | if enabled () |
| 479 | { |
| 480 | match fetch.request |
| 481 | "GE T" |
| 482 | (at "http://127.0.0.1:1/") |
| 483 | [] |
| 484 | (bytes.empty ()) |
| 485 | (local ()) |
| 486 | { Error(fetch.BadHeader(_)) -> () |
| 487 | , other -> panic "expected BadHeader, got {other}" |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // ---- failures ------------------------------------------------------------------ |
| 493 | |
| 494 | @test |
| 495 | fn malformed_responses :: -> () / <io, proc, clock> |
| 496 | { |
| 497 | if enabled () |
| 498 | { |
| 499 | let malformed = fn text |
| 500 | { |
| 501 | let port = answering text |
| 502 | match fetch.get (at "http://127.0.0.1:{port}/") [] (local ()) |
| 503 | { Error(Malformed(_)) -> True, _ -> False } |
| 504 | } |
| 505 | assert |
| 506 | (malformed "HTTP/1.1 200 OK\r\nTransfer-Encoding: gzip\r\n\r\nxx") |
| 507 | "malformed HTTP/1.1 200 OKrnTransfer-Encoding: gziprnrnxx" |
| 508 | assert |
| 509 | (malformed "HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nabc") |
| 510 | "malformed HTTP/1.1 200 OKrnContent-Length: 10rnrnabc" |
| 511 | assert |
| 512 | (malformed "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nab") |
| 513 | "malformed HTTP/1.1 200 OKrnTransfer-Encoding: chunkedrnrn5rnab" |
| 514 | assert |
| 515 | (malformed "SSH-2.0-OpenSSH_9.6\r\n\r\n") |
| 516 | "malformed SSH-2.0-OpenSSH_9.6rnrn" |
| 517 | assert (malformed "") "malformed " |
| 518 | assert |
| 519 | ( malformed |
| 520 | ("HTTP/1.1 200 OK\r\nX: " <> string.repeat "a" 70000 <> "\r\n\r\n") |
| 521 | ) |
| 522 | "malformed (HTTP/1.1 200 OKrnX: <> string.repeat a 70000 <> rnrn)" |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | @test |
| 527 | fn public_only_refuses_loopback :: -> () / <io, proc, clock> |
| 528 | { |
| 529 | if enabled () |
| 530 | { |
| 531 | let opts = Options(..local (), policy: PublicOnly) |
| 532 | assert_equal |
| 533 | (fetch.get (at "http://127.0.0.1:9/") [] opts) |
| 534 | Error(Refused("127.0.0.1", "127.0.0.1")) |
| 535 | assert_equal |
| 536 | (fetch.get (at "http://[::1]:9/") [] opts) |
| 537 | Error(Refused("::1", "::1")) |
| 538 | match fetch.get (at "http://localhost:9/") [] opts |
| 539 | { Error(Refused("localhost", _)) -> () |
| 540 | , other -> panic "localhost: {show other}" |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | @test |
| 546 | fn a_stalling_server_times_out :: -> () / <io, proc, clock> |
| 547 | { |
| 548 | if enabled () |
| 549 | { |
| 550 | let port = stalling_server () |
| 551 | let start = now_ms () |
| 552 | let opts = Options(..local (), deadline: fetch.deadline_in 300) |
| 553 | assert_equal |
| 554 | (fetch.get (at "http://127.0.0.1:{port}/") [] opts) |
| 555 | Error(TimedOut) |
| 556 | let took = now_ms () - start |
| 557 | assert (took >= 250 && took < 2000) "took >= 250 && took < 2000" |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | @test |
| 562 | fn refused_before_any_connection :: -> () / <io, proc, clock> |
| 563 | { |
| 564 | if enabled () |
| 565 | { |
| 566 | let opts = local () |
| 567 | assert |
| 568 | (is_bad_url (fetch.get (at "ftp://127.0.0.1/") [] opts)) |
| 569 | "is_bad_url (fetch.get (at ftp://127.0.0.1/) [] opts)" |
| 570 | assert |
| 571 | ( is_bad_url |
| 572 | (fetch.get (Url("http", "a\r\nb", None, "/", None, None)) [] opts) |
| 573 | ) |
| 574 | "is_bad_url (fetch.get (Url(http, arnb, None, /, None, None)) [] opts)" |
| 575 | assert |
| 576 | ( is_bad_url (fetch.get (Url("http", "a", None, "/", None, None)) [] opts) |
| 577 | == False |
| 578 | ) |
| 579 | "is_bad_url (fetch.get (Url(http, a, None, /, None, None)) [] opts) ..." |
| 580 | assert_equal |
| 581 | (fetch.get (at "http://127.0.0.1:9/") [("X\r\nY", "1")] opts) |
| 582 | Error(BadHeader("X\r\nY")) |
| 583 | assert_equal |
| 584 | (fetch.get (at "http://127.0.0.1:9/") [("X", "1\r\nY: 2")] opts) |
| 585 | Error(BadHeader("X")) |
| 586 | assert_equal |
| 587 | (fetch.get (at "http://127.0.0.1:9/") [("Bad Name", "1")] opts) |
| 588 | Error(BadHeader("Bad Name")) |
| 589 | assert_equal |
| 590 | ( fetch.get |
| 591 | (at "http://127.0.0.1:9/") |
| 592 | [] |
| 593 | Options(..opts, deadline: fetch.deadline_in (-1)) |
| 594 | ) |
| 595 | Error(TimedOut) |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | fn is_bad_url :: answer: Result<Response, fetch.Error> -> Bool |
| 600 | { |
| 601 | match answer { Error(BadUrl(_)) -> True, _ -> False } |
| 602 | } |
| 603 | |
| 604 | @test |
| 605 | fn nothing_listening :: -> () / <io, proc, clock> |
| 606 | { |
| 607 | if enabled () |
| 608 | { |
| 609 | let listener = result.expect (net.listen "127.0.0.1" 0) "listen" |
| 610 | let port = net.port listener |
| 611 | net.close_listener listener |
| 612 | match fetch.get (at "http://127.0.0.1:{port}/") [] (local ()) |
| 613 | { Error(Connect(_)) -> (), other -> panic (show other) } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | @test |
| 618 | fn a_name_that_does_not_resolve :: -> () / <io, proc, clock> |
| 619 | { |
| 620 | if enabled () |
| 621 | { |
| 622 | match fetch.get (at "http://louise-fetch-test.invalid/") [] (local ()) |
| 623 | { Error(NoAddress("louise-fetch-test.invalid")) -> () |
| 624 | , Error(TimedOut) -> () |
| 625 | , other -> panic (show other) |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /// A kill can only land between two steps of the worker: one waiting in |
| 631 | /// `net.resolve` (on the `io` blocking pool) is killed when the lookup |
| 632 | /// returns (`runtime/louise_rt/src/blocking.rs`). `get` does not wait |
| 633 | /// for that: it answers `TimedOut` at the deadline, and the worker's late |
| 634 | /// reply, if any, is dropped. Whether a 1 ms deadline lands inside the |
| 635 | /// lookup depends on the resolver, so this checks the outcome, not where |
| 636 | /// the kill landed. |
| 637 | @test |
| 638 | fn a_deadline_during_the_lookup :: -> () / <io, proc, clock> |
| 639 | { |
| 640 | if enabled () |
| 641 | { |
| 642 | list.each |
| 643 | (list.range 1 10) |
| 644 | fn ms |
| 645 | { |
| 646 | let start = now_ms () |
| 647 | let opts = Options(..local (), deadline: fetch.deadline_in ms) |
| 648 | match fetch.get (at "http://louise-fetch-test-{ms}.invalid/") [] opts |
| 649 | { Error(TimedOut) | Error(NoAddress(_)) -> () |
| 650 | , other -> panic (show other) |
| 651 | } |
| 652 | assert (now_ms () - start < ms + 1000) "now_ms () - start < ms + 1000" |
| 653 | } |
| 654 | let port = http_server 1 |
| 655 | assert_equal |
| 656 | (body_text (fetch.get (at "http://127.0.0.1:{port}/hello") [] (local ()))) |
| 657 | Ok((200, "hello", False)) |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // ---- TLS ----------------------------------------------------------------------- |
| 662 | |
| 663 | @test |
| 664 | fn https_with_a_private_authority :: -> () / <io, proc, clock> |
| 665 | { |
| 666 | if enabled () |
| 667 | { |
| 668 | let port = |
| 669 | tls_server |
| 670 | "localhost" |
| 671 | "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok" |
| 672 | True |
| 673 | assert_equal |
| 674 | (body_text (fetch.get (at "https://localhost:{port}/") [] (local ()))) |
| 675 | Ok((200, "ok", False)) |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | @test |
| 680 | fn a_tls_close_without_close_notify_ends_the_body :: -> () / <io, proc, clock> |
| 681 | { |
| 682 | if enabled () |
| 683 | { |
| 684 | let port = |
| 685 | tls_server "localhost" "HTTP/1.1 200 OK\r\n\r\nuntil the end" False |
| 686 | assert_equal |
| 687 | (body_text (fetch.get (at "https://127.0.0.1:{port}/") [] (local ()))) |
| 688 | Ok((200, "until the end", False)) |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | @test |
| 693 | fn a_certificate_for_another_name_is_refused :: -> () / <io, proc, clock> |
| 694 | { |
| 695 | if enabled () |
| 696 | { |
| 697 | let port = tls_server "other-host" "HTTP/1.1 200 OK\r\n\r\nno" True |
| 698 | match fetch.get (at "https://localhost:{port}/") [] (local ()) |
| 699 | { Error(Tls(_)) -> (), other -> panic (show other) } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | @test |
| 704 | fn an_unknown_authority_is_refused :: -> () / <io, proc, clock> |
| 705 | { |
| 706 | if enabled () |
| 707 | { |
| 708 | let port = tls_server "localhost" "HTTP/1.1 200 OK\r\n\r\nno" True |
| 709 | let opts = |
| 710 | Options( ..local () |
| 711 | , tls: result.expect (tls.config (tls.defaults ())) "Mozilla's roots" |
| 712 | ) |
| 713 | match fetch.get (at "https://localhost:{port}/") [] opts |
| 714 | { Error(Tls(_)) -> (), other -> panic (show other) } |
| 715 | } |
| 716 | } |