Removed hard coded ports from thttpclient_standalone.nim. (#18374)

Co-authored-by: Dominik Picheta <dominikpicheta@googlemail.com>
Co-authored-by: Mark Pointing <mark@futurepoint.com.au>
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
This commit is contained in:
Mark Pointing 2021-07-01 03:22:38 +10:00 • committed by GitHub
commit 17911a116d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,29 +2,37 @@ discard """
cmd: "nim c --threads:on $file" cmd: "nim c --threads:on $file"
""" """
import asynchttpserver, httpclient, asyncdispatch, strutils import asynchttpserver, httpclient, asyncdispatch, strutils, net
block: # bug #16436 block: # bug #16436
proc startServer() {.async.} = proc startServer(): AsyncHttpServer =
result = newAsyncHttpServer()
result.listen(Port(0))
proc processRequest(server: AsyncHttpServer) {.async.} =
proc cb(req: Request) {.async.} = proc cb(req: Request) {.async.} =
let headers = { "Content-length": "15"} # Provide invalid content-length let headers = { "Content-length": "15"} # Provide invalid content-length
await req.respond(Http200, "Hello World", headers.newHttpHeaders()) await req.respond(Http200, "Hello World", headers.newHttpHeaders())
var server = newAsyncHttpServer() await server.acceptRequest(cb)
await server.serve(Port(5555), cb)
proc runClient() {.async.} = proc runClient(port: Port) {.async.} =
let c = newAsyncHttpClient(headers = {"Connection": "close"}.newHttpHeaders) let c = newAsyncHttpClient(headers = {"Connection": "close"}.newHttpHeaders)
let r = await c.getContent("http://127.0.0.1:5555") discard await c.getContent("http://127.0.0.1:" & $port)
doAssert false, "should fail earlier" doAssert false, "should fail earlier"
asyncCheck startServer() let server = startServer()
asyncCheck processRequest(server)
let port = server.getPort()
doAssertRaises(ProtocolError): doAssertRaises(ProtocolError):
waitFor runClient() waitFor runClient(port)
block: # bug #14794 (And test for presence of content-length header when using postContent) block: # bug #14794 (And test for presence of content-length header when using postContent)
proc startServer() {.async.} = proc startServer(): AsyncHttpServer =
var killServer = false result = newAsyncHttpServer()
result.listen(Port(0))
proc runServer(server: AsyncHttpServer) {.async.} =
proc cb(req: Request) {.async.} = proc cb(req: Request) {.async.} =
doAssert(req.body.endsWith(httpNewLine), "Multipart body does not end with a newline.") doAssert(req.body.endsWith(httpNewLine), "Multipart body does not end with a newline.")
# this next line is probably not required because asynchttpserver does not call # this next line is probably not required because asynchttpserver does not call
@ -32,23 +40,18 @@ block: # bug #14794 (And test for presence of content-length header when using p
# Error: unhandled exception: 411 Length Required # Error: unhandled exception: 411 Length Required
# Added for good measure in case the server becomes more permissive. # Added for good measure in case the server becomes more permissive.
doAssert(req.headers.hasKey("content-length"), "Content-Length header is not present.") doAssert(req.headers.hasKey("content-length"), "Content-Length header is not present.")
killServer = true
asyncCheck req.respond(Http200, "OK") asyncCheck req.respond(Http200, "OK")
var server = newAsyncHttpServer() await server.acceptRequest(cb)
server.listen(Port(5556))
while not killServer:
if server.shouldAcceptRequest():
await server.acceptRequest(cb)
else:
poll()
proc runClient() {.async.} = proc runClient(port: Port) {.async.} =
let c = newAsyncHttpClient() let c = newAsyncHttpClient()
var data = newMultipartData() let data = newMultipartData()
data.add("file.txt", "This is intended to be an example text file.\r\nThis would be the second line.\r\n") data.add("file.txt", "This is intended to be an example text file.\r\nThis would be the second line.\r\n")
let r = await c.postContent("http://127.0.0.1:5556", multipart = data) discard await c.postContent("http://127.0.0.1:" & $port, multipart = data)
c.close() c.close()
asyncCheck startServer() let server = startServer()
waitFor runClient() let port = server.getPort()
asyncCheck runServer(server)
waitFor runClient(port)