Skip to content

Commit

Permalink
use String(take!(r)) instead of take!(String, r) (JuliaWeb#39)
Browse files Browse the repository at this point in the history
no other IO types do the latter
  • Loading branch information
tkelman authored and quinnj committed Mar 18, 2017
1 parent b407b24 commit b1fffb3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ Build and execute an http "$($f_str)" request. Query parameters can be passed vi
query parameters with the same key can be passed like `Dict("key1"=>["value1", "value2"], "key2"=>...)`.
Returns a `Response` object that includes the resulting status code (`HTTP.status(r)` and `HTTP.statustext(r)`),
response headers (`HTTP.headers(r)`), cookies (`HTTP.cookies(r)`), response history if redirects were involved
(`HTTP.history(r)`), and response body (`HTTP.body(r)` or `take!(String, r)` or `take!(r)`).
(`HTTP.history(r)`), and response body (`HTTP.body(r)` or `String(take!(r))` or `take!(r)`).
The body or payload for a request can be given through the `body` keyword arugment.
The body can be given as a `String`, `Vector{UInt8}`, `IO`, `HTTP.FIFOBuffer` or `Dict` argument type.
Expand Down
5 changes: 2 additions & 3 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Accessor methods include:
Two convenience methods are provided for accessing a request body:
* `take!(r)`: consume the request body, returning it as a `Vector{UInt8}`
* `take!(String, r)`: consume the request body, returning it as a `String`
* `String(take!(r))`: consume the request body, returning it as a `String`
"""
type Request
method::HTTP.Method
Expand Down Expand Up @@ -296,7 +296,7 @@ Accessor methods include:
Two convenience methods are provided for accessing a response body:
* `take!(r)`: consume the response body, returning it as a `Vector{UInt8}`
* `take!(String, r)`: consume the response body, returning it as a `String`
* `String(take!(r))`: consume the response body, returning it as a `String`
"""
type Response
status::Int32
Expand All @@ -320,7 +320,6 @@ history(r::Response) = r.history
statustext(r::Response) = Base.get(STATUS_CODES, r.status, "Unknown Code")
body(r::Union{Request, Response}) = r.body
Base.take!(r::Union{Request, Response}) = readavailable(body(r))
Base.take!(::Type{String}, r::Union{Request, Response}) = String(take!(r))
Base.String(r::Union{Request, Response}) = String(body(r))

Response(; status::Int=200,
Expand Down
18 changes: 9 additions & 9 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ for sch in ("http", "https")
@test (haskey(h, "Hey") ? h["Hey"] == "dude" : h["hey"] == "dude")

r = HTTP.get("$sch://httpbin.org/cookies")
body = take!(String, r)
body = String(take!(r))
@test (body == "{\n \"cookies\": {}\n}\n" || body == "{\n \"cookies\": {\n \"hey\": \"\"\n }\n}\n" || body == "{\n \"cookies\": {\n \"hey\": \"sailor\"\n }\n}\n")
r = HTTP.get("$sch://httpbin.org/cookies/set?hey=sailor")
@test HTTP.status(r) == 200
body = take!(String, r)
body = String(take!(r))
@test (body == "{\n \"cookies\": {\n \"hey\": \"sailor\"\n }\n}\n" || body == "{\n \"cookies\": {\n \"hey\": \"\"\n }\n}\n")

# r = HTTP.get("$sch://httpbin.org/cookies/delete?hey")
# @test take!(String, r) == "{\n \"cookies\": {\n \"hey\": \"\"\n }\n}\n"
# @test String(take!(r)) == "{\n \"cookies\": {\n \"hey\": \"\"\n }\n}\n"

# stream
r = HTTP.post("$sch://httpbin.org/post"; body="hey")
Expand Down Expand Up @@ -98,19 +98,19 @@ for sch in ("http", "https")
# multipart
r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there"))
@test HTTP.status(r) == 200
@test startswith(take!(String, r), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }")
@test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }")

r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there"), chunksize=1000)
@test HTTP.status(r) == 200
@test startswith(take!(String, r), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }")
@test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }")

tmp = tempname()
open(f->write(f, "hey"), tmp, "w")
io = open(tmp)
r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "iostream"=>io))
close(io); rm(tmp)
@test HTTP.status(r) == 200
str = take!(String, r)
str = String(take!(r))
@test startswith(str, "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"iostream\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }")

tmp = tempname()
Expand All @@ -119,7 +119,7 @@ for sch in ("http", "https")
r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "iostream"=>io), chunksize=1000)
close(io); rm(tmp)
@test HTTP.status(r) == 200
@test startswith(take!(String, r), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"iostream\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }")
@test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"iostream\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }")

tmp = tempname()
open(f->write(f, "hey"), tmp, "w")
Expand All @@ -128,7 +128,7 @@ for sch in ("http", "https")
r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "multi"=>m))
close(io); rm(tmp)
@test HTTP.status(r) == 200
@test startswith(take!(String, r), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }")
@test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }")

tmp = tempname()
open(f->write(f, "hey"), tmp, "w")
Expand All @@ -137,7 +137,7 @@ for sch in ("http", "https")
r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "multi"=>m), chunksize=1000)
close(io); rm(tmp)
@test HTTP.status(r) == 200
@test startswith(take!(String, r), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }")
@test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }")

# asynchronous
f = HTTP.FIFOBuffer()
Expand Down
2 changes: 1 addition & 1 deletion test/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sleep(1.0)
r = HTTP.get("http://127.0.0.1:8081/"; readtimeout=30)

@test HTTP.status(r) == 200
@test take!(String, r) == ""
@test String(take!(r)) == ""

print(readstring(serverlog))

Expand Down

0 comments on commit b1fffb3

Please sign in to comment.