Handle redirects in HttpClient's post procs & post test.

This commit is contained in:
Dominik Picheta
2016-09-24 17:50:58 +02:00
parent 547fb7f1e4
commit fa9ec7a6b5
2 changed files with 27 additions and 18 deletions

View File

@@ -964,6 +964,8 @@ proc get*(client: HttpClient | AsyncHttpClient,
## This procedure will follow redirects up to a maximum number of redirects
## specified in ``client.maxRedirects``.
result = await client.request(url, HttpGET)
# Handle redirects.
var lastURL = url
for i in 1..client.maxRedirects:
if result.status.redirection():
@@ -990,3 +992,11 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
client.headers["Content-Length"] = $len(xb)
result = await client.request(url, HttpPOST, xb)
# Handle redirects.
var lastURL = url
for i in 1..client.maxRedirects:
if result.status.redirection():
let redirectTo = getNewLocation(lastURL, result.headers)
var meth = if result.status != "307": HttpGet else: HttpPost
result = await client.request(redirectTo, meth, xb)
lastURL = redirectTo

View File

@@ -20,6 +20,15 @@ proc asyncTest() {.async.} =
resp = await client.request("https://google.com/")
doAssert(resp.code.is2xx or resp.code.is3xx)
# Multipart test.
var data = newMultipartData()
data["output"] = "soap12"
data["uploaded_file"] = ("test.html", "text/html",
"<html><head></head><body><p>test</p></body></html>")
resp = await client.post("http://validator.w3.org/check", multipart=data)
doAssert(resp.code.is2xx)
client.close()
# Proxy test
@@ -41,6 +50,14 @@ proc syncTest() =
resp = client.request("https://google.com/")
doAssert(resp.code.is2xx or resp.code.is3xx)
# Multipart test.
var data = newMultipartData()
data["output"] = "soap12"
data["uploaded_file"] = ("test.html", "text/html",
"<html><head></head><body><p>test</p></body></html>")
resp = client.post("http://validator.w3.org/check", multipart=data)
doAssert(resp.code.is2xx)
client.close()
# Timeout test.
@@ -56,21 +73,3 @@ proc syncTest() =
syncTest()
waitFor(asyncTest())
#[
else:
#downloadFile("http://force7.de/nim/index.html", "nimindex.html")
#downloadFile("http://www.httpwatch.com/", "ChunkTest.html")
#downloadFile("http://validator.w3.org/check?uri=http%3A%2F%2Fgoogle.com",
# "validator.html")
#var r = get("http://validator.w3.org/check?uri=http%3A%2F%2Fgoogle.com&
# charset=%28detect+automatically%29&doctype=Inline&group=0")
var data = newMultipartData()
data["output"] = "soap12"
data["uploaded_file"] = ("test.html", "text/html",
"<html><head></head><body><p>test</p></body></html>")
echo postContent("http://validator.w3.org/check", multipart=data)]#