Skip to content

Commit

Permalink
Merge branch 'release/0.18'
Browse files Browse the repository at this point in the history
Ary Borenszweig committed Jun 24, 2016

Verified

This commit was signed with the committer’s verified signature. The key has expired.
2 parents 0041ed9 + 409cd76 commit a8dc441
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions spec/std/http/request_spec.cr
Original file line number Diff line number Diff line change
@@ -75,51 +75,51 @@ module HTTP
end

it "parses GET" do
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\r\nHost: host.example.org\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\r\nHost: host.example.org\r\n\r\n")).as(Request)
request.method.should eq("GET")
request.path.should eq("/")
request.headers.should eq({"Host" => "host.example.org"})
end

it "parses GET with query params" do
request = Request.from_io(MemoryIO.new("GET /greet?q=hello&name=world HTTP/1.1\r\nHost: host.example.org\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /greet?q=hello&name=world HTTP/1.1\r\nHost: host.example.org\r\n\r\n")).as(Request)
request.method.should eq("GET")
request.path.should eq("/greet")
request.headers.should eq({"Host" => "host.example.org"})
end

it "parses GET without \\r" do
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\nHost: host.example.org\n\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\nHost: host.example.org\n\n")).as(Request)
request.method.should eq("GET")
request.path.should eq("/")
request.headers.should eq({"Host" => "host.example.org"})
end

it "parses empty header" do
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\r\nHost: host.example.org\r\nReferer:\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\r\nHost: host.example.org\r\nReferer:\r\n\r\n")).as(Request)
request.method.should eq("GET")
request.path.should eq("/")
request.headers.should eq({"Host" => "host.example.org", "Referer" => ""})
end

it "parses GET with cookie" do
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\r\nHost: host.example.org\r\nCookie: a=b\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\r\nHost: host.example.org\r\nCookie: a=b\r\n\r\n")).as(Request)
request.method.should eq("GET")
request.path.should eq("/")
request.cookies["a"].value.should eq("b")
request.headers.should eq({"Host" => "host.example.org"})
end

it "headers are case insensitive" do
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\r\nHost: host.example.org\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\r\nHost: host.example.org\r\n\r\n")).as(Request)
headers = request.headers.not_nil!
headers["HOST"].should eq("host.example.org")
headers["host"].should eq("host.example.org")
headers["Host"].should eq("host.example.org")
end

it "parses POST (with body)" do
request = Request.from_io(MemoryIO.new("POST /foo HTTP/1.1\r\nContent-Length: 13\r\n\r\nthisisthebody")).not_nil!
request = Request.from_io(MemoryIO.new("POST /foo HTTP/1.1\r\nContent-Length: 13\r\n\r\nthisisthebody")).as(Request)
request.method.should eq("POST")
request.path.should eq("/foo")
request.headers.should eq({"Content-Length" => "13"})
@@ -128,7 +128,7 @@ module HTTP

it "handles malformed request" do
request = Request.from_io(MemoryIO.new("nonsense"))
request.should be_nil
request.should be_a(Request::BadRequest)
end

describe "keep-alive" do
@@ -163,26 +163,26 @@ module HTTP

describe "#path" do
it "returns parsed path" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).as(Request)
request.path.should eq("/api/v3/some/resource")
end
end

describe "#path=" do
it "sets path" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).as(Request)
request.path = "/api/v2/greet"
request.path.should eq("/api/v2/greet")
end

it "updates @resource" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).as(Request)
request.path = "/api/v2/greet"
request.resource.should eq("/api/v2/greet?filter=hello&world=test")
end

it "updates serialized form" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).as(Request)
request.path = "/api/v2/greet"

io = MemoryIO.new
@@ -193,26 +193,26 @@ module HTTP

describe "#query" do
it "returns request's query" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).as(Request)
request.query.should eq("filter=hello&world=test")
end
end

describe "#query=" do
it "sets query" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).as(Request)
request.query = "q=isearchforsomething&locale=de"
request.query.should eq("q=isearchforsomething&locale=de")
end

it "updates @resource" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).as(Request)
request.query = "q=isearchforsomething&locale=de"
request.resource.should eq("/api/v3/some/resource?q=isearchforsomething&locale=de")
end

it "updates serialized form" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?filter=hello&world=test HTTP/1.1\r\n\r\n")).as(Request)
request.query = "q=isearchforsomething&locale=de"

io = MemoryIO.new
@@ -223,7 +223,7 @@ module HTTP

describe "#query_params" do
it "returns parsed HTTP::Params" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).as(Request)
params = request.query_params

params["foo"].should eq("bar")
@@ -232,30 +232,30 @@ module HTTP
end

it "happily parses when query is not a canonical url-encoded string" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?{\"hello\":\"world\"} HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?{\"hello\":\"world\"} HTTP/1.1\r\n\r\n")).as(Request)
params = request.query_params
params["{\"hello\":\"world\"}"].should eq("")
params.to_s.should eq("%7B%22hello%22%3A%22world%22%7D=")
end

it "affects #query when modified" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).as(Request)
params = request.query_params

params["foo"] = "not-bar"
request.query.should eq("foo=not-bar&foo=baz&baz=qux")
end

it "updates @resource when modified" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).as(Request)
params = request.query_params

params["foo"] = "not-bar"
request.resource.should eq("/api/v3/some/resource?foo=not-bar&foo=baz&baz=qux")
end

it "updates serialized form when modified" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).as(Request)
params = request.query_params

params["foo"] = "not-bar"
@@ -266,7 +266,7 @@ module HTTP
end

it "is affected when #query is modified" do
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).not_nil!
request = Request.from_io(MemoryIO.new("GET /api/v3/some/resource?foo=bar&foo=baz&baz=qux HTTP/1.1\r\n\r\n")).as(Request)
params = request.query_params

new_query = "foo=not-bar&foo=not-baz&not-baz=hello&name=world"

0 comments on commit a8dc441

Please sign in to comment.