spec/model/paths_spec.rb in her-0.3.1 vs spec/model/paths_spec.rb in her-0.3.2

- old
+ new

@@ -8,32 +8,58 @@ spawn_model "Foo::User" end # }}} describe "#build_request_path" do it "builds paths with defaults" do # {{{ - Foo::User.build_request_path(:id => "foo").should == "/users/foo" - Foo::User.build_request_path.should == "/users" + Foo::User.build_request_path(:id => "foo").should == "users/foo" + Foo::User.build_request_path.should == "users" end # }}} it "builds paths with custom collection path" do # {{{ Foo::User.collection_path "/utilisateurs" Foo::User.build_request_path(:id => "foo").should == "/utilisateurs/foo" Foo::User.build_request_path.should == "/utilisateurs" end # }}} + it "builds paths with custom relative collection path" do # {{{ + Foo::User.collection_path "utilisateurs" + Foo::User.build_request_path(:id => "foo").should == "utilisateurs/foo" + Foo::User.build_request_path.should == "utilisateurs" + end # }}} + it "builds paths with custom collection path with multiple variables" do # {{{ Foo::User.collection_path "/organizations/:organization_id/utilisateurs" + Foo::User.build_request_path(:id => "foo", :_organization_id => "acme").should == "/organizations/acme/utilisateurs/foo" Foo::User.build_request_path(:_organization_id => "acme").should == "/organizations/acme/utilisateurs" + + Foo::User.build_request_path(:id => "foo", :organization_id => "acme").should == "/organizations/acme/utilisateurs/foo" + Foo::User.build_request_path(:organization_id => "acme").should == "/organizations/acme/utilisateurs" end # }}} + it "builds paths with custom relative collection path with multiple variables" do # {{{ + Foo::User.collection_path "organizations/:organization_id/utilisateurs" + + Foo::User.build_request_path(:id => "foo", :_organization_id => "acme").should == "organizations/acme/utilisateurs/foo" + Foo::User.build_request_path(:_organization_id => "acme").should == "organizations/acme/utilisateurs" + + Foo::User.build_request_path(:id => "foo", :organization_id => "acme").should == "organizations/acme/utilisateurs/foo" + Foo::User.build_request_path(:organization_id => "acme").should == "organizations/acme/utilisateurs" + end # }}} + it "builds paths with custom item path" do # {{{ Foo::User.resource_path "/utilisateurs/:id" Foo::User.build_request_path(:id => "foo").should == "/utilisateurs/foo" - Foo::User.build_request_path.should == "/users" + Foo::User.build_request_path.should == "users" end # }}} + it "builds paths with custom relative item path" do # {{{ + Foo::User.resource_path "utilisateurs/:id" + Foo::User.build_request_path(:id => "foo").should == "utilisateurs/foo" + Foo::User.build_request_path.should == "users" + end # }}} + it "raises exceptions when building a path without required custom variables" do # {{{ Foo::User.collection_path "/organizations/:organization_id/utilisateurs" expect { Foo::User.build_request_path(:id => "foo") }.to raise_error(Her::Errors::PathError) end # }}} end @@ -44,69 +70,92 @@ spawn_model "Foo::AdminUser" end # }}} describe "#build_request_path" do it "builds paths with defaults" do # {{{ - Foo::AdminUser.build_request_path(:id => "foo").should == "/admin_users/foo" - Foo::AdminUser.build_request_path.should == "/admin_users" + Foo::AdminUser.build_request_path(:id => "foo").should == "admin_users/foo" + Foo::AdminUser.build_request_path.should == "admin_users" end # }}} it "builds paths with custom collection path" do # {{{ Foo::AdminUser.collection_path "/users" Foo::AdminUser.build_request_path(:id => "foo").should == "/users/foo" Foo::AdminUser.build_request_path.should == "/users" end # }}} + it "builds paths with custom relative collection path" do # {{{ + Foo::AdminUser.collection_path "users" + Foo::AdminUser.build_request_path(:id => "foo").should == "users/foo" + Foo::AdminUser.build_request_path.should == "users" + end # }}} + it "builds paths with custom collection path with multiple variables" do # {{{ Foo::AdminUser.collection_path "/organizations/:organization_id/users" Foo::AdminUser.build_request_path(:id => "foo", :_organization_id => "acme").should == "/organizations/acme/users/foo" Foo::AdminUser.build_request_path(:_organization_id => "acme").should == "/organizations/acme/users" end # }}} + it "builds paths with custom relative collection path with multiple variables" do # {{{ + Foo::AdminUser.collection_path "organizations/:organization_id/users" + Foo::AdminUser.build_request_path(:id => "foo", :_organization_id => "acme").should == "organizations/acme/users/foo" + Foo::AdminUser.build_request_path(:_organization_id => "acme").should == "organizations/acme/users" + end # }}} + it "builds paths with custom item path" do # {{{ Foo::AdminUser.resource_path "/users/:id" Foo::AdminUser.build_request_path(:id => "foo").should == "/users/foo" - Foo::AdminUser.build_request_path.should == "/admin_users" + Foo::AdminUser.build_request_path.should == "admin_users" end # }}} + it "builds paths with custom relative item path" do # {{{ + Foo::AdminUser.resource_path "users/:id" + Foo::AdminUser.build_request_path(:id => "foo").should == "users/foo" + Foo::AdminUser.build_request_path.should == "admin_users" + end # }}} + it "raises exceptions when building a path without required custom variables" do # {{{ Foo::AdminUser.collection_path "/organizations/:organization_id/users" expect { Foo::AdminUser.build_request_path(:id => "foo") }.to raise_error(Her::Errors::PathError) end # }}} + + it "raises exceptions when building a relative path without required custom variables" do # {{{ + Foo::AdminUser.collection_path "organizations/:organization_id/users" + expect { Foo::AdminUser.build_request_path(:id => "foo") }.to raise_error(Her::Errors::PathError) + end # }}} end end context "nested model" do before do # {{{ spawn_model "Foo::User" end # }}} describe "#build_request_path" do it "builds paths with defaults" do # {{{ - Foo::User.build_request_path(:id => "foo").should == "/users/foo" - Foo::User.build_request_path.should == "/users" + Foo::User.build_request_path(:id => "foo").should == "users/foo" + Foo::User.build_request_path.should == "users" end # }}} end end end - context "making HTTP requests" do + context "making subdomain HTTP requests" do before do # {{{ - Her::API.setup :url => "https://api.example.com" do |builder| + Her::API.setup :url => "https://api.example.com/" do |builder| builder.use Her::Middleware::FirstLevelParseJSON builder.use Faraday::Request::UrlEncoded builder.adapter :test do |stub| - stub.get("/organizations/2/users") { |env| [200, {}, [{ :id => 1, :fullname => "Tobias Fünke", :organization_id => 2 }, { :id => 2, :fullname => "Lindsay Fünke", :organization_id => 2 }].to_json] } - stub.post("/organizations/2/users") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke", :organization_id => 2 }.to_json] } - stub.put("/organizations/2/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke", :organization_id => 2 }.to_json] } - stub.get("/organizations/2/users/1") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke", :organization_id => 2, :active => true }.to_json] } - stub.delete("/organizations/2/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke", :organization_id => 2, :active => false }.to_json] } + stub.get("organizations/2/users") { |env| [200, {}, [{ :id => 1, :fullname => "Tobias Fünke", :organization_id => 2 }, { :id => 2, :fullname => "Lindsay Fünke", :organization_id => 2 }].to_json] } + stub.post("organizations/2/users") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke", :organization_id => 2 }.to_json] } + stub.put("organizations/2/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke", :organization_id => 2 }.to_json] } + stub.get("organizations/2/users/1") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke", :organization_id => 2, :active => true }.to_json] } + stub.delete("organizations/2/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke", :organization_id => 2, :active => false }.to_json] } end end spawn_model "Foo::User" do - collection_path "/organizations/:organization_id/users" + collection_path "organizations/:organization_id/users" end end # }}} describe "fetching a resource" do it "maps a single resource to a Ruby object" do # {{{ @@ -177,9 +226,60 @@ it "handle resource deletion through #destroy on an existing resource" do # {{{ @user = Foo::User.find(1, :_organization_id => 2) @user.destroy @user.active.should be_false + end # }}} + end + end + + context "making path HTTP requests" do + before do # {{{ + Her::API.setup :url => "https://example.com/api/" do |builder| + builder.use Her::Middleware::FirstLevelParseJSON + builder.use Faraday::Request::UrlEncoded + builder.adapter :test do |stub| + stub.get("/api/organizations/2/users") { |env| [200, {}, [{ :id => 1, :fullname => "Tobias Fünke", :organization_id => 2 }, { :id => 2, :fullname => "Lindsay Fünke", :organization_id => 2 }].to_json] } + stub.get("/api/organizations/2/users/1") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke", :organization_id => 2, :active => true }.to_json] } + end + end + + spawn_model "Foo::User" do + collection_path "organizations/:organization_id/users" + end + end # }}} + + describe "fetching a resource" do + it "maps a single resource to a Ruby object" do # {{{ + @user = Foo::User.find(1, :_organization_id => 2) + @user.id.should == 1 + @user.fullname.should == "Tobias Fünke" + end # }}} + end + + describe "fetching a collection" do + it "maps a collection of resources to an array of Ruby objects" do # {{{ + @users = Foo::User.all(:_organization_id => 2) + @users.length.should == 2 + @users.first.fullname.should == "Tobias Fünke" + end # }}} + end + + describe "fetching a resource with absolute path" do + it "maps a single resource to a Ruby object" do # {{{ + Foo::User.resource_path '/api/' + Foo::User.resource_path + @user = Foo::User.find(1, :_organization_id => 2) + @user.id.should == 1 + @user.fullname.should == "Tobias Fünke" + end # }}} + end + + describe "fetching a collection with absolute path" do + it "maps a collection of resources to an array of Ruby objects" do # {{{ + Foo::User.collection_path '/api/' + Foo::User.collection_path + @users = Foo::User.all(:_organization_id => 2) + @users.length.should == 2 + @users.first.fullname.should == "Tobias Fünke" end # }}} end end end