Move async example to asynchronous version of proc (#18078)
* improve runnableExamples in std/httpclient * Add synchronous example. * Update lib/pure/httpclient.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
This commit is contained in:
parent
d217888e56
commit
478f717377
1 changed files with 14 additions and 7 deletions
|
|
@ -574,15 +574,11 @@ proc newHttpClient*(userAgent = defUserAgent, maxRedirects = 5,
|
||||||
##
|
##
|
||||||
## `headers` specifies the HTTP Headers.
|
## `headers` specifies the HTTP Headers.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
import std/[asyncdispatch, httpclient, strutils]
|
import std/strutils
|
||||||
|
|
||||||
proc asyncProc(): Future[string] {.async.} =
|
let exampleHtml = newHttpClient().getContent("http://example.com")
|
||||||
var client = newAsyncHttpClient()
|
|
||||||
return await client.getContent("http://example.com")
|
|
||||||
|
|
||||||
let exampleHtml = waitFor asyncProc()
|
|
||||||
assert "Example Domain" in exampleHtml
|
assert "Example Domain" in exampleHtml
|
||||||
assert not ("Pizza" in exampleHtml)
|
assert "Pizza" notin exampleHtml
|
||||||
|
|
||||||
new result
|
new result
|
||||||
result.headers = headers
|
result.headers = headers
|
||||||
|
|
@ -616,6 +612,17 @@ proc newAsyncHttpClient*(userAgent = defUserAgent, maxRedirects = 5,
|
||||||
## connections.
|
## connections.
|
||||||
##
|
##
|
||||||
## `headers` specifies the HTTP Headers.
|
## `headers` specifies the HTTP Headers.
|
||||||
|
runnableExamples:
|
||||||
|
import std/[asyncdispatch, strutils]
|
||||||
|
|
||||||
|
proc asyncProc(): Future[string] {.async.} =
|
||||||
|
let client = newAsyncHttpClient()
|
||||||
|
result = await client.getContent("http://example.com")
|
||||||
|
|
||||||
|
let exampleHtml = waitFor asyncProc()
|
||||||
|
assert "Example Domain" in exampleHtml
|
||||||
|
assert "Pizza" notin exampleHtml
|
||||||
|
|
||||||
new result
|
new result
|
||||||
result.headers = headers
|
result.headers = headers
|
||||||
result.userAgent = userAgent
|
result.userAgent = userAgent
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue