feat: make tests smaller, add functions
diff --git a/src/Std/Internal/Http/Data/Header/Name.lean b/src/Std/Internal/Http/Data/Header/Name.lean index a000e8b..83c2a31 100644 --- a/src/Std/Internal/Http/Data/Header/Name.lean +++ b/src/Std/Internal/Http/Data/Header/Name.lean
@@ -219,6 +219,65 @@ instance : ToString Name where toString name := name.toCanonical +/-- +Standard Content-Type header name +-/ +def contentType : Header.Name := .new "content-type" + +/-- +Standard Content-Length header name +-/ +def contentLength : Header.Name := .new "content-length" + +/-- +Standard Host header name +-/ +def host : Header.Name := .new "host" + +/-- +Standard Authorization header name +-/ +def authorization : Header.Name := .new "authorization" + +/-- +Standard User-Agent header name +-/ +def userAgent : Header.Name := .new "user-agent" + +/-- +Standard Accept header name +-/ +def accept : Header.Name := .new "accept" + +/-- +Standard Connection header name +-/ +def connection : Header.Name := .new "connection" + +/-- +Standard Transfer-Encoding header name +-/ +def transferEncoding : Header.Name := .new "transfer-encoding" + +/-- +Standard Server header name +-/ +def server : Header.Name := .new "server" + end Name +namespace Value + +/-- +Standard "close" header value for Connection header +-/ +def close : Header.Value := .new "close" + +/-- +Standard "chunked" header value for Transfer-Encoding header +-/ +def chunked : Header.Value := .new "chunked" + +end Value + end Std.Http.Header
diff --git a/src/Std/Internal/Http/Data/Headers.lean b/src/Std/Internal/Http/Data/Headers.lean index 1caaf1b..74015a2 100644 --- a/src/Std/Internal/Http/Data/Headers.lean +++ b/src/Std/Internal/Http/Data/Headers.lean
@@ -179,6 +179,58 @@ def merge (headers1 headers2 : Headers) : Headers := { map := headers1.map ∪ headers2.map } +/-- +Converts the headers to a list of key-value pairs (flattened). +Each header with multiple values produces multiple pairs. +-/ +def toList (headers : Headers) : List (Header.Name × Header.Value) := + headers.map.toList + +/-- +Converts the headers to an array of key-value pairs (flattened). +Each header with multiple values produces multiple pairs. +-/ +def toArray (headers : Headers) : Array (Header.Name × Header.Value) := + headers.map.toArray + +/-- +Folds over all key-value pairs in the headers. +-/ +def fold (headers : Headers) (init : α) (f : α → Header.Name → Header.Value → α) : α := + headers.map.toArray.foldl (fun acc (k, v) => f acc k v) init + +/-- +Maps a function over all header values, producing new headers. +-/ +def mapValues (headers : Headers) (f : Header.Name → Header.Value → Header.Value) : Headers := + let pairs := headers.map.toArray.map (fun (k, v) => (k, f k v)) + { map := pairs.foldl (fun acc (k, v) => acc.insert k v) MultiMap.empty } + +/-- +Filters and maps over header key-value pairs. +Returns only the pairs for which the function returns `some`. +-/ +def filterMap (headers : Headers) (f : Header.Name → Header.Value → Option Header.Value) : Headers := + let pairs := headers.map.toArray.filterMap (fun (k, v) => + match f k v with + | some v' => some (k, v') + | none => none) + { map := pairs.foldl (fun acc (k, v) => acc.insert k v) MultiMap.empty } + +/-- +Filters header key-value pairs, keeping only those that satisfy the predicate. +-/ +def filter (headers : Headers) (f : Header.Name → Header.Value → Bool) : Headers := + headers.filterMap (fun k v => if f k v then some v else none) + +/-- +Updates the first value of a header if it exists, or inserts if it doesn't. +Replaces all existing values for that header with the new value. +-/ +def update (headers : Headers) (name : Header.Name) (f : Option Header.Value → Header.Value) : Headers := + let newValue := f (headers.get? name) + { map := headers.map.erase name |>.insert name newValue } + instance : ToString Headers where toString headers := let pairs := headers.map.toArray.map (fun (k, v) => s!"{k}: {v.value}")
diff --git a/src/Std/Internal/Http/Data/Request.lean b/src/Std/Internal/Http/Data/Request.lean index ba58b3f..6d4ff39 100644 --- a/src/Std/Internal/Http/Data/Request.lean +++ b/src/Std/Internal/Http/Data/Request.lean
@@ -227,4 +227,39 @@ |>.method .patch |>.uri uri +/-- +Creates a new HTTP HEAD Request builder with the specified URI. +Named `head'` to avoid conflict with the `head` field accessor. +-/ +def head' (uri : RequestTarget) : Builder := + new + |>.method .head + |>.uri uri + +/-- +Creates a new HTTP OPTIONS Request builder with the specified URI. +Use `Request.options (RequestTarget.asteriskForm)` for server-wide OPTIONS. +-/ +def options (uri : RequestTarget) : Builder := + new + |>.method .options + |>.uri uri + +/-- +Creates a new HTTP CONNECT Request builder with the specified URI. +Typically used with `RequestTarget.authorityForm` for tunneling. +-/ +def connect (uri : RequestTarget) : Builder := + new + |>.method .connect + |>.uri uri + +/-- +Creates a new HTTP TRACE Request builder with the specified URI +-/ +def trace (uri : RequestTarget) : Builder := + new + |>.method .trace + |>.uri uri + end Std.Http.Request
diff --git a/src/Std/Internal/Http/Data/Response.lean b/src/Std/Internal/Http/Data/Response.lean index fadd105..799033d 100644 --- a/src/Std/Internal/Http/Data/Response.lean +++ b/src/Std/Internal/Http/Data/Response.lean
@@ -197,4 +197,65 @@ def badRequest : Builder := .empty |>.status .badRequest +/-- +Creates a new HTTP Response builder with the 201 status code. +-/ +def created : Builder := + .empty |>.status .created + +/-- +Creates a new HTTP Response builder with the 202 status code. +-/ +def accepted : Builder := + .empty |>.status .accepted + +/-- +Creates a new HTTP Response builder with the 401 status code. +-/ +def unauthorized : Builder := + .empty |>.status .unauthorized + +/-- +Creates a new HTTP Response builder with the 403 status code. +-/ +def forbidden : Builder := + .empty |>.status .forbidden + +/-- +Creates a new HTTP Response builder with the 409 status code. +-/ +def conflict : Builder := + .empty |>.status .conflict + +/-- +Creates a new HTTP Response builder with the 503 status code. +-/ +def serviceUnavailable : Builder := + .empty |>.status .serviceUnavailable + +/-- +Creates a redirect response with the 302 Found status code (temporary redirect). +-/ +def redirect (location : String) : Builder := + Builder.empty + |>.status .found + |>.header! "Location" location + +/-- +Creates a redirect response with the 301 Moved Permanently status code (permanent redirect). +-/ +def redirectPermanent (location : String) : Builder := + Builder.empty + |>.status .movedPermanently + |>.header! "Location" location + +/-- +Creates a redirect response with a configurable status code. +Use `permanent := true` for 301 Moved Permanently, `permanent := false` for 302 Found. +-/ +def redirectWith (location : String) (permanent : Bool) : Builder := + Builder.empty + |>.status (if permanent then .movedPermanently else .found) + |>.header! "Location" location + end Response
diff --git a/src/Std/Internal/Http/Data/Status.lean b/src/Std/Internal/Http/Data/Status.lean index e54b730..8825ba9 100644 --- a/src/Std/Internal/Http/Data/Status.lean +++ b/src/Std/Internal/Http/Data/Status.lean
@@ -609,6 +609,15 @@ def isServerError (c : Status) : Bool := 500 ≤ c.toCode ∧ c.toCode < 600 +/-- +Checks if the status code indicates an error (either client error 4xx or server error 5xx). + +* Reference: https://httpwg.org/specs/rfc9110.html#status.codes +-/ +@[inline] +def isError (c : Status) : Bool := + c.isClientError ∨ c.isServerError + instance : Encode .v11 Status where encode buffer status := buffer |>.writeString (toString <| toCode status)
diff --git a/src/Std/Internal/Http/Data/URI.lean b/src/Std/Internal/Http/Data/URI.lean index 24e11d1..09a808e 100644 --- a/src/Std/Internal/Http/Data/URI.lean +++ b/src/Std/Internal/Http/Data/URI.lean
@@ -40,6 +40,17 @@ | some res => res | none => panic! "invalid request target" +/-- +Creates an origin-form request target from a path string. +The path should start with '/' (e.g., "/api/users" or "/search?q=test"). +Panics if the string is not a valid origin-form request target. +-/ +@[inline] +def originForm! (path : String) : RequestTarget := + match parse? path with + | some (.originForm p q f) => .originForm p q f + | _ => panic! s!"invalid origin-form request target: {path}" + end RequestTarget namespace URI
diff --git a/src/Std/Internal/Http/Data/URI/Basic.lean b/src/Std/Internal/Http/Data/URI/Basic.lean index 039a945..916f4b3 100644 --- a/src/Std/Internal/Http/Data/URI/Basic.lean +++ b/src/Std/Internal/Http/Data/URI/Basic.lean
@@ -211,6 +211,14 @@ { p with segments := (loop p.segments.toList []).toArray } +/-- +Returns the path segments as decoded strings. +Segments that cannot be decoded as UTF-8 are returned as their raw encoded form. +-/ +def toDecodedSegments (p : Path) : Array String := + p.segments.map fun seg => + seg.decode.getD (toString seg) + end Path /-- @@ -319,6 +327,30 @@ query.filter (fun x => x.fst.toByteArray ≠ encodedKey.toByteArray) /-- +Gets the first value of a query parameter by key name, decoded as a string. +Returns `none` if the key is not found or if the value cannot be decoded as UTF-8. +-/ +def get (query : Query) (key : String) : Option String := + match query.find? key with + | none => none + | some none => some "" -- Key exists but has no value + | some (some encoded) => encoded.decode + +/-- +Gets the first value of a query parameter by key name, decoded as a string. +Returns the default value if the key is not found or if the value cannot be decoded. +-/ +def getD (query : Query) (key : String) (default : String) : String := + query.get key |>.getD default + +/-- +Sets a query parameter, replacing all existing values for that key. +Both key and value will be automatically percent-encoded. +-/ +def set (query : Query) (key : String) (value : String) : Query := + query.erase key |>.insert key value + +/-- Converts the query to a properly encoded query string format. Example: "key1=value1&key2=value2&flag" -/
diff --git a/src/Std/Internal/Http/Protocol/H1.lean b/src/Std/Internal/Http/Protocol/H1.lean index f0cf449..d0061bb 100644 --- a/src/Std/Internal/Http/Protocol/H1.lean +++ b/src/Std/Internal/Http/Protocol/H1.lean
@@ -148,7 +148,7 @@ -- Helper Functions private def isChunked (headers : Headers) : Option Bool := - if let some res := headers.get? (.new "transfer-encoding") then + if let some res := headers.get? Header.Name.transferEncoding then let encodings := res.value.split "," |>.toArray.map (·.trimAscii.toString.toLower) if encodings.isEmpty then none @@ -166,14 +166,14 @@ some false private def extractBodyLengthFromHeaders (headers : Headers) : Option Body.Length := - match (headers.get? (.new "content-length"), isChunked headers) with + match (headers.get? Header.Name.contentLength, isChunked headers) with | (some cl, some false) => cl.value.toNat? >>= (some ∘ Body.Length.fixed) | (_, some true) => some Body.Length.chunked | _ => none private def checkMessageHead (message : Message.Head dir) : Option Body.Length := do match dir with - | .receiving => guard (message.headers.get? (.new "host") |>.isSome) + | .receiving => guard (message.headers.get? Header.Name.host |>.isSome) | .sending => pure () if let .receiving := dir then @@ -319,10 +319,10 @@ let size := Writer.determineTransferMode machine.writer let headers := - if messageHead.headers.contains (.new "host") then + if messageHead.headers.contains Header.Name.host then messageHead.headers else if let some host := machine.host then - messageHead.headers.insert (.new "host") host + messageHead.headers.insert Header.Name.host host else messageHead.headers @@ -330,23 +330,23 @@ let headers := let identityOpt := machine.config.identityHeader match dir, identityOpt with - | .receiving, some server => headers.insert (.new "server") server - | .sending, some userAgent => headers.insert (.new "user-agent") userAgent + | .receiving, some server => headers.insert Header.Name.server server + | .sending, some userAgent => headers.insert Header.Name.userAgent userAgent | _, none => headers -- Add Connection: close if needed let headers := - if !machine.keepAlive ∧ !headers.hasEntry (.new "connection") (.new "close") then - headers.insert (.new "connection") (.new "close") + if !machine.keepAlive ∧ !headers.hasEntry Header.Name.connection Header.Value.close then + headers.insert Header.Name.connection Header.Value.close else headers -- Add Content-Length or Transfer-Encoding if needed let headers := - if !(headers.contains (.new "content-length") ∨ headers.contains (.new "transfer-encoding")) then + if !(headers.contains Header.Name.contentLength ∨ headers.contains Header.Name.transferEncoding) then match size with - | .fixed n => headers.insert (.new "content-length") (.ofString! <| toString n) - | .chunked => headers.insert (.new "transfer-encoding") (.new "chunked") + | .fixed n => headers.insert Header.Name.contentLength (.ofString! <| toString n) + | .chunked => headers.insert Header.Name.transferEncoding Header.Value.chunked else headers
diff --git a/tests/lean/run/async_http_body.lean b/tests/lean/run/async_http_body.lean new file mode 100644 index 0000000..e8ce960 --- /dev/null +++ b/tests/lean/run/async_http_body.lean
@@ -0,0 +1,309 @@ +import Std.Internal.Http +import Std.Internal.Async + +open Std.Internal.IO.Async +open Std Http + +-- ============================================================================ +-- collectByteArray tests +-- ============================================================================ + +def testCollectByteArrayExactMax : IO Unit := do + let res ← Async.block do + let data := ByteArray.mk (List.replicate 100 65 |>.toArray) + let body := Body.bytes data + let collected ← body.collectByteArray (some 100) + return collected.size + IO.println res + +/-- +info: 100 +-/ +#guard_msgs in +#eval testCollectByteArrayExactMax + +def testCollectByteArrayOverLimit : IO Unit := do + let res ← Async.block do + let data := ByteArray.mk (List.replicate 101 65 |>.toArray) + let body := Body.bytes data + let _ ← body.collectByteArray (some 100) + return "should not reach here" + IO.println res + +/-- +error: body exceeds limit (100 bytes) +-/ +#guard_msgs in +#eval testCollectByteArrayOverLimit + +def testCollectByteArrayUnderLimit : IO Unit := do + let res ← Async.block do + let data := ByteArray.mk (List.replicate 50 65 |>.toArray) + let body := Body.bytes data + let collected ← body.collectByteArray (some 100) + return collected.size + IO.println res + +/-- +info: 50 +-/ +#guard_msgs in +#eval testCollectByteArrayUnderLimit + +def testCollectByteArrayNoLimit : IO Unit := do + let res ← Async.block do + let data := ByteArray.mk (List.replicate 1000 65 |>.toArray) + let body := Body.bytes data + let collected ← body.collectByteArray none + return collected.size + IO.println res + +/-- +info: 1000 +-/ +#guard_msgs in +#eval testCollectByteArrayNoLimit + +-- ============================================================================ +-- collectString tests +-- ============================================================================ + +def testCollectStringValid : IO Unit := do + let res ← Async.block do + let body : Body := "hello" + body.collectString none + IO.println (repr res) + +/-- +info: some "hello" +-/ +#guard_msgs in +#eval testCollectStringValid + +def testCollectStringInvalidUtf8 : IO Unit := do + let res ← Async.block do + let invalidUtf8 := ByteArray.mk #[0xFF, 0xFE, 0x00, 0x01] + let body := Body.bytes invalidUtf8 + body.collectString none + IO.println (repr res) + +/-- +info: none +-/ +#guard_msgs in +#eval testCollectStringInvalidUtf8 + +def testCollectStringEmpty : IO Unit := do + let res ← Async.block do + let body := Body.empty + body.collectString none + IO.println (repr res) + +/-- +info: some "" +-/ +#guard_msgs in +#eval testCollectStringEmpty + +-- ============================================================================ +-- Streaming body tests +-- ============================================================================ + +def testStreamingBody : IO Unit := do + let res ← Async.block do + let stream ← Body.ByteStream.empty + + background do + discard <| stream.write "hello ".toUTF8 + discard <| stream.write "world".toUTF8 + stream.close + + let body := Body.stream stream + let result ← body.collectByteArray none + return String.fromUTF8! result + IO.println <| res.quote + +/-- +info: "hello world" +-/ +#guard_msgs in +#eval testStreamingBody + +def testStreamingMultipleChunks : IO Unit := do + let count ← Async.block do + let stream ← Body.ByteStream.empty + + background do + for i in [0:3] do + discard <| stream.write s!"chunk{i}".toUTF8 + stream.close + + let body := Body.stream stream + let mut count := 0 + for _ in body do + count := count + 1 + return count + IO.println s!"collected {count} chunks" + +/-- +info: collected 3 chunks +-/ +#guard_msgs in +#eval testStreamingMultipleChunks + +def testStreamingTotalSize : IO Unit := do + let size ← Async.block do + let stream ← Body.ByteStream.empty + + background do + discard <| stream.write "aaaaa".toUTF8 + discard <| stream.write "bbbbb".toUTF8 + discard <| stream.write "ccccc".toUTF8 + stream.close + + let body := Body.stream stream + let collected ← body.collectByteArray none + return collected.size + IO.println size + +/-- +info: 15 +-/ +#guard_msgs in +#eval testStreamingTotalSize + +-- ============================================================================ +-- Empty body tests +-- ============================================================================ + +def testEmptyBodySize : IO Unit := do + let size ← Async.block do + let body := Body.empty + let collected ← body.collectByteArray none + return collected.size + IO.println size + +/-- +info: 0 +-/ +#guard_msgs in +#eval testEmptyBodySize + +def testEmptyBodyLength : IO Unit := do + let isZero ← Async.block do + let body := Body.empty + let len ← body.getContentLength + return (len == .fixed 0) + IO.println isZero + +/-- +info: true +-/ +#guard_msgs in +#eval testEmptyBodyLength + +-- ============================================================================ +-- Content length tests +-- ============================================================================ + +def testContentLengthFixed : IO Unit := do + let len ← Async.block do + let body : Body := "hello" + body.getContentLength + IO.println (repr len) + +/-- +info: Std.Http.Body.Length.fixed 5 +-/ +#guard_msgs in +#eval testContentLengthFixed + +def testContentLengthEmpty : IO Unit := do + let len ← Async.block do + let body := Body.empty + body.getContentLength + IO.println (repr len) + +/-- +info: Std.Http.Body.Length.fixed 0 +-/ +#guard_msgs in +#eval testContentLengthEmpty + +-- ============================================================================ +-- Body coercions +-- ============================================================================ + +def testStringCoercion : IO Unit := do + let size ← Async.block do + let body : Body := "hello" + let collected ← body.collectByteArray none + return collected.size + IO.println size + +/-- +info: 5 +-/ +#guard_msgs in +#eval testStringCoercion + +def testByteArrayCoercion : IO Unit := do + let size ← Async.block do + let data := ByteArray.mk #[1, 2, 3] + let body : Body := data + let collected ← body.collectByteArray none + return collected.size + IO.println size + +/-- +info: 3 +-/ +#guard_msgs in +#eval testByteArrayCoercion + +def testUnitCoercion : IO Unit := do + let size ← Async.block do + let body : Body := () + let collected ← body.collectByteArray none + return collected.size + IO.println size + +/-- +info: 0 +-/ +#guard_msgs in +#eval testUnitCoercion + +-- ============================================================================ +-- Body iteration +-- ============================================================================ + +def testBytesBodyIteration : IO Unit := do + let count ← Async.block do + let body : Body := "hello" + let mut count := 0 + for _ in body do + count := count + 1 + return count + IO.println count + +/-- +info: 1 +-/ +#guard_msgs in +#eval testBytesBodyIteration + +def testEmptyBodyIteration : IO Unit := do + let count ← Async.block do + let body := Body.empty + let mut count := 0 + for _ in body do + count := count + 1 + return count + IO.println count + +/-- +info: 0 +-/ +#guard_msgs in +#eval testEmptyBodyIteration
diff --git a/tests/lean/run/async_http_connection.lean b/tests/lean/run/async_http_connection.lean index e1e36ab..a93ce30 100644 --- a/tests/lean/run/async_http_connection.lean +++ b/tests/lean/run/async_http_connection.lean
@@ -71,7 +71,23 @@ /-- Check if request uses chunked transfer encoding. -/ def isChunkedRequest (req : Request Body) : Bool := - req.head.headers.hasEntry (.new "transfer-encoding") (.ofString! "chunked") + let headers := req.head.headers + if let some res := headers.get? (.new "transfer-encoding") then + let encodings := res.value.split "," |>.toArray.map (·.trimAscii.toString.toLower) + if encodings.isEmpty then + false + else + let chunkedCount := encodings.filter (· == "chunked") |>.size + let lastIsChunked := encodings.back? == some "chunked" + + if chunkedCount > 1 then + false + else if chunkedCount = 1 ∧ ¬lastIsChunked then + false + else + lastIsChunked + else + false /-- Check if request has a specific header with a specific value. -/ def hasHeader (req : Request Body) (name : String) (value : String) : Bool := @@ -370,3 +386,302 @@ expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 13\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nprocessed xml" } + +-- Limits + +#eval + let bigString := String.fromUTF8! (ByteArray.mk (Array.ofFn (n := 257) (fun _ => 65))) + + runTestCase { + name := "Huge String request" + + request := Request.new + |>.method .head + |>.uri! "/api/users" + |>.header! "Host" "api.example.com" + |>.header! bigString "a" + |>.header! "Connection" "close" + |>.body #[] + + handler := fun req => do + if hasMethod req .head + then return Response.ok + |>.header (.ofString! bigString) (.ofString! "ata") + |>.body "" + else return Response.notFound + |>.body () + expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +} + +#eval runTestCase { + name := "Request line too long" + + request := + Request.new + |>.method .get + |>.uri (.originForm (.mk #[URI.EncodedString.encode <| String.ofList (List.replicate 2000 'a')] true) none none) + |>.header! "Host" "api.example.com" + |>.header! "Connection" "close" + |>.body #[] + + handler := fun req => do + return Response.ok + |>.body (toString (toString req.head.uri).length) + + expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +} + +#eval runTestCase { + name := "Header long" + + request := + Request.new + |>.method .get + |>.uri (.originForm (.mk #[URI.EncodedString.encode <| String.ofList (List.replicate 200 'a')] true) none none) + |>.header! "Host" (String.ofList (List.replicate 8230 'a')) + |>.header! "Connection" "close" + |>.body #[] + + handler := fun req => do + return Response.ok + |>.body (toString (toString req.head.uri).length) + + expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +} + +#eval runTestCase { + name := "Too many headers" + + request := Id.run do + let mut req := Request.new + |>.method .get + |>.uri! "/api/data" + |>.header! "Host" "api.example.com" + |>.header! "Connection" "close" + + for i in [0:101] do + req := req |>.header! s!"X-Header-{i}" s!"value{i}" + + return req |>.body #[] + + handler := fun _ => do + return Response.ok + |>.body "success" + + expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +} + +#eval runTestCase { + name := "Header value too long" + + request := Request.new + |>.method .get + |>.uri! "/api/test" + |>.header! "Host" "api.example.com" + |>.header! "X-Long-Value" (String.ofList (List.replicate 9000 'x')) + |>.header! "Connection" "close" + |>.body #[] + + handler := fun _ => do + return Response.ok + |>.body "ok" + + expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +} + +#eval runTestCase { + name := "Total headers size too large" + + request := Id.run do + let mut req := Request.new + |>.method .get + |>.uri! "/api/data" + |>.header! "Host" "api.example.com" + |>.header! "Connection" "close" + + for i in [0:200] do + req := req |>.header! s!"X-Header-{i}" (String.ofList (List.replicate 200 'a')) + return req |>.body #[] + + handler := fun _ => do + return Response.ok + |>.body "success" + + expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +} + +-- Tests + +#eval runTestCase { + name := "Streaming response with fixed Content-Length" + + request := Request.new + |>.method .get + |>.uri! "/stream" + |>.header! "Host" "example.com" + |>.header! "Connection" "close" + |>.body #[] + + handler := fun req => do + let stream ← Body.ByteStream.empty + + background do + for i in [0:3] do + let sleep ← Sleep.mk 5 + sleep.wait + discard <| stream.write s!"chunk{i}\n".toUTF8 + stream.close + + return Response.ok + |>.header (.new "content-length") (.new "21") + |>.body stream + + expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 21\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nchunk0\nchunk1\nchunk2\n" +} + +#eval runTestCase { + name := "Streaming response with setKnownSize fixed" + + request := Request.new + |>.method .get + |>.uri! "/stream-sized" + |>.header! "Host" "example.com" + |>.header! "Connection" "close" + |>.body #[] + + handler := fun _ => do + let stream ← Body.ByteStream.empty + stream.setKnownSize (some (.fixed 15)) + + background do + for i in [0:3] do + discard <| stream.write s!"data{i}".toUTF8 + + stream.close + + return Response.ok + |>.body stream + + expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 15\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\ndata0data1data2" +} + +#eval runTestCase { + name := "Streaming response with chunked encoding" + + request := Request.new + |>.method .get + |>.uri! "/stream-chunked" + |>.header! "Host" "example.com" + |>.header! "Connection" "close" + |>.body #[] + + handler := fun _ => do + let stream ← Body.ByteStream.empty + + background do + discard <| stream.write "hello".toUTF8 + discard <| stream.write "world".toUTF8 + stream.close + return Response.ok + |>.body stream + + expected := "HTTP/1.1 200 OK\x0d\nTransfer-Encoding: chunked\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n5\x0d\nhello\x0d\n5\x0d\nworld\x0d\n0\x0d\n\x0d\n" +} + +#eval runTestCase { + name := "Chunked request with streaming response" + + request := Request.new + |>.method .post + |>.uri! "/" + |>.header! "Host" "example.com" + |>.header! "Transfer-Encoding" "chunked" + |>.header! "Connection" "close" + |>.body #[ + .mk "data1".toUTF8 #[], + .mk "data2".toUTF8 #[] + ] + + handler := fun req => do + if isChunkedRequest req + then + let stream ← Body.ByteStream.empty + background do + for i in [0:2] do + discard <| stream.write s!"response{i}".toUTF8 + stream.close + return Response.ok + |>.header (.new "content-length") (.new "18") + |>.body stream + else + return Response.badRequest |>.body "not chunked" + + expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 18\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nresponse0response1" + chunked := true +} + +#eval runTestCase { + name := "Chunked request with streaming response" + + request := Request.new + |>.method .post + |>.uri! "/" + |>.header! "Host" "example.com" + |>.header! "Transfer-Encoding" "chunked" + |>.header! "Connection" "close" + |>.body #[ + .mk "data1".toUTF8 #[], + .mk "data2".toUTF8 #[] + ] + + handler := fun req => do + if isChunkedRequest req + then + let stream ← Body.ByteStream.empty + background do + for i in [0:2] do + discard <| stream.write s!"response{i}".toUTF8 + stream.close + return Response.ok + |>.header (.new "content-length") (.new "18") + |>.body stream + else + return Response.badRequest + |>.body "not chunked" + + expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 18\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nresponse0response1" + chunked := true +} + +#eval runTestCase { + name := "Chunked request with streaming response and other encodings" + + request := Request.new + |>.method .post + |>.uri! "/" + |>.header! "Host" "example.com" + |>.header! "Transfer-Encoding" "gzip, chunked" + |>.header! "Connection" "close" + |>.body #[ + .mk "data1".toUTF8 #[], + .mk "data2".toUTF8 #[] + ] + + handler := fun req => do + if isChunkedRequest req + then + let stream ← Body.ByteStream.empty + background do + for i in [0:2] do + discard <| stream.write s!"response{i}".toUTF8 + stream.close + return Response.ok + |>.header (.new "content-length") (.new "18") + |>.body stream + else + return Response.badRequest + |>.body "not chunked" + + expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 18\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nresponse0response1" + chunked := true +}
diff --git a/tests/lean/run/async_http_connection_limits.lean b/tests/lean/run/async_http_connection_limits.lean deleted file mode 100644 index 8d0568e..0000000 --- a/tests/lean/run/async_http_connection_limits.lean +++ /dev/null
@@ -1,212 +0,0 @@ -import Std.Internal.Http -import Std.Internal.Async - -open Std.Internal.IO.Async -open Std Http - -structure TestCase where - /-- Descriptive name for the test -/ - name : String - /-- The HTTP request to send -/ - request : Request (Array Chunk) - /-- Handler function to process the request -/ - handler : Request Body → ContextAsync (Response Body) - /-- Expected response string -/ - expected : String - /-- Whether to use chunked encoding -/ - chunked : Bool := false - deriving Inhabited - -def toByteArray (req : Request (Array Chunk)) (chunked := false) : IO ByteArray := Async.block do - let mut data := String.toUTF8 <| toString req.head - let toByteArray (part : Chunk) := Internal.Encode.encode .v11 .empty part |>.toByteArray - for part in req.body do data := data ++ (if chunked then toByteArray part else part.data) - if chunked then data := data ++ toByteArray (Chunk.mk .empty .empty) - return data - -/-- Send multiple requests through a mock connection and return the response data. -/ -def sendRequests (client : Mock.Client) (server : Mock.Server) (reqs : Array (Request (Array Chunk))) - (onRequest : Request Body → ContextAsync (Response Body)) - (chunked : Bool := false) : IO ByteArray := Async.block do - - let mut data := .empty - for req in reqs do data := data ++ (← toByteArray req chunked) - - client.send data - Std.Http.Server.serveConnection server onRequest (fun _ => pure ()) (config := { lingeringTimeout := 3000 }) - |>.run - - let res ← client.recv? - pure <| res.getD .empty - -/-- Run a single test case, comparing actual response against expected response. -/ -def runTest (name : String) (client : Mock.Client) (server : Mock.Server) (req : Request (Array Chunk)) - (handler : Request Body → ContextAsync (Response Body)) (expected : String) (chunked : Bool := false) : - IO Unit := do - let response ← sendRequests client server #[req] handler chunked - let responseData := String.fromUTF8! response - - if responseData != expected then - throw <| IO.userError s! - "Test '{name}' failed:\n\ - Expected:\n{expected}\n\ - Got:\n{responseData}" - -def runTestCase (tc : TestCase) : IO Unit := do - let (client, server) ← Mock.new - runTest tc.name client server tc.request tc.handler tc.expected tc.chunked - --- Request Predicates - -/-- Check if request is a basic GET requests to the specified URI and host. -/ -def isBasicGetRequest (req : Request Body) (uri : String) (host : String) : Bool := - req.head.method == .get ∧ - req.head.version == .v11 ∧ - toString req.head.uri = uri ∧ - req.head.headers.hasEntry (.new "host") (.ofString! host) - -/-- Check if request has a specific Content-Length header. -/ -def hasContentLength (req : Request Body) (length : String) : Bool := - req.head.headers.hasEntry (.new "content-length") (.ofString! length) - -/-- Check if request uses chunked transfer encoding. -/ -def isChunkedRequest (req : Request Body) : Bool := - req.head.headers.hasEntry (.new "transfer-encoding") (.ofString! "chunked") - -/-- Check if request has a specific header with a specific value. -/ -def hasHeader (req : Request Body) (name : String) (value : String) : Bool := - if let some name := Header.Name.ofString? name then - req.head.headers.hasEntry name (.ofString! value) - else - false - -/-- Check if request method matches the expected method. -/ -def hasMethod (req : Request Body) (method : Method) : Bool := - req.head.method == method - -/-- Check if request URI matches the expected URI string. -/ -def hasUri (req : Request Body) (uri : String) : Bool := - toString req.head.uri = uri - --- Limits - -#eval - let bigString := String.fromUTF8! (ByteArray.mk (Array.ofFn (n := 257) (fun _ => 65))) - - runTestCase { - name := "Huge String request" - - request := Request.new - |>.method .head - |>.uri! "/api/users" - |>.header! "Host" "api.example.com" - |>.header! bigString "a" - |>.header! "Connection" "close" - |>.body #[] - - handler := fun req => do - if hasMethod req .head - then return Response.ok - |>.header (.ofString! bigString) (.ofString! "ata") - |>.body "" - else return Response.notFound - |>.body () - expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" -} - -#eval runTestCase { - name := "Request line too long" - - request := - Request.new - |>.method .get - |>.uri (.originForm (.mk #[URI.EncodedString.encode <| String.ofList (List.replicate 2000 'a')] true) none none) - |>.header! "Host" "api.example.com" - |>.header! "Connection" "close" - |>.body #[] - - handler := fun req => do - return Response.ok - |>.body (toString (toString req.head.uri).length) - - expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" -} - -#eval runTestCase { - name := "Header long" - - request := - Request.new - |>.method .get - |>.uri (.originForm (.mk #[URI.EncodedString.encode <| String.ofList (List.replicate 200 'a')] true) none none) - |>.header! "Host" (String.ofList (List.replicate 8230 'a')) - |>.header! "Connection" "close" - |>.body #[] - - handler := fun req => do - return Response.ok - |>.body (toString (toString req.head.uri).length) - - expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" -} - -#eval runTestCase { - name := "Too many headers" - - request := Id.run do - let mut req := Request.new - |>.method .get - |>.uri! "/api/data" - |>.header! "Host" "api.example.com" - |>.header! "Connection" "close" - - for i in [0:101] do - req := req |>.header! s!"X-Header-{i}" s!"value{i}" - - return req |>.body #[] - - handler := fun _ => do - return Response.ok - |>.body "success" - - expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" -} - -#eval runTestCase { - name := "Header value too long" - - request := Request.new - |>.method .get - |>.uri! "/api/test" - |>.header! "Host" "api.example.com" - |>.header! "X-Long-Value" (String.ofList (List.replicate 9000 'x')) - |>.header! "Connection" "close" - |>.body #[] - - handler := fun _ => do - return Response.ok - |>.body "ok" - - expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" -} - -#eval runTestCase { - name := "Total headers size too large" - - request := Id.run do - let mut req := Request.new - |>.method .get - |>.uri! "/api/data" - |>.header! "Host" "api.example.com" - |>.header! "Connection" "close" - - for i in [0:200] do - req := req |>.header! s!"X-Header-{i}" (String.ofList (List.replicate 200 'a')) - return req |>.body #[] - - handler := fun _ => do - return Response.ok - |>.body "success" - - expected := "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" -}
diff --git a/tests/lean/run/async_http_connection_streaming.lean b/tests/lean/run/async_http_connection_streaming.lean deleted file mode 100644 index a271642..0000000 --- a/tests/lean/run/async_http_connection_streaming.lean +++ /dev/null
@@ -1,280 +0,0 @@ -import Std.Internal.Http -import Std.Internal.Async - -open Std.Internal.IO.Async -open Std Http - -structure TestCase where - /-- Descriptive name for the test -/ - name : String - /-- The HTTP request to send -/ - request : Request (Array Chunk) - /-- Handler function to process the request -/ - handler : Request Body → ContextAsync (Response Body) - /-- Expected response string -/ - expected : String - /-- Whether to use chunked encoding -/ - chunked : Bool := false - deriving Inhabited - -def toByteArray (req : Request (Array Chunk)) (chunked := false) : IO ByteArray := Async.block do - let mut data := String.toUTF8 <| toString req.head - let toByteArray (part : Chunk) := Internal.Encode.encode .v11 .empty part |>.toByteArray - for part in req.body do data := data ++ (if chunked then toByteArray part else part.data) - if chunked then data := data ++ toByteArray (Chunk.mk .empty .empty) - return data - -/-- Send multiple requests through a mock connection and return the response data. -/ -def sendRequests (client : Mock.Client) (server : Mock.Server) (reqs : Array (Request (Array Chunk))) - (onRequest : Request Body → ContextAsync (Response Body)) - (chunked : Bool := false) : IO ByteArray := Async.block do - let mut data := .empty - for req in reqs do data := data ++ (← toByteArray req chunked) - - client.send data - Std.Http.Server.serveConnection server onRequest (fun _ => pure ()) (config := { lingeringTimeout := 3000 }) - |>.run - - let res ← client.recv? - pure <| res.getD .empty - -/-- Run a single test case, comparing actual response against expected response. -/ -def runTest (name : String) (client : Mock.Client) (server : Mock.Server) (req : Request (Array Chunk)) - (handler : Request Body → ContextAsync (Response Body)) (expected : String) (chunked : Bool := false) : - IO Unit := do - let response ← sendRequests client server #[req] handler chunked - let responseData := String.fromUTF8! response - - if responseData != expected then - throw <| IO.userError s! - "Test '{name}' failed:\n\ - Expected:\n{expected}\n\ - Got:\n{responseData}" - -def runTestCase (tc : TestCase) : IO Unit := do - let (client, server) ← Mock.new - runTest tc.name client server tc.request tc.handler tc.expected tc.chunked - --- Request Predicates - -/-- Check if request is a basic GET requests to the specified URI and host. -/ -def isBasicGetRequest (req : Request Body) (uri : String) (host : String) : Bool := - req.head.method == .get ∧ - req.head.version == .v11 ∧ - toString req.head.uri = uri ∧ - req.head.headers.hasEntry (.new "host") (.ofString! host) - -/-- Check if request has a specific Content-Length header. -/ -def hasContentLength (req : Request Body) (length : String) : Bool := - req.head.headers.hasEntry (.new "content-length") (.ofString! length) - -/-- Check if request uses chunked transfer encoding. -/ -def isChunkedRequest (req : Request Body) : Bool := - let headers := req.head.headers - if let some res := headers.get? (.new "transfer-encoding") then - let encodings := res.value.split "," |>.toArray.map (·.trimAscii.toString.toLower) - if encodings.isEmpty then - false - else - let chunkedCount := encodings.filter (· == "chunked") |>.size - let lastIsChunked := encodings.back? == some "chunked" - - if chunkedCount > 1 then - false - else if chunkedCount = 1 ∧ ¬lastIsChunked then - false - else - lastIsChunked - else - false - -/-- Check if request has a specific header with a specific value. -/ -def hasHeader (req : Request Body) (name : String) (value : String) : Bool := - if let some name := Header.Name.ofString? name then - req.head.headers.hasEntry name (.ofString! value) - else - false - -/-- Check if request method matches the expected method. -/ -def hasMethod (req : Request Body) (method : Method) : Bool := - req.head.method == method - -/-- Check if request URI matches the expected URI string. -/ -def hasUri (req : Request Body) (uri : String) : Bool := - toString req.head.uri = uri - --- Tests - -#eval runTestCase { - name := "Streaming response with fixed Content-Length" - - request := Request.new - |>.method .get - |>.uri! "/stream" - |>.header! "Host" "example.com" - |>.header! "Connection" "close" - |>.body #[] - - handler := fun req => do - let stream ← Body.ByteStream.empty - - background do - for i in [0:3] do - let sleep ← Sleep.mk 5 - sleep.wait - discard <| stream.write s!"chunk{i}\n".toUTF8 - stream.close - - return Response.ok - |>.header (.new "content-length") (.new "21") - |>.body stream - - expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 21\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nchunk0\nchunk1\nchunk2\n" -} - -#eval runTestCase { - name := "Streaming response with setKnownSize fixed" - - request := Request.new - |>.method .get - |>.uri! "/stream-sized" - |>.header! "Host" "example.com" - |>.header! "Connection" "close" - |>.body #[] - - handler := fun _ => do - let stream ← Body.ByteStream.empty - stream.setKnownSize (some (.fixed 15)) - - background do - for i in [0:3] do - discard <| stream.write s!"data{i}".toUTF8 - - stream.close - - return Response.ok - |>.body stream - - expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 15\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\ndata0data1data2" -} - -#eval runTestCase { - name := "Streaming response with chunked encoding" - - request := Request.new - |>.method .get - |>.uri! "/stream-chunked" - |>.header! "Host" "example.com" - |>.header! "Connection" "close" - |>.body #[] - - handler := fun _ => do - let stream ← Body.ByteStream.empty - - background do - discard <| stream.write "hello".toUTF8 - discard <| stream.write "world".toUTF8 - stream.close - return Response.ok - |>.body stream - - expected := "HTTP/1.1 200 OK\x0d\nTransfer-Encoding: chunked\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n5\x0d\nhello\x0d\n5\x0d\nworld\x0d\n0\x0d\n\x0d\n" -} - -#eval runTestCase { - name := "Chunked request with streaming response" - - request := Request.new - |>.method .post - |>.uri! "/" - |>.header! "Host" "example.com" - |>.header! "Transfer-Encoding" "chunked" - |>.header! "Connection" "close" - |>.body #[ - .mk "data1".toUTF8 #[], - .mk "data2".toUTF8 #[] - ] - - handler := fun req => do - if isChunkedRequest req - then - let stream ← Body.ByteStream.empty - background do - for i in [0:2] do - discard <| stream.write s!"response{i}".toUTF8 - stream.close - return Response.ok - |>.header (.new "content-length") (.new "18") - |>.body stream - else - return Response.badRequest |>.body "not chunked" - - expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 18\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nresponse0response1" - chunked := true -} - -#eval runTestCase { - name := "Chunked request with streaming response" - - request := Request.new - |>.method .post - |>.uri! "/" - |>.header! "Host" "example.com" - |>.header! "Transfer-Encoding" "chunked" - |>.header! "Connection" "close" - |>.body #[ - .mk "data1".toUTF8 #[], - .mk "data2".toUTF8 #[] - ] - - handler := fun req => do - if isChunkedRequest req - then - let stream ← Body.ByteStream.empty - background do - for i in [0:2] do - discard <| stream.write s!"response{i}".toUTF8 - stream.close - return Response.ok - |>.header (.new "content-length") (.new "18") - |>.body stream - else - return Response.badRequest - |>.body "not chunked" - - expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 18\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nresponse0response1" - chunked := true -} - -#eval runTestCase { - name := "Chunked request with streaming response and other encodings" - - request := Request.new - |>.method .post - |>.uri! "/" - |>.header! "Host" "example.com" - |>.header! "Transfer-Encoding" "gzip, chunked" - |>.header! "Connection" "close" - |>.body #[ - .mk "data1".toUTF8 #[], - .mk "data2".toUTF8 #[] - ] - - handler := fun req => do - if isChunkedRequest req - then - let stream ← Body.ByteStream.empty - background do - for i in [0:2] do - discard <| stream.write s!"response{i}".toUTF8 - stream.close - return Response.ok - |>.header (.new "content-length") (.new "18") - |>.body stream - else - return Response.badRequest - |>.body "not chunked" - - expected := "HTTP/1.1 200 OK\x0d\nContent-Length: 18\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nresponse0response1" - chunked := true -}
diff --git a/tests/lean/run/async_http_headers.lean b/tests/lean/run/async_http_headers.lean new file mode 100644 index 0000000..ae545e9 --- /dev/null +++ b/tests/lean/run/async_http_headers.lean
@@ -0,0 +1,209 @@ +import Std.Internal.Http.Data.Headers + +open Std Http + +-- ============================================================================ +-- Headers.merge tests +-- ============================================================================ + +/-- +info: 2 +-/ +#guard_msgs in +#eval do + -- Merge with overlapping keys - multimap behavior keeps both values + let h1 := Headers.empty.insert (.ofString! "content-type") (.ofString! "text/plain") + let h2 := Headers.empty.insert (.ofString! "content-type") (.ofString! "application/json") + let merged := h1.merge h2 + -- After merge, content-type should have both values + IO.println (merged.getAll? (.ofString! "content-type")).get!.size + +/-- +info: 3 +-/ +#guard_msgs in +#eval do + -- Merge with non-overlapping keys + let h1 := Headers.empty.insert (.ofString! "x-custom-1") (.ofString! "value1") + let h2 := Headers.empty + |>.insert (.ofString! "x-custom-2") (.ofString! "value2") + |>.insert (.ofString! "x-custom-3") (.ofString! "value3") + let merged := h1.merge h2 + IO.println merged.size + +-- ============================================================================ +-- Headers.getAll tests (multi-value headers) +-- ============================================================================ + +/-- +info: 3 +-/ +#guard_msgs in +#eval do + -- Multiple values for same header + let headers := Headers.empty + |>.insert (.ofString! "accept") (.ofString! "text/html") + |>.insert (.ofString! "accept") (.ofString! "application/json") + |>.insert (.ofString! "accept") (.ofString! "text/plain") + if let some values := headers.getAll? (.ofString! "accept") then + IO.println values.size + else + IO.println "not found" + +-- ============================================================================ +-- Case-insensitive header lookup +-- ============================================================================ + +#guard + let headers := Headers.empty.insert (.ofString! "content-type") (.ofString! "text/plain") + -- All these should find the same header (case-insensitive) + headers.contains (.ofString! "content-type") && + headers.contains (.ofString! "Content-Type") && + headers.contains (.ofString! "CONTENT-TYPE") + +/-- +info: text/plain +-/ +#guard_msgs in +#eval do + let headers := Headers.empty.insert (.ofString! "Content-Type") (.ofString! "text/plain") + if let some v := headers.get? (.ofString! "content-type") then + IO.println v.value + else + IO.println "not found" + +-- ============================================================================ +-- Invalid header name characters +-- ============================================================================ + +#guard (Header.Name.ofString? "valid-name").isSome +#guard (Header.Name.ofString? "").isNone -- empty +#guard (Header.Name.ofString? "has space").isNone -- space invalid +#guard (Header.Name.ofString? "has(paren").isNone -- parentheses invalid +#guard (Header.Name.ofString? "has,comma").isNone -- comma invalid + +-- ============================================================================ +-- Header value validation +-- ============================================================================ + +#guard (Header.Value.ofString? "valid value").isSome +#guard (Header.Value.ofString? "value with tab\t").isSome -- tab is valid + +-- ============================================================================ +-- HasAll proofs +-- ============================================================================ + +/-- +info: true +-/ +#guard_msgs in +#eval do + let headers := Headers.empty + |>.insert (.ofString! "content-type") (.ofString! "application/json") + |>.insert (.ofString! "host") (.ofString! "example.com") + |>.insert (.ofString! "accept") (.ofString! "text/plain") + + -- Check HasAll for a subset of headers + let hasAll : Bool := match Headers.HasAll.decidable (h := headers) (l := ["content-type", "host"]) with + | isTrue _ => true + | isFalse _ => false + IO.println hasAll + +/-- +info: false +-/ +#guard_msgs in +#eval do + let headers := Headers.empty + |>.insert (.ofString! "content-type") (.ofString! "application/json") + + -- Check HasAll for headers not present + let hasAll : Bool := match Headers.HasAll.decidable (h := headers) (l := ["content-type", "missing-header"]) with + | isTrue _ => true + | isFalse _ => false + IO.println hasAll + +-- ============================================================================ +-- Headers iteration (toArray, toList) +-- ============================================================================ + +/-- +info: 2 +-/ +#guard_msgs in +#eval do + let headers := Headers.empty + |>.insert (.ofString! "a") (.ofString! "1") + |>.insert (.ofString! "b") (.ofString! "2") + IO.println headers.toArray.size + +/-- +info: 2 +-/ +#guard_msgs in +#eval do + let headers := Headers.empty + |>.insert (.ofString! "x") (.ofString! "y") + |>.insert (.ofString! "z") (.ofString! "w") + IO.println headers.toList.length + +-- ============================================================================ +-- Header name constants +-- ============================================================================ + +#guard Header.Name.contentType == .ofString! "content-type" +#guard Header.Name.contentLength == .ofString! "content-length" +#guard Header.Name.host == .ofString! "host" +#guard Header.Name.authorization == .ofString! "authorization" +#guard Header.Name.userAgent == .ofString! "user-agent" +#guard Header.Name.accept == .ofString! "accept" +#guard Header.Name.connection == .ofString! "connection" +#guard Header.Name.transferEncoding == .ofString! "transfer-encoding" + +-- ============================================================================ +-- Using header name constants in practice +-- ============================================================================ + +/-- +info: application/json +-/ +#guard_msgs in +#eval do + let headers := Headers.empty + |>.insert Header.Name.contentType (.ofString! "application/json") + if let some v := headers.get? Header.Name.contentType then + IO.println v.value + else + IO.println "not found" + +-- ============================================================================ +-- Headers filter and map +-- ============================================================================ + +/-- +info: 1 +-/ +#guard_msgs in +#eval do + let headers := Headers.empty + |>.insert (.ofString! "x-custom") (.ofString! "keep") + |>.insert (.ofString! "x-remove") (.ofString! "remove") + let filtered := headers.filter (fun name _ => name.is "x-custom") + IO.println filtered.size + +-- ============================================================================ +-- Headers update +-- ============================================================================ + +/-- +info: updated +-/ +#guard_msgs in +#eval do + let headers := Headers.empty + |>.insert (.ofString! "x-value") (.ofString! "original") + let updated := headers.update (.ofString! "x-value") (fun _ => .ofString! "updated") + if let some v := updated.get? (.ofString! "x-value") then + IO.println v.value + else + IO.println "not found"
diff --git a/tests/lean/run/async_http_parser.lean b/tests/lean/run/async_http_parser.lean index a3713a2..3b0bfd3 100644 --- a/tests/lean/run/async_http_parser.lean +++ b/tests/lean/run/async_http_parser.lean
@@ -1,11 +1,26 @@ +/- +Copyright (c) 2025 Lean FRO, LLC. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Sofia Rodrigues +-/ import Std.Internal.Http.Protocol.H1.Parser open Std.Http.Protocol +/-! +# HTTP/1.1 Parser Tests + +Comprehensive tests for H1 protocol parsing including chunks, headers, +request lines, status lines, and edge cases. +-/ + def runParser (parser : Std.Internal.Parsec.ByteArray.Parser α) (s : String) : IO α := IO.ofExcept (parser.run s.toUTF8) --- Chunk parsing tests +-- ============================================================================ +-- Chunk Parsing Tests +-- ============================================================================ + /-- info: 16 / #[] / "adasdssdabcdabde" -/ @@ -37,13 +52,16 @@ | some (size, ext, body) => IO.println s!"{size} / {ext} / {String.fromUTF8! body.toByteArray |>.quote}" | none => IO.println "end chunk" --- Chunk size parsing tests (refactored to use runParser) +-- ============================================================================ +-- Chunk Size Parsing Tests +-- ============================================================================ + /-- info: 16 / #[(abc, none), (def, none), (g, (some h))] -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseChunkSize {}) "10;abc;def;g=h\r\n" + let result ← runParser (H1.parseChunkSize {}) "10;abc;def;g=h\r\n" IO.println s!"{result.1} / {result.2}" /-- @@ -51,7 +69,7 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseChunkSize {}) "0;abc;def;g=h\r\n" + let result ← runParser (H1.parseChunkSize {}) "0;abc;def;g=h\r\n" IO.println s!"{result.1} / {result.2}" /-- @@ -59,7 +77,7 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseChunkSize {}) "FFF\r\n" + let result ← runParser (H1.parseChunkSize {}) "FFF\r\n" IO.println s!"{result.1} / {result.2}" /-- @@ -67,16 +85,79 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseChunkSize {}) "1;name=\"value with spaces\"\r\n" + let result ← runParser (H1.parseChunkSize {}) "1;name=\"value with spaces\"\r\n" IO.println s!"{result.1} / {result.2}" --- Single header parsing tests (refactored to use runParser) +/-- +info: 0 / #[] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseChunkSize {}) "0\r\n" + IO.println s!"{result.1} / {result.2}" + +/-- +info: 16 / #[] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseChunkSize {}) "10\r\n" + IO.println s!"{result.1} / {result.2}" + +/-- +info: 255 / #[] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseChunkSize {}) "FF\r\n" + IO.println s!"{result.1} / {result.2}" + +/-- +info: 255 / #[] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseChunkSize {}) "ff\r\n" + IO.println s!"{result.1} / {result.2}" + +-- ============================================================================ +-- Chunk Extension Tests +-- ============================================================================ + +/-- +info: 10 / #[(ext1, none)] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseChunkSize {}) "A;ext1\r\n" + IO.println s!"{result.1} / {result.2}" + +/-- +info: 10 / #[(name, (some value))] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseChunkSize {}) "A;name=value\r\n" + IO.println s!"{result.1} / {result.2}" + +/-- +info: 10 / #[(name, (some (value with spaces)))] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseChunkSize {}) "A;name=\"value with spaces\"\r\n" + IO.println s!"{result.1} / {result.2}" + +-- ============================================================================ +-- Single Header Parsing Tests +-- ============================================================================ + /-- info: User-Agent / "Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0" -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseSingleHeader {}) "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0\r\n" + let result ← runParser (H1.parseSingleHeader {}) "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0\r\n" match result with | some (k, v) => IO.println s!"{k} / {v.quote}" | none => IO.println "end" @@ -86,7 +167,7 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseSingleHeader {}) "Content-Type: application/json; charset=utf-8\r\n" + let result ← runParser (H1.parseSingleHeader {}) "Content-Type: application/json; charset=utf-8\r\n" match result with | some (k, v) => IO.println s!"{k} / {v.quote}" | none => IO.println "end" @@ -96,7 +177,7 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseSingleHeader {}) "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\r\n" + let result ← runParser (H1.parseSingleHeader {}) "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\r\n" match result with | some (k, v) => IO.println s!"{k} / {v}" | none => IO.println "end" @@ -106,17 +187,97 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseSingleHeader {}) "\r\n" + let result ← runParser (H1.parseSingleHeader {}) "\r\n" match result with | some (k, v) => IO.println s!"{k} / {v}" | none => IO.println "end" /-- +info: X-Custom-Header / value with multiple spaces +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseSingleHeader {}) "X-Custom-Header: value with multiple spaces\r\n" + match result with + | some (k, v) => IO.println s!"{k} / {v}" + | none => IO.println "end" + +/-- +info: Valid-Name / value +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseSingleHeader {}) "Valid-Name: value\r\n" + match result with + | some (k, v) => IO.println s!"{k} / {v}" + | none => IO.println "end" + +/-- +info: X-Custom-123 / test +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseSingleHeader {}) "X-Custom-123: test\r\n" + match result with + | some (k, v) => IO.println s!"{k} / {v}" + | none => IO.println "end" + +/-- +info: X-Special / value with spaces and !@#$%^&*() +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseSingleHeader {}) "X-Special: value with spaces and !@#$%^&*()\r\n" + match result with + | some (k, v) => IO.println s!"{k} / {v}" + | none => IO.println "end" + +-- ============================================================================ +-- Header Edge Cases +-- ============================================================================ + +-- Empty header value requires at least one character and fails +/-- +error: offset 8: expected at least one char +-/ +#guard_msgs in +#eval show IO _ from do + let _ ← runParser (H1.parseSingleHeader {}) "X-Empty:\r\n" + IO.println "should not reach" + +-- Tab character is preserved in header value +/-- +info: X-Tab / "\t" +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseSingleHeader {}) "X-Tab:\t\r\n" + match result with + | some (k, v) => IO.println s!"{k} / {v.quote}" + | none => IO.println "end" + +-- Long header values (near limit of 8192) +/-- +info: X-Long / 8000 chars +-/ +#guard_msgs in +#eval show IO _ from do + let longValue := String.ofList (List.replicate 8000 'x') + let result ← runParser (H1.parseSingleHeader {}) s!"X-Long: {longValue}\r\n" + match result with + | some (k, v) => IO.println s!"{k} / {v.length} chars" + | none => IO.println "end" + +-- ============================================================================ +-- Trailer Parsing Tests +-- ============================================================================ + +/-- error: offset 0: unexpected end of input -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseTrailers {}) "" + let result ← runParser (H1.parseTrailers {}) "" IO.println s!"{result}" /-- @@ -124,7 +285,7 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseTrailers {}) "\r\n" + let result ← runParser (H1.parseTrailers {}) "\r\n" IO.println s!"{result}" /-- @@ -132,16 +293,35 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseTrailers {}) "X-Checksum: abc123\r\nX-Timestamp: 2023-01-01T12:00:00Z\r\n\r\n" + let result ← runParser (H1.parseTrailers {}) "X-Checksum: abc123\r\nX-Timestamp: 2023-01-01T12:00:00Z\r\n\r\n" IO.println s!"{result}" --- Request line parsing tests (refactored to use runParser) +/-- +info: #[(X-Checksum, abc123)] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseTrailers {}) "X-Checksum: abc123\r\n\r\n" + IO.println s!"{result}" + +/-- +info: #[(X-First, value1), (X-Second, value2)] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseTrailers {}) "X-First: value1\r\nX-Second: value2\r\n\r\n" + IO.println s!"{result}" + +-- ============================================================================ +-- Request Line Parsing Tests +-- ============================================================================ + /-- info: Std.Http.Method.get / Std.Http.RequestTarget.originForm { segments := #["ata", ""], absolute := true } none none / Std.Http.Version.v11 -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseRequestLine {}) "GET /ata/ HTTP/1.1\r\n" + let result ← runParser (H1.parseRequestLine {}) "GET /ata/ HTTP/1.1\r\n" IO.println s!"{repr result.method} / {repr result.uri} / {repr result.version}" /-- @@ -149,7 +329,7 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseRequestLine {}) "POST /api/v1/users HTTP/1.1\r\n" + let result ← runParser (H1.parseRequestLine {}) "POST /api/v1/users HTTP/1.1\r\n" IO.println s!"{repr result.method} / {repr result.uri} / {repr result.version}" /-- @@ -160,7 +340,7 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseRequestLine {}) "PUT /data?param1=value1¶m2=value2 HTTP/1.1\r\n" + let result ← runParser (H1.parseRequestLine {}) "PUT /data?param1=value1¶m2=value2 HTTP/1.1\r\n" IO.println s!"{repr result.method} / {repr result.uri} / {repr result.version}" /-- @@ -168,7 +348,7 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseRequestLine {}) "DELETE /items/123 HTTP/1.1\r\n" + let result ← runParser (H1.parseRequestLine {}) "DELETE /items/123 HTTP/1.1\r\n" IO.println s!"{repr result.method} / {repr result.uri} / {repr result.version}" /-- @@ -176,7 +356,7 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseRequestLine {}) "HEAD / HTTP/1.1\r\n" + let result ← runParser (H1.parseRequestLine {}) "HEAD / HTTP/1.1\r\n" IO.println s!"{repr result.method} / {repr result.uri} / {repr result.version}" /-- @@ -184,24 +364,149 @@ -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseRequestLine {}) "OPTIONS * HTTP/1.1\r\n" + let result ← runParser (H1.parseRequestLine {}) "OPTIONS * HTTP/1.1\r\n" IO.println s!"{repr result.method} / {repr result.uri} / {repr result.version}" --- Additional edge case tests /-- -info: 0 / #[] +info: Std.Http.Method.get / Std.Http.Version.v11 -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseChunkSize {}) "0\r\n" - IO.println s!"{result.1} / {result.2}" + let result ← runParser (H1.parseRequestLine {}) "GET / HTTP/1.1\r\n" + IO.println s!"{repr result.method} / {repr result.version}" + +-- ============================================================================ +-- All Standard HTTP Methods +-- ============================================================================ /-- -info: X-Custom-Header / value with multiple spaces +info: Std.Http.Method.head -/ #guard_msgs in #eval show IO _ from do - let result ← runParser (Std.Http.Protocol.H1.parseSingleHeader {}) "X-Custom-Header: value with multiple spaces\r\n" - match result with - | some (k, v) => IO.println s!"{k} / {v}" - | none => IO.println "end" + let result ← runParser (H1.parseRequestLine {}) "HEAD / HTTP/1.1\r\n" + IO.println s!"{repr result.method}" + +/-- +info: Std.Http.Method.put +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseRequestLine {}) "PUT /resource HTTP/1.1\r\n" + IO.println s!"{repr result.method}" + +/-- +info: Std.Http.Method.patch +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseRequestLine {}) "PATCH /resource HTTP/1.1\r\n" + IO.println s!"{repr result.method}" + +/-- +info: Std.Http.Method.options +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseRequestLine {}) "OPTIONS * HTTP/1.1\r\n" + IO.println s!"{repr result.method}" + +/-- +info: Std.Http.Method.trace +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseRequestLine {}) "TRACE / HTTP/1.1\r\n" + IO.println s!"{repr result.method}" + +/-- +info: Std.Http.Method.connect +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseRequestLine {}) "CONNECT example.com:443 HTTP/1.1\r\n" + IO.println s!"{repr result.method}" + +-- ============================================================================ +-- Invalid HTTP Versions +-- ============================================================================ + +/-- +error: offset 14: expected value but got none +-/ +#guard_msgs in +#eval show IO _ from do + let _ ← runParser (H1.parseRequestLine {}) "GET / HTTP/1.0\r\n" + IO.println "should not reach" + +/-- +info: Std.Http.Method.get / Std.Http.Version.v20 +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseRequestLine {}) "GET / HTTP/2.0\r\n" + IO.println s!"{repr result.method} / {repr result.version}" + +/-- +error: offset 14: expected value but got none +-/ +#guard_msgs in +#eval show IO _ from do + let _ ← runParser (H1.parseRequestLine {}) "GET / HTTP/3.1\r\n" + IO.println "should not reach" + +-- ============================================================================ +-- Case-Sensitive Method Names +-- ============================================================================ + +/-- +error: offset 0: expected: '80' +-/ +#guard_msgs in +#eval show IO _ from do + let _ ← runParser (H1.parseRequestLine {}) "get / HTTP/1.1\r\n" + IO.println "should not reach" + +/-- +error: offset 1: expected: '69' +-/ +#guard_msgs in +#eval show IO _ from do + let _ ← runParser (H1.parseRequestLine {}) "Get / HTTP/1.1\r\n" + IO.println "should not reach" + +/-- +error: offset 1: expected: '65' +-/ +#guard_msgs in +#eval show IO _ from do + let _ ← runParser (H1.parseRequestLine {}) "Post / HTTP/1.1\r\n" + IO.println "should not reach" + +-- ============================================================================ +-- Status Line Parsing Tests +-- ============================================================================ + +/-- +info: Std.Http.Status.ok / Std.Http.Version.v11 +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseStatusLine {}) "HTTP/1.1 200 OK\r\n" + IO.println s!"{repr result.status} / {repr result.version}" + +/-- +info: Std.Http.Status.notFound / Std.Http.Version.v11 +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseStatusLine {}) "HTTP/1.1 404 Not Found\r\n" + IO.println s!"{repr result.status} / {repr result.version}" + +/-- +info: Std.Http.Status.internalServerError / Std.Http.Version.v11 +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser (H1.parseStatusLine {}) "HTTP/1.1 500 Internal Server Error\r\n" + IO.println s!"{repr result.status} / {repr result.version}"
diff --git a/tests/lean/run/async_http_raw.lean b/tests/lean/run/async_http_raw.lean deleted file mode 100644 index 13a81c9..0000000 --- a/tests/lean/run/async_http_raw.lean +++ /dev/null
@@ -1,382 +0,0 @@ -import Std.Internal.Http -import Std.Internal.Async - -open Std.Internal.IO.Async -open Std Http - -structure Result where - responseSent : Nat - data : ByteArray - -def requestToByteArray (req : Request (Array Chunk)) (chunked := false) : IO ByteArray := Async.block do - let mut data := String.toUTF8 <| toString req.head - let toByteArray (part : Chunk) := Internal.Encode.encode .v11 .empty part |>.toByteArray - for part in req.body do data := data ++ (if chunked then toByteArray part else part.data) - if chunked then data := data ++ toByteArray (Chunk.mk .empty .empty) - return data - -/-- Send raw byte stream through a mock connection and return the response data. -/ -def sendRawBytes (pair : Mock.Client × Mock.Server) (data : ByteArray) - (onRequest : Request Body → ContextAsync (Response Body)) (config : Config := { lingeringTimeout := 3000 }) : IO ByteArray := Async.block do - let (client, server) := pair - - client.send data - Std.Http.Server.serveConnection server onRequest (fun _ => pure ()) config - |>.runIn (← CancellationContext.new) - - let res ← client.recv? - pure <| res.getD .empty - -def testSizeLimit (pair : Mock.Client × Mock.Server) : IO Unit := do - let handler : Request Body → ContextAsync (Response Body) := fun (req : Request Body) => do - let mut size := 0 - for i in req.body do - size := size + i.size - if size > 10 then - return Response.new - |>.status .payloadTooLarge - |>.header! "Connection" "close" - |>.body .empty - - return Response.new - |>.status .ok - |>.body "hello robert" - - -- Manually construct the byte stream - let mut data := .empty - data := data ++ (← requestToByteArray ( - Request.new - |>.uri! "/ata/po" - |>.header! "Content-Length" "4" - |>.header! "Host" "." - |>.body #[.mk "test".toUTF8 #[]])) - data := data ++ (← requestToByteArray ( - Request.new - |>.uri! "/ata/po" - |>.header! "Content-Length" "13" - |>.header! "Connection" "close" - |>.header! "Host" "." - |>.body #[.mk "testtesttests".toUTF8 #[]])) - data := data ++ (← requestToByteArray ( - Request.new - |>.uri! "/ata/po" - |>.header! "Content-Length" "4" - |>.header! "Host" "." - |>.body #[.mk "test".toUTF8 #[]])) - - let response ← sendRawBytes pair data handler - - let responseData := String.fromUTF8! response - IO.println <| String.quote responseData - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 12\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nhello robertHTTP/1.1 413 Payload Too Large\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO _ from do testSizeLimit (← Mock.new) - -def testBasicRequest : IO Unit := do - let pair ← Mock.new - - let handler := fun (_ : Request Body) => do - return Response.new - |>.status .ok - |>.header! "Custom-Header" "test-value" - |>.header! "Connection" "close" - |>.body "Hello World" - - let data ← requestToByteArray ( - Request.new - |>.uri! "/hello" - |>.header! "Host" "localhost" - |>.header! "User-Agent" "TestClient/1.0" - |>.header! "Connection" "close" - |>.header! "Content-Length" "0" - |>.method .get - |>.body #[.mk "".toUTF8 #[]]) - - let response ← sendRawBytes pair data handler - - let responseData := String.fromUTF8! response - IO.println s!"{responseData.quote}" - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 11\x0d\nConnection: close\x0d\nCustom-Header: test-value\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nHello World" --/ -#guard_msgs in -#eval show IO _ from do testBasicRequest - -def testPostRequest : IO Unit := do - let pair ← Mock.new - - let handler := fun (req : Request Body) => do - let mut body := "" - for chunk in req.body do - body := body ++ String.fromUTF8! chunk.data - - return Response.new - |>.status .ok - |>.header! "Content-Type" "application/json" - |>.header! "Connection" "close" - |>.body s!"Received: {body}" - - let data ← requestToByteArray ( - Request.new - |>.uri! "/api/data" - |>.method .post - |>.header! "Host" "localhost" - |>.header! "Content-Type" "application/json" - |>.header! "Content-Length" "25" - |>.header! "Connection" "close" - |>.body #[.mk "{\"name\": \"test\", \"id\": 1}".toUTF8 #[]]) - - let response ← sendRawBytes pair data handler - - let responseData := String.fromUTF8! response - IO.println s!"{responseData.quote}" - -def test100Continue : IO Unit := do - let pair ← Mock.new - - let handler := fun (req : Request Body) => do - let expectHeader := req.head.headers.getLast? (.new "expect") |>.getD (.new "") - if expectHeader.is "100-continue" then - return Response.new - |>.status .continue - |>.header! "Connection" "close" - |>.body Body.empty - else - return Response.new - |>.status .ok - |>.body "Request processed" - - let data ← requestToByteArray ( - Request.new - |>.uri! "/" - |>.method .get - |>.header! "Host" "example.com" - |>.header! "Content-Length" "1" - |>.header! "Expect" "100-continue" - |>.header! "Connection" "close" - |>.body #[.mk "a".toUTF8 #[]]) - - let response ← sendRawBytes pair data handler - - let responseData := String.fromUTF8! response - IO.println s!"{responseData.quote}" - -/-- -info: "HTTP/1.1 100 Continue\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO _ from do test100Continue - -def testMaxRequestSize : IO Unit := do - let pair ← Mock.new - - let handler := fun (req : Request Body) => do - let mut totalSize := 0 - for chunk in req.body do - totalSize := totalSize + chunk.size - if totalSize > 1000 then - return Response.new - |>.status .payloadTooLarge - |>.header! "Content-Type" "application/json" - |>.header! "Connection" "close" - |>.body "{\"error\": \"Request too large\", \"max_size\": 1000}" - - return Response.new - |>.status .ok - |>.body s!"Accepted request of size {totalSize}" - - let largeData := - Array.replicate 1500 97 - |> ByteArray.mk - |> String.fromUTF8! - - let data ← requestToByteArray ( - Request.new - |>.uri! "/upload" - |>.method .post - |>.header! "Host" "localhost" - |>.header! "Content-Type" "text/plain" - |>.header! "Content-Length" s!"{largeData.length}" - |>.header! "Connection" "close" - |>.body #[.mk largeData.toUTF8 #[]]) - - let response ← sendRawBytes pair data handler - - let responseData := String.fromUTF8! response - IO.println s!"{responseData.quote}" - -/-- -info: "HTTP/1.1 413 Payload Too Large\x0d\nContent-Length: 48\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\nContent-Type: application/json\x0d\n\x0d\n{\"error\": \"Request too large\", \"max_size\": 1000}" --/ -#guard_msgs in -#eval show IO _ from do testMaxRequestSize - -def testChunkedWithTrailer : IO Unit := do - let pair ← Mock.new - - let handler := fun (req : Request Body) => do - let mut body := "" - for chunk in req.body do - body := body ++ String.fromUTF8! chunk.data - - let checksum := req.head.headers.getLast? (.new "x-checksum") |>.map (·.value) |>.getD "" - - return Response.new - |>.status .ok - |>.header! "Content-Type" "text/plain" - |>.header! "Connection" "close" - |>.body s!"hello: {body.quote}." - - -- Construct raw ByteArray for chunked request with trailer - -- This cannot be done with Request builder since it doesn't support trailers - let rawRequest := "POST / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: TestClient/1.0\r\nTransfer-Encoding: chunked\r\nTrailer: X-Checksum\r\nAccept: */*\r\n\r\n5\r\nHello\r\n6\r\n World\r\n0\r\nX-Checksum: abcd\r\n\r\n" - let data : ByteArray := rawRequest.toUTF8 - - let response ← sendRawBytes pair data handler - - let responseData := String.fromUTF8! response - IO.println s!"{responseData.quote}" - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 21\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\nContent-Type: text/plain\x0d\n\x0d\nhello: \"Hello World\"." --/ -#guard_msgs in -#eval show IO _ from do testChunkedWithTrailer - -def testContentNegotiation : IO Unit := do - let pair ← Mock.new - - let handler := fun (req : Request Body) => do - if req.head.headers.hasEntry (.new "accept") (.new "application/json") then - return Response.new - |>.status .accepted - |>.header! "Content-Type" "application/json" - |>.body "{\"message\": \"JSON response\", \"status\": \"accepted\"}" - else if req.head.headers.hasEntry (.new "accept") (.new "text/xml") then - return Response.new - |>.status .ok - |>.header! "Content-Type" "application/xml" - |>.body "<?xml version=\"1.0\"?><response><message>XML response</message></response>" - else - return Response.new - |>.status .ok - |>.header! "Content-Type" "text/plain" - |>.body "Plain text response" - - let mut data := .empty - - data := data ++ (← requestToByteArray ( - Request.new - |>.uri! "/api/content" - |>.method .post - |>.header! "Host" "localhost" - |>.header! "Accept" "application/json" - |>.header! "Content-Type" "application/json" - |>.header! "Content-Length" "19" - |>.body #[.mk "{\"request\": \"data\"}".toUTF8 #[]])) - - data := data ++ (← requestToByteArray ( - Request.new - |>.uri! "/api/content" - |>.method .get - |>.header! "Host" "localhost" - |>.header! "Accept" "text/xml" - |>.header! "Content-Length" "1" - |>.body #[.mk "a".toUTF8 #[]])) - - let response ← sendRawBytes pair data handler - - let responseData := String.fromUTF8! response - IO.println s!"{responseData.quote}" - -def test20Requests : IO Unit := do - let pair ← Mock.new - - let handler := fun (_ : Request Body) => do - return Response.new - |>.status .ok - |>.header! "Content-Type" "text/plain" - |>.body "OK" - - -- Build 20 identical requests - let mut data := .empty - for _ in [0:20] do - data := data ++ (← requestToByteArray ( - Request.new - |>.uri! "/test" - |>.method .get - |>.header! "Host" "localhost" - |>.header! "User-Agent" "TestClient/1.0" - |>.header! "Content-Length" "0" - |>.body #[.mk "".toUTF8 #[]])) - - -- Add Connection: close to the last request - data := data ++ (← requestToByteArray ( - Request.new - |>.uri! "/test" - |>.method .get - |>.header! "Host" "localhost" - |>.header! "User-Agent" "TestClient/1.0" - |>.header! "Connection" "close" - |>.header! "Content-Length" "0" - |>.body #[.mk "".toUTF8 #[]])) - - let response ← sendRawBytes pair data handler { lingeringTimeout := 2000, maxRequests := 10 } - - let responseData := String.fromUTF8! response - let responseCount := (responseData.splitOn "HTTP/1.1 200 OK").length - 1 - IO.println s!"Sent 21 requests, received {responseCount} responses" - -/-- -info: Sent 21 requests, received 10 responses --/ -#guard_msgs in -#eval show IO _ from do test20Requests - -def testKeepAlive : IO Unit := do - let pair ← Mock.new - - let handler := fun (_ : Request Body) => do - return Response.new - |>.status .ok - |>.header! "Content-Type" "text/plain" - |>.body "OK" - - -- Build 20 identical requests - let mut data := .empty - for _ in [0:20] do - data := data ++ (← requestToByteArray ( - Request.new - |>.uri! "/test" - |>.method .get - |>.header! "Host" "localhost" - |>.header! "User-Agent" "TestClient/1.0" - |>.header! "Content-Length" "0" - |>.body #[.mk "".toUTF8 #[]])) - - -- Add Connection: close to the last request - data := data ++ (← requestToByteArray ( - Request.new - |>.uri! "/test" - |>.method .get - |>.header! "Host" "localhost" - |>.header! "User-Agent" "TestClient/1.0" - |>.header! "Connection" "close" - |>.header! "Content-Length" "0" - |>.body #[.mk "".toUTF8 #[]])) - - let response ← sendRawBytes pair data handler { lingeringTimeout := 2000, maxRequests := 10, enableKeepAlive := false } - - let responseData := String.fromUTF8! response - let responseCount := (responseData.splitOn "HTTP/1.1 200 OK").length - 1 - IO.println s!"Sent 21 requests, received {responseCount} responses" - -/-- -info: Sent 21 requests, received 1 responses --/ -#guard_msgs in -#eval show IO _ from do testKeepAlive
diff --git a/tests/lean/run/async_http_compliance.lean b/tests/lean/run/async_http_server.lean similarity index 68% rename from tests/lean/run/async_http_compliance.lean rename to tests/lean/run/async_http_server.lean index 1845dbc..771cd2d 100644 --- a/tests/lean/run/async_http_compliance.lean +++ b/tests/lean/run/async_http_server.lean
@@ -1,12 +1,24 @@ +/- +Copyright (c) 2025 Lean FRO, LLC. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Sofia Rodrigues +-/ import Std.Internal.Http import Std.Internal.Async open Std.Internal.IO.Async open Std Http -structure Result where - responseSent : Nat - data : ByteArray +/-! +# HTTP Server Tests + +Comprehensive tests for HTTP server compliance, security, and request handling. +Tests raw byte handling, request smuggling prevention, and protocol compliance. +-/ + +-- ============================================================================ +-- Helper Functions +-- ============================================================================ def requestToByteArray (req : Request (Array Chunk)) (chunked := false) : IO ByteArray := Async.block do let mut data := String.toUTF8 <| toString req.head @@ -15,41 +27,43 @@ if chunked then data := data ++ toByteArray (Chunk.mk .empty .empty) return data -/-- Send raw byte stream through a mock connection and return the response data. -/ def sendRawBytes (data : Array ByteArray) - (onRequest : Request Body → ContextAsync (Response Body)) (config : Config := { lingeringTimeout := 3000 }) : IO ByteArray := Async.block do + (onRequest : Request Body → ContextAsync (Response Body)) + (config : Config := { lingeringTimeout := 3000 }) : IO ByteArray := Async.block do let (client, server) ← Mock.new - - for data in data do - client.send data - + for d in data do + client.send d client.close - Std.Http.Server.serveConnection server onRequest (fun _ => pure ()) config |>.run - let res ← client.recv? pure <| res.getD .empty --- Handlers +def echoHandler (req: Request Body) : ContextAsync (Response Body) := do + let mut data := ByteArray.empty + for chunk in req.body do + data := data ++ chunk.data + return Response.new + |>.status .ok + |>.body data def maximumSizeHandlerEcho (maxSize : Nat) (req: Request Body) : ContextAsync (Response Body) := do - let mut size := 0 - let mut data := ByteArray.empty - - for i in req.body do - size := size + i.size - data := data ++ i.data - if size > maxSize then - return Response.new + let mut size := 0 + let mut data := ByteArray.empty + for i in req.body do + size := size + i.size + data := data ++ i.data + if size > maxSize then + return Response.new |>.status .payloadTooLarge |>.header! "Connection" "close" |>.body .empty + return Response.new + |>.status .ok + |>.body data - return Response.new - |>.status .ok - |>.body data - --- Tests +-- ============================================================================ +-- Fragmented Request Tests +-- ============================================================================ /-- info: "HTTP/1.1 200 OK\x0d\nContent-Length: 1\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\na" @@ -75,50 +89,6 @@ let response ← sendRawBytes #["POST /path/to/".toUTF8, "resource HTTP/1.1\r\nContent-Length: 4\r\nHost: test\r\n\r\ntest".toUTF8] (maximumSizeHandlerEcho 150) IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 3\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nabc" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.".toUTF8, "1\r\nContent-Length: 3\r\nHost: test\r\n\r\nabc".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 4\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\ndata" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nCon".toUTF8, "tent-Length: 4\r\nHost: test\r\n\r\ndata".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 6\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nfoobar" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 6\r\nX-Custom: val".toUTF8, "ue123\r\nHost: test\r\n\r\nfoobar".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 2\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nab" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 2\r".toUTF8, "\nHost: test\r\n\r\nab".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 7\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\ncontent" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 7\r\nHost: test\r\n\r".toUTF8, "\ncontent".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - /-- info: "HTTP/1.1 200 OK\x0d\nContent-Length: 10\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nhelloworld" -/ @@ -128,15 +98,6 @@ IO.println <| String.quote <| String.fromUTF8! response /-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 8\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nmultipar" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST /".toUTF8, "api HTTP/1.1\r\nContent-Length: 8\r\nHost: t".toUTF8, "est\r\n\r\nmultipar".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- info: "HTTP/1.1 200 OK\x0d\nContent-Length: 2\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nok" -/ #guard_msgs in @@ -144,12 +105,16 @@ let response ← sendRawBytes #["P".toUTF8, "O".toUTF8, "ST / HTTP/1.1\r\nContent-Length: 2\r\nHost: test\r\n\r\n".toUTF8, "o".toUTF8, "k".toUTF8] (maximumSizeHandlerEcho 150) IO.println <| String.quote <| String.fromUTF8! response +-- ============================================================================ +-- Basic HTTP Methods +-- ============================================================================ + /-- info: "HTTP/1.1 200 OK\x0d\nContent-Length: 0\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" -/ #guard_msgs in #eval show IO Unit from do - let response ← sendRawBytes #["GET / HTTP/1.1\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) + let response ← sendRawBytes #["GET / HTTP/1.1\r\nHost: test\r\n\r\n".toUTF8] echoHandler IO.println <| String.quote <| String.fromUTF8! response /-- @@ -160,89 +125,9 @@ let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 0\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) IO.println <| String.quote <| String.fromUTF8! response -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["INVALID / HTTP/1.1\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["GET /\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["GET / HTTP/2.0\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["GET / HTTP/1.1\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 4\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\ndata" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 4\r\nX-Custom: value1\r\nX-Custom: value2\r\nHost: test\r\n\r\ndata".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5 \r\nHost: test\r\n\r\nhello".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nX-Custom: line1\r\n continuation\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nInvalid Header: value\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\n: value\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 2\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nok" --/ -#guard_msgs in -#eval show IO Unit from do - let longValue := String.join (List.replicate 1000 "x") - let response ← sendRawBytes #[s!"POST / HTTP/1.1\r\nX-Long: {longValue}\r\nContent-Length: 2\r\nHost: test\r\n\r\nok".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response +-- ============================================================================ +-- Chunked Encoding Tests +-- ============================================================================ /-- info: "HTTP/1.1 200 OK\x0d\nContent-Length: 11\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nhello world" @@ -277,72 +162,332 @@ IO.println <| String.quote <| String.fromUTF8! response /-- +info: "HTTP/1.1 200 OK\x0d\nContent-Length: 5\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nhello" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: gzip, chunked\r\nHost: test\r\n\r\n5\r\nhello\r\n0\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- CL.CL Attack Prevention (Duplicate Content-Length) +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nContent-Length: 5\r\nHost: test\r\n\r\nhello".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nContent-Length: 10\r\nHost: test\r\n\r\nhello".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5, 10\r\nHost: test\r\n\r\nhello".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- CL.TE Attack Prevention (Content-Length + Transfer-Encoding) +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 100\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\n5\r\nhello\r\n0\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nContent-Length: 100\r\nHost: test\r\n\r\n5\r\nhello\r\n0\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Transfer-Encoding Validation +-- ============================================================================ + +/-- +info: "HTTP/1.1 200 OK\x0d\nContent-Length: 5\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nhello" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\n5\r\nhello\r\n0\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Invalid Methods +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["INVALID / HTTP/1.1\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["get / HTTP/1.1\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Invalid HTTP Versions +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["GET / HTTP/1.0\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["GET / HTTP/2.0\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Missing Host Header +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["GET / HTTP/1.1\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Malformed Request Line +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["GET /\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Header Injection Prevention +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nX-Custom".toUTF8, ByteArray.mk #[0], ": value\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nX-Custom\t: value\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nX-Custom : value\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\n: value\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nInvalid Header: value\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Invalid Chunked Encoding +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\nZZZ\r\ndata\r\n0\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\n5hello\r\n0\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\n5\r\nhello\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\nA\r\nhello\r\n0\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Content-Length Validation +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: -5\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: abc\r\nHost: test\r\n\r\n".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 100\r\nHost: test\r\n\r\nshort".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 200 OK\x0d\nContent-Length: 5\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nhello" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nHost: test\r\n\r\nhello".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- info: "HTTP/1.1 200 OK\x0d\nContent-Length: 5\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nexact" -/ #guard_msgs in #eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nHost: test\r\n\r\nexact".toUTF8] (maximumSizeHandlerEcho 150) + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nHost: test\r\n\r\nexact".toUTF8] echoHandler IO.println <| String.quote <| String.fromUTF8! response -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 100\r\nHost: test\r\n\r\nshort".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 200 OK\x0d\nContent-Length: 5\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nhelloHTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nHost: test\r\n\r\nhello extra data here".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: -5\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: abc\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - +-- ============================================================================ +-- Keep-Alive / Pipelining Tests +-- ============================================================================ /-- info: "HTTP/1.1 200 OK\x0d\nContent-Length: 5\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nfirstHTTP/1.1 200 OK\x0d\nContent-Length: 6\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nsecond" -/ #guard_msgs in #eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nHost: test\r\n\r\nfirstPOST / HTTP/1.1\r\nContent-Length: 6\r\nHost: test\r\n\r\nsecond".toUTF8] (maximumSizeHandlerEcho 150) + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nHost: test\r\n\r\nfirstPOST / HTTP/1.1\r\nContent-Length: 6\r\nHost: test\r\n\r\nsecond".toUTF8] echoHandler IO.println <| String.quote <| String.fromUTF8! response - /-- info: "HTTP/1.1 200 OK\x0d\nContent-Length: 4\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\ndata" -/ #guard_msgs in #eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 4\r\nConnection: close\r\nHost: test\r\n\r\ndata".toUTF8] (maximumSizeHandlerEcho 150) + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 4\r\nConnection: close\r\nHost: test\r\n\r\ndata".toUTF8] echoHandler IO.println <| String.quote <| String.fromUTF8! response - /-- info: "HTTP/1.1 200 OK\x0d\nContent-Length: 9\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nkeepalive" -/ #guard_msgs in #eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 9\r\nConnection: keep-alive\r\nHost: test\r\n\r\nkeepalive".toUTF8] (maximumSizeHandlerEcho 150) + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 9\r\nConnection: keep-alive\r\nHost: test\r\n\r\nkeepalive".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Multiple Headers +-- ============================================================================ + +/-- +info: "HTTP/1.1 200 OK\x0d\nContent-Length: 4\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\ndata" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 4\r\nX-Custom: value1\r\nX-Custom: value2\r\nHost: test\r\n\r\ndata".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +/-- +info: "HTTP/1.1 200 OK\x0d\nContent-Length: 2\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nok" +-/ +#guard_msgs in +#eval show IO Unit from do + let longValue := String.join (List.replicate 1000 "x") + let response ← sendRawBytes #[s!"POST / HTTP/1.1\r\nX-Long: {longValue}\r\nContent-Length: 2\r\nHost: test\r\n\r\nok".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Extra Data After Body +-- ============================================================================ + +/-- +info: "HTTP/1.1 200 OK\x0d\nContent-Length: 5\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\nhelloHTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nHost: test\r\n\r\nhello extra data here".toUTF8] echoHandler + IO.println <| String.quote <| String.fromUTF8! response + +-- ============================================================================ +-- Header Folding (Obsolete, should be rejected) +-- ============================================================================ + +/-- +info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" +-/ +#guard_msgs in +#eval show IO Unit from do + let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5 \r\nHost: test\r\n\r\nhello".toUTF8] echoHandler IO.println <| String.quote <| String.fromUTF8! response /-- @@ -350,118 +495,17 @@ -/ #guard_msgs in #eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 100\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\n5\r\nhello\r\n0\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) + let response ← sendRawBytes #["POST / HTTP/1.1\r\nX-Custom: line1\r\n continuation\r\nHost: test\r\n\r\n".toUTF8] echoHandler IO.println <| String.quote <| String.fromUTF8! response +-- ============================================================================ +-- Control Characters in Path +-- ============================================================================ + /-- info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" -/ #guard_msgs in #eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nContent-Length: 100\r\nHost: test\r\n\r\n5\r\nhello\r\n0\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nContent-Length: 5\r\nHost: test\r\n\r\nhello".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5\r\nContent-Length: 5\r\nHost: test\r\n\r\nhello".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nContent-Length: 5, 10\r\nHost: test\r\n\r\nhello".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\nZZZ\r\ndata\r\n0\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\n5hello\r\n0\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\n5\r\nhello\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nHost: test\r\n\r\nA\r\nhello\r\n0\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nX-Custom".toUTF8, ByteArray.mk #[0], ": value\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nX-Ignore: \r\nGET /evil HTTP/1.1\r\nHost: evil\r\n\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nX-Custom\t: value\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST / HTTP/1.1\r\nX-Custom : value\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) - IO.println <| String.quote <| String.fromUTF8! response - - -/-- -info: "HTTP/1.1 400 Bad Request\x0d\nContent-Length: 0\x0d\nConnection: close\x0d\nServer: LeanHTTP/1.1\x0d\n\x0d\n" --/ -#guard_msgs in -#eval show IO Unit from do - let response ← sendRawBytes #["POST /path\x0Btest HTTP/1.1\r\nHost: test\r\n\r\n".toUTF8] (maximumSizeHandlerEcho 150) + let response ← sendRawBytes #["POST /path\x0Btest HTTP/1.1\r\nHost: test\r\n\r\n".toUTF8] echoHandler IO.println <| String.quote <| String.fromUTF8! response
diff --git a/tests/lean/run/async_http_uri.lean b/tests/lean/run/async_http_uri.lean new file mode 100644 index 0000000..039db53 --- /dev/null +++ b/tests/lean/run/async_http_uri.lean
@@ -0,0 +1,826 @@ +/- +Copyright (c) 2025 Lean FRO, LLC. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Sofia Rodrigues +-/ +import Std.Internal.Http.Data.URI +import Std.Internal.Http.Data.URI.Encoding +import Std.Internal.Http.Protocol.H1.Parser + +open Std.Http +open Std.Http.URI +open Std.Http.URI.Parser + +/-! +# URI Tests + +Comprehensive tests for URI parsing, encoding, normalization, and manipulation. +This file consolidates tests from multiple URI-related test files. +-/ + +-- ============================================================================ +-- Helper Functions +-- ============================================================================ + +def runParser (parser : Std.Internal.Parsec.ByteArray.Parser α) (s : String) : IO α := + IO.ofExcept ((parser <* Std.Internal.Parsec.eof).run s.toUTF8) + +def parseCheck (s : String) (exact : String := s) : IO Unit := do + let result ← runParser parseRequestTarget s + if toString result = exact then + pure () + else + throw (.userError s!"expect {exact.quote} but got {(toString result).quote}") + +def parseCheckFail (s : String) : IO Unit := do + match (parseRequestTarget <* Std.Internal.Parsec.eof).run s.toUTF8 with + | .ok r => + throw <| .userError + s!"expected parse failure, but succeeded with {(repr r)}" + | .error _ => + pure () + +-- ============================================================================ +-- Percent Encoding Tests (EncodedString) +-- ============================================================================ + +-- Valid percent encoding validation +/-- +info: some "abc" +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "abc".toUTF8)) + +/-- +info: some "%20" +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "%20".toUTF8)) + +/-- +info: some "hello%20world" +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "hello%20world".toUTF8)) + +/-- +info: some "%FF" +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "%FF".toUTF8)) + +/-- +info: some "%00" +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "%00".toUTF8)) + +-- Invalid percent encoding: incomplete +/-- +info: none +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "%".toUTF8)) + +/-- +info: none +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "hello%".toUTF8)) + +/-- +info: none +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "%2".toUTF8)) + +/-- +info: none +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "%A".toUTF8)) + +-- Invalid percent encoding: non-hex characters +/-- +info: none +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "%GG".toUTF8)) + +/-- +info: none +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "%2G".toUTF8)) + +/-- +info: none +-/ +#guard_msgs in +#eval IO.println (repr (EncodedString.ofByteArray? "%G2".toUTF8)) + +-- ============================================================================ +-- Percent Encoding Decode Tests +-- ============================================================================ + +/-- +info: some "abc" +-/ +#guard_msgs in +#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "abc".toUTF8)) + +/-- +info: some " " +-/ +#guard_msgs in +#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%20".toUTF8)) + +/-- +info: some "hello world" +-/ +#guard_msgs in +#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "hello%20world".toUTF8)) + +/-- +info: some " !" +-/ +#guard_msgs in +#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%20%21".toUTF8)) + +/-- +info: none +-/ +#guard_msgs in +#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%FF".toUTF8)) + +/-- +info: some "\x00" +-/ +#guard_msgs in +#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%00".toUTF8)) + +-- ============================================================================ +-- Query String Encoding Tests +-- ============================================================================ + +/-- +info: some "hello+world" +-/ +#guard_msgs in +#eval IO.println (repr (EncodedQueryString.ofByteArray? "hello+world".toUTF8)) + +/-- +info: none +-/ +#guard_msgs in +#eval IO.println (repr (EncodedQueryString.ofByteArray? "%".toUTF8)) + +/-- +info: some "hello world" +-/ +#guard_msgs in +#eval IO.println (repr <| EncodedQueryString.decode =<< (EncodedQueryString.ofByteArray? "hello+world".toUTF8)) + +/-- +info: some " " +-/ +#guard_msgs in +#eval IO.println (repr <| EncodedQueryString.decode =<< (EncodedQueryString.ofByteArray? "%20".toUTF8)) + +-- ============================================================================ +-- Request Target Parsing - Basic Tests +-- ============================================================================ + +#eval parseCheck "/path/with/encoded%20space" +#eval parseCheck "/path/with/encoded%20space/" +#eval parseCheck "*" +#eval parseCheck "https://ata/b?ata=be#lol%F0%9F%94%A5" +#eval parseCheck "/api/search?q=hello%20world&category=tech%2Bgames" +#eval parseCheck "/" +#eval parseCheck "/api/v1/users/123/posts/456/comments/789" +#eval parseCheck "/files/../etc/passwd" +#eval parseCheck "example.com:8080" +#eval parseCheck "https://example.com:8080/ata" +#eval parseCheck "192.168.1.1:3000" +#eval parseCheck "[::1]:8080" +#eval parseCheck "http://example.com/path/to/resource?query=value" +#eval parseCheck "https://api.example.com:443/v1/users?limit=10" +#eval parseCheck "http://[2001:db8::1]:8080/path" +#eval parseCheck "https://example.com/page#section1" +#eval parseCheck "https://xn--nxasmq6b.xn--o3cw4h/path" +#eval parseCheck "localhost:65535" +#eval parseCheck "https://user:pass@secure.example.com/private" +#eval parseCheck "/double//slash//path" + +-- Parse failure tests +#eval parseCheckFail "/path with space" +#eval parseCheckFail "/path/%" +#eval parseCheckFail "/path/%2" +#eval parseCheckFail "/path/%ZZ" +#eval parseCheckFail "" +#eval parseCheckFail "[::1" +#eval parseCheckFail "[:::1]:80" +#eval parseCheckFail "#frag" +#eval parseCheckFail "/path/\n" +#eval parseCheckFail "/path/\u0000" + +-- ============================================================================ +-- Request Target Parsing - Detailed Output Tests +-- ============================================================================ + +/-- +info: Std.Http.RequestTarget.originForm { segments := #["path", "with", "encoded%20space"], absolute := true } none none +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/path/with/encoded%20space" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.asteriskForm +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "*" + IO.println (repr result) + +/-- +info: some "lol🔥" +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "https://ata/b?ata=be#lol%F0%9F%94%A5" + IO.println (repr (result.fragment?)) + +/-- +info: #[("q", some "hello%20world"), ("category", some "tech%2Bgames")] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/api/search?q=hello%20world&category=tech%2Bgames" + IO.println (repr result.query) + +/-- +info: Std.Http.RequestTarget.originForm { segments := #[], absolute := true } none none +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.authorityForm + { userInfo := none, host := Std.Http.URI.Host.name "example.com", port := some 8080 } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "example.com:8080" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.authorityForm { userInfo := none, host := Std.Http.URI.Host.ipv4 192.168.1.1, port := some 3000 } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "192.168.1.1:3000" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.authorityForm { userInfo := none, host := Std.Http.URI.Host.ipv6 ::1, port := some 8080 } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "[::1]:8080" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.absoluteForm + { scheme := "https", + authority := some { userInfo := none, host := Std.Http.URI.Host.name "example.com", port := some 8080 }, + path := { segments := #["ata"], absolute := true }, + query := #[], + fragment := none } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "https://example.com:8080/ata" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.absoluteForm + { scheme := "http", + authority := some { userInfo := none, host := Std.Http.URI.Host.ipv6 2001:db8::1, port := some 8080 }, + path := { segments := #["path"], absolute := true }, + query := #[], + fragment := none } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "http://[2001:db8::1]:8080/path" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.absoluteForm + { scheme := "https", + authority := some { userInfo := some { username := "user b", password := some "pass" }, + host := Std.Http.URI.Host.name "secure.example.com", + port := none }, + path := { segments := #["private"], absolute := true }, + query := #[], + fragment := none } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "https://user%20b:pass@secure.example.com/private" + IO.println (repr result) + +-- ============================================================================ +-- IPv6 Host Tests +-- ============================================================================ + +/-- +info: Std.Http.URI.Host.ipv6 ::1 +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "[::1]:8080" + match result.authority? with + | some auth => IO.println (repr auth.host) + | none => IO.println "no authority" + +/-- +info: Std.Http.URI.Host.ipv6 2001:db8::8a2e:370:7334 +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "http://[2001:db8::8a2e:370:7334]:8080/api" + match result.authority? with + | some auth => IO.println (repr auth.host) + | none => IO.println "no authority" + +/-- +info: Std.Http.URI.Host.ipv6 :: +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "http://[::]/path" + match result.authority? with + | some auth => IO.println (repr auth.host) + | none => IO.println "no authority" + +-- ============================================================================ +-- UserInfo Tests +-- ============================================================================ + +/-- +info: some { username := "user", password := some "pass" } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "https://user:pass@example.com/private" + match result.authority? with + | some auth => IO.println (repr auth.userInfo) + | none => IO.println "no authority" + +/-- +info: some { username := "user only", password := none } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "https://user%20only@example.com/path" + match result.authority? with + | some auth => IO.println (repr auth.userInfo) + | none => IO.println "no authority" + +/-- +info: some { username := "", password := some "pass" } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "https://:pass@example.com/path" + match result.authority? with + | some auth => IO.println (repr auth.userInfo) + | none => IO.println "no authority" + +/-- +info: some { username := "user", password := some "p@ss:w0rd" } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "https://user:p%40ss%3Aw0rd@example.com/" + match result.authority? with + | some auth => IO.println (repr auth.userInfo) + | none => IO.println "no authority" + +-- ============================================================================ +-- Path.normalize Tests (RFC 3986 Section 5.2.4) +-- ============================================================================ + +/-- +info: /a/b +-/ +#guard_msgs in +#eval IO.println <| toString (URI.parse! "http://example.com/a/./b").path.normalize + +/-- +info: /a +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a/b/..").path.normalize + +/-- +info: /a/g +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a/b/c/./../../g").path.normalize + +/-- +info: /g +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/../../../g").path.normalize + +/-- +info: /a/c +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a/b/../c").path.normalize + +/-- +info: /a/ +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a/b/c/../.././").path.normalize + +/-- +info: / +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a/b/../../..").path.normalize + +/-- +info: / +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/../../../").path.normalize + +/-- +info: /a/b/c +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/./a/./b/./c/.").path.normalize + +/-- +info: /c +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a/../b/../c").path.normalize + +-- ============================================================================ +-- Path.parent Tests +-- ============================================================================ + +/-- +info: /a/b +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a/b/c").path.parent + +/-- +info: /a +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a/b").path.parent + +/-- +info: / +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a").path.parent + +/-- +info: / +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/").path.parent + +-- ============================================================================ +-- Path.join Tests +-- ============================================================================ + +/-- +info: /a/b/c/d +-/ +#guard_msgs in +#eval do + let p1 := (URI.parse! "http://example.com/a/b").path + let p2 : URI.Path := { segments := #[URI.EncodedString.encode "c", URI.EncodedString.encode "d"], absolute := false } + IO.println (p1.join p2) + +/-- +info: /x/y +-/ +#guard_msgs in +#eval do + let p1 := (URI.parse! "http://example.com/a/b").path + let p2 : URI.Path := { segments := #[URI.EncodedString.encode "x", URI.EncodedString.encode "y"], absolute := true } + IO.println (p1.join p2) + +-- ============================================================================ +-- Path.isEmpty Tests +-- ============================================================================ + +#guard (URI.parse! "http://example.com").path.isEmpty = true +#guard (URI.parse! "http://example.com/").path.absolute = true +#guard (URI.parse! "http://example.com/a").path.isEmpty = false +#guard (URI.parse! "http://example.com/a").path.absolute = true + +-- ============================================================================ +-- URI Modification Helpers +-- ============================================================================ + +#guard ((URI.parse! "http://example.com").withScheme "htTps" |>.scheme) == "https" +#guard ((URI.parse! "http://example.com").withScheme "ftP" |>.scheme) == "ftp" + +/-- +info: http://example.com/#section1 +-/ +#guard_msgs in +#eval IO.println ((URI.parse! "http://example.com/").withFragment (some (toString <| URI.EncodedString.encode "section1"))) + +/-- +info: http://example.com/?key=value +-/ +#guard_msgs in +#eval do + let uri := URI.parse! "http://example.com/" + let query := URI.Query.empty.insert "key" "value" + IO.println (uri.withQuery query) + +/-- +info: http://example.com/new/path +-/ +#guard_msgs in +#eval do + let uri := URI.parse! "http://example.com/old/path" + let newPath : URI.Path := { segments := #[URI.EncodedString.encode "new", URI.EncodedString.encode "path"], absolute := true } + IO.println (uri.withPath newPath) + +-- ============================================================================ +-- URI.normalize Tests (RFC 3986 Section 6) +-- ============================================================================ + +#guard (URI.parse! "HTTP://example.com").normalize.scheme == "http" +#guard (URI.parse! "HtTpS://example.com").normalize.scheme == "https" + +/-- +info: http://example.com/ +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://EXAMPLE.COM/").normalize + +/-- +info: http://example.com/ +-/ +#guard_msgs in +#eval IO.println (URI.parse! "HTTP://Example.COM/").normalize + +/-- +info: http://example.com/a/c +-/ +#guard_msgs in +#eval IO.println (URI.parse! "http://example.com/a/b/../c").normalize + +/-- +info: http://example.com/a/g +-/ +#guard_msgs in +#eval IO.println (URI.parse! "HTTP://EXAMPLE.COM/a/b/c/./../../g").normalize + +/-- +info: https://www.example.com/PATH +-/ +#guard_msgs in +#eval IO.println (URI.parse! "HTTPS://WWW.EXAMPLE.COM/PATH").normalize + +-- ============================================================================ +-- Query Parameter Tests +-- ============================================================================ + +-- Query with duplicate keys +/-- +info: 3 +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/search?tag=a&tag=b&tag=c" + let all := result.query.findAll "tag" + IO.println all.size + +/-- +info: #[some "a", some "b", some "c"] +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/search?tag=a&tag=b&tag=c" + let all := result.query.findAll "tag" + IO.println (repr all) + +/-- +info: some (some "a") +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/search?key=a&key=b&key=c" + IO.println (repr (result.query.find? "key")) + +-- Empty value vs no value +/-- +info: some (some "") +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/api?key=" + IO.println (repr (result.query.find? "key")) + +/-- +info: some none +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/api?key" + IO.println (repr (result.query.find? "key")) + +/-- +info: some (some "value") +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/api?key=value" + IO.println (repr (result.query.find? "key")) + +-- ============================================================================ +-- Query Operations +-- ============================================================================ + +#guard (URI.Query.empty.insert "a" "1" |>.contains "a") = true +#guard (URI.Query.empty.contains "nonexistent") = false + +/-- +info: a=1&b=2 +-/ +#guard_msgs in +#eval do + let query := URI.Query.empty + |>.insert "a" "1" + |>.insert "b" "2" + IO.println query.toRawString + +/-- +info: b=2 +-/ +#guard_msgs in +#eval do + let query := URI.Query.empty + |>.insert "a" "1" + |>.insert "b" "2" + |>.erase "a" + IO.println query.toRawString + +/-- +info: key=new +-/ +#guard_msgs in +#eval do + let query := URI.Query.empty + |>.insert "key" "old" + |>.set "key" "new" + IO.println query.toRawString + +-- ============================================================================ +-- Fragment Tests +-- ============================================================================ + +/-- +info: some "section/subsection" +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "https://example.com/page#section%2Fsubsection" + IO.println (repr result.fragment?) + +/-- +info: some "heading with spaces" +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "https://example.com/page#heading%20with%20spaces" + IO.println (repr result.fragment?) + +/-- +info: none +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/path" + IO.println (repr result.fragment?) + +/-- +info: some "" +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/path#" + IO.println (repr result.fragment?) + +-- ============================================================================ +-- URI Builder Tests +-- ============================================================================ + +/-- +info: https://example.com/api/users?page=1 +-/ +#guard_msgs in +#eval do + let uri := URI.Builder.empty + |>.setScheme "https" + |>.setHost "example.com" + |>.appendPathSegment "api" + |>.appendPathSegment "users" + |>.addQueryParam "page" "1" + |>.build + IO.println uri + +/-- +info: http://localhost:8080/ +-/ +#guard_msgs in +#eval do + let uri := URI.Builder.empty + |>.setScheme "http" + |>.setHost "localhost" + |>.setPort 8080 + |>.build + IO.println uri + +/-- +info: https://user:pass@secure.example.com/private +-/ +#guard_msgs in +#eval do + let uri := URI.Builder.empty + |>.setScheme "https" + |>.setUserInfo "user" (some "pass") + |>.setHost "secure.example.com" + |>.appendPathSegment "private" + |>.build + IO.println uri + +-- ============================================================================ +-- Encoded Path Segment Tests +-- ============================================================================ + +/-- +info: Std.Http.RequestTarget.originForm { segments := #["path%2Fwith%2Fslashes"], absolute := true } none none +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/path%2Fwith%2Fslashes" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.originForm { segments := #["file%20name.txt"], absolute := true } none none +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/file%20name.txt" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.originForm { segments := #["caf%C3%A9"], absolute := true } none none +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "/caf%C3%A9" + IO.println (repr result) + +-- ============================================================================ +-- Authority Form Tests +-- ============================================================================ + +/-- +info: Std.Http.RequestTarget.authorityForm + { userInfo := none, host := Std.Http.URI.Host.name "proxy.example.com", port := some 3128 } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "proxy.example.com:3128" + IO.println (repr result) + +/-- +info: Std.Http.RequestTarget.authorityForm { userInfo := none, host := Std.Http.URI.Host.ipv4 127.0.0.1, port := some 8080 } +-/ +#guard_msgs in +#eval show IO _ from do + let result ← runParser parseRequestTarget "127.0.0.1:8080" + IO.println (repr result)
diff --git a/tests/lean/run/async_http_uri_api.lean b/tests/lean/run/async_http_uri_api.lean deleted file mode 100644 index da963ed..0000000 --- a/tests/lean/run/async_http_uri_api.lean +++ /dev/null
@@ -1,191 +0,0 @@ -/- -Copyright (c) 2025 Lean FRO, LLC. All rights reserved. -Released under Apache 2.0 license as described in the file LICENSE. -Authors: Sofia Rodrigues --/ -import Std.Internal.Http.Data.URI - -open Std.Http - -/-! -# URI API Tests - -Tests for Path utilities, URI modification helpers, normalization, and resolution. --/ - --- ============================================================================ --- Path.normalize tests (RFC 3986 Section 5.2.4) --- ============================================================================ - --- Single dot removal -/-- -info: /a/b --/ -#guard_msgs in -#eval IO.println <| toString (URI.parse! "http://example.com/a/./b").path.normalize - --- Double dot removal -/-- -info: /a --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/a/b/..").path.normalize - --- Complex path normalization -/-- -info: /a/g --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/a/b/c/./../../g").path.normalize - --- Double dot at start (should not go above root) -/-- -info: /g --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/../../../g").path.normalize - --- Mixed dots -/-- -info: /a/c --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/a/b/../c").path.normalize - --- Multiple consecutive dots and double-dots -/-- -info: /a/ --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/a/b/c/../.././").path.normalize - --- ============================================================================ --- Path.parent tests --- ============================================================================ - -/-- -info: /a/b --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/a/b/c").path.parent - -/-- -info: /a --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/a/b").path.parent - -/-- -info: / --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/a").path.parent - --- Parent of root path -/-- -info: / --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/").path.parent - --- ============================================================================ --- Path.join tests --- ============================================================================ - -/-- -info: /a/b/c/d --/ -#guard_msgs in -#eval do - let p1 := (URI.parse! "http://example.com/a/b").path - let p2 : URI.Path := { segments := #[URI.EncodedString.encode "c", URI.EncodedString.encode "d"], absolute := false } - IO.println (p1.join p2) - --- Join with absolute path (absolute path wins) -/-- -info: /x/y --/ -#guard_msgs in -#eval do - let p1 := (URI.parse! "http://example.com/a/b").path - let p2 : URI.Path := { segments := #[URI.EncodedString.encode "x", URI.EncodedString.encode "y"], absolute := true } - IO.println (p1.join p2) - --- ============================================================================ --- Path.isEmpty tests --- ============================================================================ - -#guard (URI.parse! "http://example.com").path.isEmpty = true -#guard (URI.parse! "http://example.com/").path.absolute = true -#guard (URI.parse! "http://example.com/a").path.isEmpty = false -#guard (URI.parse! "http://example.com/a").path.absolute = true - --- ============================================================================ --- URI modification helpers tests --- ============================================================================ - --- withScheme -#guard ((URI.parse! "http://example.com").withScheme "htTps" |>.scheme) == "https" -#guard ((URI.parse! "http://example.com").withScheme "ftP" |>.scheme) == "ftp" - --- withFragment -/-- -info: http://example.com/#section1 --/ -#guard_msgs in -#eval IO.println ((URI.parse! "http://example.com/").withFragment (some (toString <| URI.EncodedString.encode "section1"))) - --- withQuery -/-- -info: http://example.com/?key=value --/ -#guard_msgs in -#eval do - let uri := URI.parse! "http://example.com/" - let query := URI.Query.empty.insert "key" "value" - IO.println (uri.withQuery query) - --- withPath -/-- -info: http://example.com/new/path --/ -#guard_msgs in -#eval do - let uri := URI.parse! "http://example.com/old/path" - let newPath : URI.Path := { segments := #[URI.EncodedString.encode "new", URI.EncodedString.encode "path"], absolute := true } - IO.println (uri.withPath newPath) - --- ============================================================================ --- URI.normalize tests (RFC 3986 Section 6) --- ============================================================================ - --- Scheme normalization (lowercase) -#guard (URI.parse! "HTTP://example.com").normalize.scheme == "http" -#guard (URI.parse! "HtTpS://example.com").normalize.scheme == "https" - --- Host normalization (lowercase) -/-- -info: http://example.com/ --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://EXAMPLE.COM/").normalize - -/-- -info: http://example.com/ --/ -#guard_msgs in -#eval IO.println (URI.parse! "HTTP://Example.COM/").normalize - --- Path normalization (dot removal) -/-- -info: http://example.com/a/c --/ -#guard_msgs in -#eval IO.println (URI.parse! "http://example.com/a/b/../c").normalize - --- Combined normalization -/-- -info: http://example.com/a/g --/ -#guard_msgs in -#eval IO.println (URI.parse! "HTTP://EXAMPLE.COM/a/b/c/./../../g").normalize
diff --git a/tests/lean/run/async_http_uri_encoding.lean b/tests/lean/run/async_http_uri_encoding.lean deleted file mode 100644 index 237677e..0000000 --- a/tests/lean/run/async_http_uri_encoding.lean +++ /dev/null
@@ -1,230 +0,0 @@ -import Std.Internal.Http.Data.URI.Encoding - -open Std.Http.URI - --- Valid percent encoding tests - -/-- -info: some "abc" --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "abc".toUTF8)) - -/-- -info: some "%20" --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%20".toUTF8)) - -/-- -info: some "hello%20world" --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "hello%20world".toUTF8)) - -/-- -info: some "%20%21" --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%20%21".toUTF8)) - -/-- -info: some "%FF" --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%FF".toUTF8)) - -/-- -info: some "%00" --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%00".toUTF8)) - --- Invalid percent encoding tests: % alone -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "hello%".toUTF8)) - --- Invalid percent encoding tests: % followed by only one hex digit -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%2".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "hello%2".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%A".toUTF8)) - --- Invalid percent encoding tests: % followed by non-hex characters -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%GG".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%2G".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedString.ofByteArray? "%G2".toUTF8)) - -/-- -info: some "hello+world" --/ -#guard_msgs in -#eval IO.println (repr (EncodedQueryString.ofByteArray? "hello+world".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedQueryString.ofByteArray? "%".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr (EncodedQueryString.ofByteArray? "%2".toUTF8)) - -/-- -info: some "%20" --/ -#guard_msgs in -#eval IO.println (repr (EncodedQueryString.ofByteArray? "%20".toUTF8)) - - --- Valid percent encoding tests - -/-- -info: some "abc" --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "abc".toUTF8)) - -/-- -info: some " " --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%20".toUTF8)) - -/-- -info: some "hello world" --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "hello%20world".toUTF8)) - -/-- -info: some " !" --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%20%21".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%FF".toUTF8)) - -/-- -info: some "\x00" --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%00".toUTF8)) - --- Invalid percent encoding tests: % alone -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "hello%".toUTF8)) - --- Invalid percent encoding tests: % followed by only one hex digit -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%2".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "hello%2".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%A".toUTF8)) - --- Invalid percent encoding tests: % followed by non-hex characters -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%GG".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%2G".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedString.decode =<< (EncodedString.ofByteArray? "%G2".toUTF8)) - -/-- -info: some "hello world" --/ -#guard_msgs in -#eval IO.println (repr <| EncodedQueryString.decode =<< (EncodedQueryString.ofByteArray? "hello+world".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedQueryString.decode =<< (EncodedQueryString.ofByteArray? "%".toUTF8)) - -/-- -info: none --/ -#guard_msgs in -#eval IO.println (repr <| EncodedQueryString.decode =<< (EncodedQueryString.ofByteArray? "%2".toUTF8)) - -/-- -info: some " " --/ -#guard_msgs in -#eval IO.println (repr <| EncodedQueryString.decode =<< (EncodedQueryString.ofByteArray? "%20".toUTF8))
diff --git a/tests/lean/run/async_http_uri_parser.lean b/tests/lean/run/async_http_uri_parser.lean deleted file mode 100644 index 397269b..0000000 --- a/tests/lean/run/async_http_uri_parser.lean +++ /dev/null
@@ -1,325 +0,0 @@ -import Std.Internal.Http.Protocol.H1.Parser - -open Std.Http.Protocol -open Std.Http.URI.Parser - -def runParser (parser : Std.Internal.Parsec.ByteArray.Parser α) (s : String) : IO α := - IO.ofExcept (parser.run s.toUTF8) - -/-- -info: Std.Http.RequestTarget.originForm { segments := #["path", "with", "encoded%20space"], absolute := true } none none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/path/with/encoded%20space" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm { segments := #["path", "with", "encoded%20space", ""], absolute := true } none none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/path/with/encoded%20space/" - IO.println (repr result) - -/-- -error: offset 0: expected: '58' --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "path/with/encoded%20space" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.asteriskForm --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "*" - IO.println (repr result) - -/-- -info: some "lol🔥" --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "https://ata/b?ata=be#lol%F0%9F%94%A5" - IO.println (repr (result.fragment?)) -/-- -info: #[("q", some "hello%20world"), ("category", some "tech%2Bgames")] --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/api/search?q=hello%20world&category=tech%2Bgames" - IO.println (repr result.query) - -/-- -info: Std.Http.RequestTarget.originForm { segments := #["files", "my%20document%2Epdf"], absolute := true } none none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/files/my%20document%2Epdf" - IO.println (repr result) - -/-- -info: some (some "%F0%9F%98%80") --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/search?name=%E2%9C%93%20checked&emoji=%F0%9F%98%80" - IO.println (repr <| result.query.find? "emoji") - -/-- -info: Std.Http.RequestTarget.originForm - { segments := #["api"], absolute := true } - (some #[("param1", some "value1"), ("param2", some "value2"), ("param3", some "value3")]) - none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/api?param1=value1¶m2=value2¶m3=value3" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm - { segments := #["search"], absolute := true } - (some #[("debug", none), ("verbose", none), ("q", some "test")]) - none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/search?debug&verbose&q=test" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm - { segments := #["api"], absolute := true } - (some #[("empty", some ""), ("also_empty", some ""), ("has_value", some "something")]) - none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/api?empty=&also_empty=&has_value=something" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm - { segments := #["search"], absolute := true } - (some #[("q", some "cats%26dogs"), ("filter", some "name%3Dmax")]) - none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/search?q=cats%26dogs&filter=name%3Dmax" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm { segments := #[], absolute := true } none none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm - { segments := #["api", "v1", "users", "123", "posts", "456", "comments", "789"], absolute := true } - none - none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/api/v1/users/123/posts/456/comments/789" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm { segments := #["files", "..", "etc", "passwd"], absolute := true } none none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/files/../etc/passwd" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm { segments := #["path%2Fwith%2Fencoded%2Fslashes"], absolute := true } none none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/path%2Fwith%2Fencoded%2Fslashes" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.authorityForm - { userInfo := none, host := Std.Http.URI.Host.name "example.com", port := some 8080 } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "example.com:8080" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.absoluteForm - { scheme := "https", - authority := some { userInfo := none, host := Std.Http.URI.Host.name "example.com", port := some 8080 }, - path := { segments := #["ata"], absolute := true }, - query := #[], - fragment := none } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "https://example.com:8080/ata" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.authorityForm { userInfo := none, host := Std.Http.URI.Host.ipv4 192.168.1.1, port := some 3000 } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "192.168.1.1:3000" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.authorityForm { userInfo := none, host := Std.Http.URI.Host.ipv6 ::1, port := some 8080 } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "[::1]:8080" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.absoluteForm - { scheme := "http", - authority := some { userInfo := none, host := Std.Http.URI.Host.name "example.com", port := none }, - path := { segments := #["path", "to", "resource"], absolute := true }, - query := #[("query", some "value")], - fragment := none } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "http://example.com/path/to/resource?query=value" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.absoluteForm - { scheme := "https", - authority := some { userInfo := none, host := Std.Http.URI.Host.name "api.example.com", port := some 443 }, - path := { segments := #["v1", "users"], absolute := true }, - query := #[("limit", some "10")], - fragment := none } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "https://api.example.com:443/v1/users?limit=10" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.absoluteForm - { scheme := "https", - authority := some { userInfo := some { username := "user b", password := some "pass" }, - host := Std.Http.URI.Host.name "secure.example.com", - port := none }, - path := { segments := #["private"], absolute := true }, - query := #[], - fragment := none } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "https://user%20b:pass@secure.example.com/private" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.absoluteForm - { scheme := "http", - authority := some { userInfo := none, host := Std.Http.URI.Host.ipv6 2001:db8::1, port := some 8080 }, - path := { segments := #["path"], absolute := true }, - query := #[], - fragment := none } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "http://[2001:db8::1]:8080/path" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.absoluteForm - { scheme := "https", - authority := some { userInfo := none, host := Std.Http.URI.Host.name "example.com", port := none }, - path := { segments := #["page"], absolute := true }, - query := #[], - fragment := some "section1" } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "https://example.com/page#section1" - IO.println (repr result) - -/-- -error: offset 0: expected at least one char --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "?query=only" - IO.println (repr result) - -/-- -error: offset 0: expected at least one char --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "//double//slash//path" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm - { segments := #["very", "long", "path", "with", "many", "segments", "and", "encoded%20spaces", "and%2Bplus%2Bsigns", - "final%2Fsegment"], - absolute := true } - none - none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/very/long/path/with/many/segments/and/encoded%20spaces/and%2Bplus%2Bsigns/final%2Fsegment" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm - { segments := #["api"], absolute := true } - (some #[("filters%5B%5D", some "active"), ("filters%5B%5D", some "verified"), ("sort%5Bname%5D", some "asc"), - ("sort%5Bdate%5D", some "desc")]) - none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/api?filters%5B%5D=active&filters%5B%5D=verified&sort%5Bname%5D=asc&sort%5Bdate%5D=desc" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.absoluteForm - { scheme := "https", - authority := some { userInfo := none, host := Std.Http.URI.Host.name "xn--nxasmq6b.xn--o3cw4h", port := none }, - path := { segments := #["path"], absolute := true }, - query := #[], - fragment := none } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "https://xn--nxasmq6b.xn--o3cw4h/path" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.authorityForm - { userInfo := none, host := Std.Http.URI.Host.name "localhost", port := some 65535 } --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "localhost:65535" - IO.println (repr result) - -/-- -info: Std.Http.RequestTarget.originForm { segments := #[], absolute := true } none none --/ -#guard_msgs in -#eval show IO _ from do - let result ← runParser parseRequestTarget "/" - IO.println (repr result)
diff --git a/tests/lean/run/async_http_uri_tostring.lean b/tests/lean/run/async_http_uri_tostring.lean deleted file mode 100644 index 32f4b05..0000000 --- a/tests/lean/run/async_http_uri_tostring.lean +++ /dev/null
@@ -1,82 +0,0 @@ -import Std.Internal.Http.Protocol.H1.Parser - -open Std.Http.Protocol - -def runParser (parser : Std.Internal.Parsec.ByteArray.Parser α) (s : String) : IO α := - IO.ofExcept ((parser <* Std.Internal.Parsec.eof).run s.toUTF8) - -def parseCheck (s : String) (exact : String := s) : IO Unit := do - let result ← runParser Std.Http.URI.Parser.parseRequestTarget s - if toString result = exact then - pure () - else - throw (.userError s!"expect {exact.quote} but got {(toString result).quote}") - -def parseCheckFail (s : String) : IO Unit := do - match (Std.Http.URI.Parser.parseRequestTarget <* Std.Internal.Parsec.eof).run s.toUTF8 with - | .ok r => - throw <| .userError - s!"expected parse failure, but succeeded with {(repr r)}" - | .error _ => - pure () - -#eval parseCheck "/path/with/encoded%20space" -#eval parseCheck "/path/with/encoded%20space/" -#eval parseCheck "*" -#eval parseCheck "https://ata/b?ata=be#lol%F0%9F%94%A5" -#eval parseCheck "/api/search?q=hello%20world&category=tech%2Bgames" -#eval parseCheck "/search?name=%E2%9C%93%20checked&emoji=%F0%9F%98%80" -#eval parseCheck "/api?param1=value1¶m2=value2¶m3=value3" -#eval parseCheck "/search?debug&verbose&q=test" -#eval parseCheck "/api?empty&also_empty&has_value=something" -#eval parseCheck "/search?q=cats%26dogs&filter=name%3Dmax" -#eval parseCheck "/" -#eval parseCheck "/api/v1/users/123/posts/456/comments/789" -#eval parseCheck "/files/../etc/passwd" -#eval parseCheck "/path%2Fwith%2Fencoded%2Fslashes" -#eval parseCheck "example.com:8080" -#eval parseCheck "https://example.com:8080/ata" -#eval parseCheck "192.168.1.1:3000" -#eval parseCheck "[::1]:8080" -#eval parseCheck "http://example.com/path/to/resource?query=value" -#eval parseCheck "https://api.example.com:443/v1/users?limit=10" -#eval parseCheck "http://[2001:db8::1]:8080/path" -#eval parseCheck "https://example.com/page#section1" -#eval parseCheck "/api?filters%5B%5D=active&filters%5B%5D=verified&sort%5Bname%5D=asc&sort%5Bdate%5D=desc" -#eval parseCheck "https://xn--nxasmq6b.xn--o3cw4h/path" -#eval parseCheck "localhost:65535" -#eval parseCheck "/" -#eval parseCheck "https://user:pass@secure.example.com/private" -#eval parseCheck "/?query=only" -#eval parseCheck "/double//slash//path" - --- Parse decode and encode of the paths differ. We can decode all percent encodings to human format and then --- encode back using other rules that are the `pchar` parsing rules. - -#eval parseCheck "/very/long/path/with/many/segments/and/encoded%20spaces/and%2Bplus%2Bsigns/final%2Fsegment" - "/very/long/path/with/many/segments/and/encoded%20spaces/and%2Bplus%2Bsigns/final%2Fsegment" - -#eval parseCheck "/files/my%20document%2Epdf" - "/files/my%20document%2Epdf" - --- raw spaces (illegal) -#eval parseCheckFail "/path with space" - --- broken percent-encoding -#eval parseCheckFail "/path/%" -#eval parseCheckFail "/path/%2" -#eval parseCheckFail "/path/%ZZ" - --- empty input -#eval parseCheckFail "" - --- malformed IPv6 -#eval parseCheckFail "[::1" -#eval parseCheckFail "[:::1]:80" - --- fragment-only is not a request-target -#eval parseCheckFail "#frag" - --- control characters -#eval parseCheckFail "/path/\n" -#eval parseCheckFail "/path/\u0000"