spec/rapidash/response_spec.rb in rapidash-0.1.0 vs spec/rapidash/response_spec.rb in rapidash-0.1.1
- old
+ new
@@ -9,11 +9,31 @@
},
:body => body
})
end
+def valid_html_response_object
+ body = {"foo" => "bar" }.to_json
+ OpenStruct.new({
+ :headers => {
+ "content-type" => "text/html"
+ },
+ :body => body
+ })
+end
+def valid_js_response_object
+ body = {"foo" => "bar" }.to_json
+ OpenStruct.new({
+ :headers => {
+ "content-type" => "text/javascript"
+ },
+ :body => body
+ })
+end
+
+
def valid_response_array
body = [{"foo" => "bar" }, {"baz" => "bra"}].to_json
OpenStruct.new({
:headers => {
"content-type" => "application/json"
@@ -30,10 +50,19 @@
},
:body => "<xml>something</xml>"
})
end
+def invalid_js_response_object
+ OpenStruct.new({
+ :headers => {
+ "content-type" => "text/javascript"
+ },
+ :body => "<xml>something</xml>"
+ })
+end
+
def nil_response
OpenStruct.new({
:body => nil
})
end
@@ -43,10 +72,20 @@
describe "#new" do
it "should parse JSON Objects" do
response = Rapidash::Response.new(valid_response_object)
response.foo.should eql("bar")
end
+
+ it "should parse JSON Objects returned with javascript type" do
+ response = Rapidash::Response.new(valid_js_response_object)
+ response.foo.should eql("bar")
+ end
+
+ it "should parse JSON Objects returned with html type" do
+ response = Rapidash::Response.new(valid_html_response_object)
+ response.foo.should eql("bar")
+ end
it "should parse JSON Arrays" do
response = Rapidash::Response.new(valid_response_array)
response[0].foo.should eql("bar")
response[1].baz.should eql("bra")
@@ -61,9 +100,15 @@
it "should raise an error on a non-json response" do
expect {
Rapidash::Response.new(invalid_response)
}.to raise_error(Rapidash::ParseError)
end
+
+ it "should raise an error on a non-json response body" do
+ expect {
+ Rapidash::Response.new(invalid_js_response_object)
+ }.to raise_error(Rapidash::ParseError)
+ end
end
end