Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Dec 29, 2023
1 parent f7d4ec8 commit 4f39ec5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/hyperx/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ when defined(hyperxTest):
proc putTestData*(client: ClientContext, data: string) {.async.} =
await client.sock.data.put data

proc testDataSent*(client: ClientContext): seq[byte] =
result = client.sock.sent

when isMainModule:
when not defined(hyperxTest):
{.error: "tests need -d:hyperxTest".}
Expand Down
2 changes: 1 addition & 1 deletion src/hyperx/frame.nim
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func newFrame*(): Frame {.inline.} =
Frame()

func setRawBytes*(frm: Frame, data: string) =
doAssert data.len <= frmHeaderSize
doAssert data.len <= frmHeaderMaxSize
for i in 0 .. data.len-1:
frm.s[i] = data[i].byte
frm.rawL = data.len.int8
Expand Down
11 changes: 8 additions & 3 deletions src/hyperx/testsocket.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ type
data*: QueueAsync[string]
buff: string
i: int
sent*: seq[byte]
isConnected*: bool
hostname*: string
port*: Port

proc newMySocket*(): TestSocket =
TestSocket(
data: newQueue[string](10),
data: newQueue[string](100),
buff: "",
i: 0,
isConnected: false,
Expand All @@ -34,10 +35,14 @@ proc recv*(s: TestSocket, i: int): Future[string] {.async.} =
s.i += i

proc send*(s: TestSocket, data: ptr byte, ln: int) {.async.} =
discard
if ln > 0:
var bytes = newSeq[byte](ln)
copyMem(addr bytes[0], data, ln)
s.sent.add bytes

proc send*(s: TestSocket, data: string) {.async.} =
discard
for c in data:
s.sent.add c.byte

proc connect*(s: TestSocket, hostname: string, port: Port) {.async.} =
doAssert not s.isConnected
Expand Down
29 changes: 28 additions & 1 deletion src/hyperx/testutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ when not defined(hyperxTest):

import std/strutils
import std/asyncdispatch
import pkg/hpack/encoder
import pkg/hpack
import ./frame
import ./client

Expand Down Expand Up @@ -84,6 +84,33 @@ proc reply*(
await tc.c.putTestData text
tc.sid += 2

type TestRequest = object
frm: Frame
payload: string

proc sent*(tc: TestClientContext): seq[TestRequest] =
result = newSeq[TestRequest]()
let data = tc.c.testDataSent()
if data.len == 0:
return
var dh = initDynHeaders(1024, 16)
#doAssert tc.prefaceSent()
const prefaceLen = "PRI * HTTP/2.0\r\L\r\LSM\r\L\r\L".len
var i = prefaceLen
while i < data.len:
var frame = newFrame()
frame.setRawBytes data[i .. i+frmHeaderSize-1].toString
frame.setRawBytes data[i .. i+frame.len-1].toString
i += frame.len
let payload = data[i .. i+frame.payloadLen.int-1]
i += payload.len
if frame.typ == frmtHeaders:
var ds = initDecodedStr()
hdecodeAll(payload, dh, ds)
result.add TestRequest(frm: frame, payload: $ds)
else:
result.add TestRequest(frm: frame, payload: payload.toString)

when isMainModule:
block:
testAsync "foobar":
Expand Down
54 changes: 51 additions & 3 deletions tests/testclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import std/asyncdispatch
import ../src/hyperx/client
import ../src/hyperx/testutils
import ../src/hyperx/frame

testAsync "sanity check req/resp":
testAsync "simple response":
const headers = ":method: foobar\r\L"
const text = "foobar body"
var tc = newTestClient("example.com")
Expand All @@ -16,7 +17,7 @@ testAsync "sanity check req/resp":
doAssert tc.resps[0].headers == headers
doAssert tc.resps[0].text == text

testAsync "sanity check multiple req/resp":
testAsync "multiple responses":
const
headers = ":method: foo\r\L"
text = "foo body"
Expand All @@ -35,7 +36,7 @@ testAsync "sanity check multiple req/resp":
doAssert tc.resps[1].headers == headers2
doAssert tc.resps[1].text == text2

testAsync "sanity check multiple req/resp order":
testAsync "multiple responses unordered":
const
headers = ":method: foo\r\L"
text = "foo body"
Expand All @@ -53,3 +54,50 @@ testAsync "sanity check multiple req/resp order":
doAssert tc.resps[0].text == text
doAssert tc.resps[1].headers == headers2
doAssert tc.resps[1].text == text2

testAsync "simple request":
var tc = newTestClient("example.com")
withConnection tc:
await (
tc.get("/") and
tc.reply("foo: foo\r\L", "bar")
)
let reqs = tc.sent()
doAssert reqs[0].frm.sid == frmsidMain
doAssert reqs[0].frm.typ == frmtSettings
doAssert reqs[0].payload.len == 0
doAssert reqs[1].frm.sid.int == 1
doAssert reqs[1].frm.typ == frmtHeaders
doAssert reqs[1].payload ==
":method: GET\r\L" &
":scheme: https\r\L" &
":path: /\r\L" &
":authority: example.com\r\L"

testAsync "multiple requests":
var tc = newTestClient("example.com")
withConnection tc:
await (
tc.get("/1") and
tc.reply("foo: foo\r\L", "bar") and
tc.get("/2") and
tc.reply("foo: foo\r\L", "bar")
)
let reqs = tc.sent()
doAssert reqs[0].frm.sid == frmsidMain
doAssert reqs[0].frm.typ == frmtSettings
doAssert reqs[0].payload.len == 0
doAssert reqs[1].frm.sid.int == 1
doAssert reqs[1].frm.typ == frmtHeaders
doAssert reqs[1].payload ==
":method: GET\r\L" &
":scheme: https\r\L" &
":path: /1\r\L" &
":authority: example.com\r\L"
doAssert reqs[2].frm.sid.int == 3
doAssert reqs[2].frm.typ == frmtHeaders
doAssert reqs[2].payload ==
":method: GET\r\L" &
":scheme: https\r\L" &
":path: /2\r\L" &
":authority: example.com\r\L"

0 comments on commit 4f39ec5

Please sign in to comment.