Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Dec 28, 2023
1 parent ea9076f commit f7d4ec8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 56 deletions.
1 change: 1 addition & 0 deletions hyperx.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ task test, "Test":
exec "nim c -r src/hyperx/queue.nim"
exec "nim c -r src/hyperx/stream.nim"
exec "nim c -r src/hyperx/frame.nim"
exec "nim c -r -d:hyperxTest src/hyperx/testutils.nim"
exec "nim c -r -d:hyperxTest src/hyperx/client.nim"
exec "nim c -r -d:hyperxTest tests/testclient.nim"

Expand Down
24 changes: 21 additions & 3 deletions src/hyperx/testutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import pkg/hpack/encoder
import ./frame
import ./client

template test*(name: string, body: untyped): untyped =
block:
template testAsync*(name: string, body: untyped): untyped =
(proc () =
echo "test " & name
body
proc test() {.async.} =
body
waitFor test()
)()

func toString(bytes: openArray[byte]): string =
let L = bytes.len
Expand Down Expand Up @@ -80,3 +83,18 @@ proc reply*(
)
await tc.c.putTestData text
tc.sid += 2

when isMainModule:
block:
testAsync "foobar":
doAssert true
block:
var asserted = false
try:
testAsync "foobar":
doAssert false
except AssertionDefect:
asserted = true
doAssert asserted

echo "ok"
100 changes: 47 additions & 53 deletions tests/testclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,52 @@ import std/asyncdispatch
import ../src/hyperx/client
import ../src/hyperx/testutils

test "sanity check req/resp":
proc test() {.async.} =
const headers = ":method: foobar\r\L"
const text = "foobar body"
var tc = newTestClient("example.com")
withConnection tc:
await (
tc.get("/") and
tc.reply(headers, text)
)
doAssert tc.resps[0].headers == headers
doAssert tc.resps[0].text == text
waitFor test()
testAsync "sanity check req/resp":
const headers = ":method: foobar\r\L"
const text = "foobar body"
var tc = newTestClient("example.com")
withConnection tc:
await (
tc.get("/") and
tc.reply(headers, text)
)
doAssert tc.resps[0].headers == headers
doAssert tc.resps[0].text == text

test "sanity check multiple req/resp":
proc test() {.async.} =
const
headers = ":method: foo\r\L"
text = "foo body"
headers2 = ":method: bar\r\L"
text2 = "bar body"
var tc = newTestClient("example.com")
withConnection tc:
await (
tc.get("/") and
tc.reply(headers, text) and
tc.get("/") and
tc.reply(headers2, text2)
)
doAssert tc.resps[0].headers == headers
doAssert tc.resps[0].text == text
doAssert tc.resps[1].headers == headers2
doAssert tc.resps[1].text == text2
waitFor test()
testAsync "sanity check multiple req/resp":
const
headers = ":method: foo\r\L"
text = "foo body"
headers2 = ":method: bar\r\L"
text2 = "bar body"
var tc = newTestClient("example.com")
withConnection tc:
await (
tc.get("/") and
tc.reply(headers, text) and
tc.get("/") and
tc.reply(headers2, text2)
)
doAssert tc.resps[0].headers == headers
doAssert tc.resps[0].text == text
doAssert tc.resps[1].headers == headers2
doAssert tc.resps[1].text == text2

test "sanity check multiple req/resp order":
proc test() {.async.} =
const
headers = ":method: foo\r\L"
text = "foo body"
headers2 = ":method: bar\r\L"
text2 = "bar body"
var tc = newTestClient("example.com")
withConnection tc:
await (
tc.get("/") and
tc.get("/") and
tc.reply(headers, text) and
tc.reply(headers2, text2)
)
doAssert tc.resps[0].headers == headers
doAssert tc.resps[0].text == text
doAssert tc.resps[1].headers == headers2
doAssert tc.resps[1].text == text2
waitFor test()
testAsync "sanity check multiple req/resp order":
const
headers = ":method: foo\r\L"
text = "foo body"
headers2 = ":method: bar\r\L"
text2 = "bar body"
var tc = newTestClient("example.com")
withConnection tc:
await (
tc.get("/") and
tc.get("/") and
tc.reply(headers, text) and
tc.reply(headers2, text2)
)
doAssert tc.resps[0].headers == headers
doAssert tc.resps[0].text == text
doAssert tc.resps[1].headers == headers2
doAssert tc.resps[1].text == text2

0 comments on commit f7d4ec8

Please sign in to comment.