Skip to content

Commit

Permalink
sending format in query or body not headers
Browse files Browse the repository at this point in the history
  • Loading branch information
jwagener committed Feb 22, 2011
1 parent 05016d5 commit e995aae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
3 changes: 1 addition & 2 deletions lib/soundcloud.rb
Expand Up @@ -15,7 +15,6 @@ def self.message(response)
end

include HTTMultiParty
headers 'Accept' => 'application/json'

# TODO fix when api is ready for client_id
CLIENT_ID_PARAM_NAME = :consumer_key
Expand Down Expand Up @@ -132,8 +131,8 @@ def construct_query_arguments(path_or_uri, options={}, body_or_query=:query)

scheme = use_ssl? ? 'https' : 'http'
options = options.dup
#options.delete(:body) if options[:body] == {}
options[body_or_query] ||= {}
options[body_or_query][:format] = "json"
if access_token
options[body_or_query][:oauth_token] = access_token
else
Expand Down
22 changes: 11 additions & 11 deletions spec/soundcloud_spec.rb
Expand Up @@ -19,33 +19,33 @@
[:get, :delete, :head].each do |method|
describe "##{method}" do
it "should accept urls as path and rewrite them" do
Soundcloud.should_receive(method).with('http://api.soundcloud.com/tracks/123', {:query => {:consumer_key => 'client'}})
Soundcloud.should_receive(method).with('http://api.soundcloud.com/tracks/123', {:query => {:format => "json", :consumer_key => 'client'}})
subject.send(method, 'http://api.soundcloud.com/tracks/123')
end

it "should preserve query string in path" do
FakeWeb.register_uri(method, "http://api.soundcloud.com/tracks?consumer_key=client&created_with_app_id=124", :body => "[{'title': 'bla'}]", :content_type => "application/json")
FakeWeb.register_uri(method, "http://api.soundcloud.com/tracks?consumer_key=client&created_with_app_id=124&format=json", :body => "[{'title': 'bla'}]", :content_type => "application/json")
subject.send(method, '/tracks?created_with_app_id=124').should be_an_instance_of Soundcloud::ArrayResponseWrapper
end

it "should pass the client_id as consumer_key (LEGACY) to .#{method}" do
# TODO fix when api is ready for client_id
Soundcloud.should_receive(method).with('http://api.soundcloud.com/tracks', {:query => {:consumer_key => 'client', :limit => 2}})
Soundcloud.should_receive(method).with('http://api.soundcloud.com/tracks', {:query => {:consumer_key => 'client', :limit => 2, :format => "json"}})
subject.send(method, '/tracks', :limit => 2)
end

it "should wrap the response object in a Response" do
FakeWeb.register_uri(method, "http://api.soundcloud.com/tracks/123?consumer_key=client", :body => "{'title': 'bla'}", :content_type => "application/json")
FakeWeb.register_uri(method, "http://api.soundcloud.com/tracks/123?format=json&consumer_key=client", :body => "{'title': 'bla'}", :content_type => "application/json")
subject.send(method, '/tracks/123').should be_an_instance_of Soundcloud::HashResponseWrapper
end

it "should wrap the response array in an array of ResponseMash" do
FakeWeb.register_uri(method, "http://api.soundcloud.com/tracks?consumer_key=client", :body => "[{'title': 'bla'}]", :content_type => "application/json")
FakeWeb.register_uri(method, "http://api.soundcloud.com/tracks?format=json&consumer_key=client", :body => "[{'title': 'bla'}]", :content_type => "application/json")
subject.send(method, '/tracks').should be_an_instance_of Soundcloud::ArrayResponseWrapper
end

it "should raise an error if request not successful" do
FakeWeb.register_uri(method, "http://api.soundcloud.com/tracks?consumer_key=client", :status => ["402", "Payment required"], :body => "{'error': 'you need to pay'}")
FakeWeb.register_uri(method, "http://api.soundcloud.com/tracks?format=json&consumer_key=client", :status => ["402", "Payment required"], :body => "{'error': 'you need to pay'}")
lambda do
subject.send(method, '/tracks')
end.should raise_error Soundcloud::ResponseError
Expand All @@ -55,7 +55,7 @@
[:post, :put].each do |method|
describe "##{method}" do
it "should accept urls as path and rewrite them" do
Soundcloud.should_receive(method).with('http://api.soundcloud.com/tracks/123', {:body => {:consumer_key => 'client'}})
Soundcloud.should_receive(method).with('http://api.soundcloud.com/tracks/123', {:body => {:format => "json", :consumer_key => 'client'}})
subject.send(method, 'http://api.soundcloud.com/tracks/123')
end

Expand All @@ -66,7 +66,7 @@

it "should pass the client_id as consumer_key (LEGACY) to .#{method}" do
# TODO fix when api is ready for client_id
Soundcloud.should_receive(method).with('http://api.soundcloud.com/tracks', {:body => {:limit => 2, :consumer_key => 'client'}})
Soundcloud.should_receive(method).with('http://api.soundcloud.com/tracks', {:body => {:limit => 2, :format => "json", :consumer_key => 'client'}})
subject.send(method, '/tracks', :limit => 2)
end

Expand Down Expand Up @@ -232,19 +232,19 @@
[:get, :head, :delete].each do |method|
describe "##{method}" do
it "should pass the oauth_token parameter when doing a request" do
Soundcloud.should_receive(method).with('https://api.soundcloud.com/tracks', {:query => {:oauth_token => 'ac'}})
Soundcloud.should_receive(method).with('https://api.soundcloud.com/tracks', {:query => {:format => "json", :oauth_token => 'ac'}})
subject.send(method, '/tracks')
end

it "should try to refresh the token if it is expired and retry" do
FakeWeb.register_uri(method, "https://api.soundcloud.com/tracks/1?oauth_token=ac", :status => ['401', "Unauthorized"], :body => "{'error': 'invalid_grant'}", :content_type => "application/json")
FakeWeb.register_uri(method, "https://api.soundcloud.com/tracks/1?format=json&oauth_token=ac", :status => ['401', "Unauthorized"], :body => "{'error': 'invalid_grant'}", :content_type => "application/json")
FakeWeb.register_uri(:post,
"https://api.soundcloud.com/oauth2/token?grant_type=refresh_token&refresh_token=ce&client_id=client&client_secret=sect",
:body => '{"access_token": "new_access_token", "expires_in": 3600, "scope": null, "refresh_token": "04u7h-r3fr35h-70k3n"}',
:content_type => "application/json"
)

FakeWeb.register_uri(method, "https://api.soundcloud.com/tracks/1?oauth_token=new_access_token", :body => "{'title': 'test'}", :content_type => "application/json")
FakeWeb.register_uri(method, "https://api.soundcloud.com/tracks/1?format=json&oauth_token=new_access_token", :body => "{'title': 'test'}", :content_type => "application/json")

lambda do
response = subject.send(method, '/tracks/1')
Expand Down

0 comments on commit e995aae

Please sign in to comment.