Skip to content

Commit

Permalink
Merge 971bff4 into 5421b92
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevoets committed Jul 31, 2017
2 parents 5421b92 + 971bff4 commit 0f61efa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/flexirest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ def prepare_request_body(params = nil)
@body = ""
else
headers["Content-Type"] ||= "application/vnd.api+json"
@body = JsonAPIProxy::Request::Params.create(params || @post_params || {}, @object).to_json
@body = JsonAPIProxy::Request::Params.create(
params || @post_params || {},
object_is_class? ? @object.new : @object
).to_json
end

headers["Accept"] ||= "application/vnd.api+json"
Expand Down
39 changes: 32 additions & 7 deletions spec/lib/json_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,36 @@ class ArticleAlias < Flexirest::Base
expect_any_instance_of(Flexirest::Connection).to receive(:post) { |_, _, _, options|
expect(options[:headers]).to include('Content-Type' => 'application/vnd.api+json')
expect(options[:headers]).to include('Accept' => 'application/vnd.api+json')
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{}', response_headers:{})))
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body: '{}', response_headers: {})))
JsonAPIExample::Article.new.create
end

it 'should be able to call #.create on class' do
expect_any_instance_of(Flexirest::Connection).to receive(:post) { |_, path, data|
hash = MultiJson.load(data)
expect(path).to eq('/articles')
expect(hash['data']).to_not be_nil
expect(hash['data']['id']).to be_nil
expect(hash['data']['type']).to eq('articles')
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body: '{}', response_headers: {})))
JsonAPIExample::Article.create
end

it 'should be able to call #.create with params on class' do
expect_any_instance_of(Flexirest::Connection).to receive(:post) { |_, path, data|
hash = MultiJson.load(data)
expect(path).to eq('/articles')
expect(hash['data']).to_not be_nil
expect(hash['data']['id']).to be_nil
expect(hash['data']['type']).to eq('articles')
expect(hash['data']['relationships']['author']['data']['type']).to eq('authors')
expect(hash['data']['relationships']['tags']['data'].first['type']).to eq('tags')
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body: '{}', response_headers: {})))
author = JsonAPIExample::Author.new
tag = JsonAPIExample::Tag.new
JsonAPIExample::Article.create(item: 'item one', author: author, tags: [tag])
end

it 'should perform a post request in proper json api format' do
expect_any_instance_of(Flexirest::Connection).to receive(:post) { |_, path, data|
hash = MultiJson.load(data)
Expand All @@ -334,7 +360,7 @@ class ArticleAlias < Flexirest::Base
expect(hash['data']['type']).to eq('articles')
expect(hash['data']['relationships']['author']['data']['type']).to eq('authors')
expect(hash['data']['relationships']['tags']['data'].first['type']).to eq('tags')
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{}', response_headers:{})))
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body: '{}', response_headers: {})))
author = JsonAPIExample::Author.new
tag = JsonAPIExample::Tag.new
article = JsonAPIExample::Article.new
Expand All @@ -350,7 +376,7 @@ class ArticleAlias < Flexirest::Base
article = JsonAPIExample::Article.new
article.item = 'item one'
article.tags = [tag, author]
expect(lambda { article.create }).to raise_error(Exception)
expect(-> { article.create }).to raise_error(Exception)
end

it 'should perform a patch request in proper json api format' do
Expand All @@ -360,7 +386,7 @@ class ArticleAlias < Flexirest::Base
expect(hash['data']).to_not be_nil
expect(hash['data']['id']).to_not be_nil
expect(hash['data']['type']).to_not be_nil
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{}', response_headers:{})))
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body: '{}', response_headers: {})))
article = JsonAPIExample::Article.find(1)
article.item = 'item one'
article.update
Expand All @@ -370,7 +396,7 @@ class ArticleAlias < Flexirest::Base
expect_any_instance_of(Flexirest::Connection).to receive(:delete) { |_, path, data|
expect(path).to eq('/articles/1')
expect(data).to eq('')
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{}', response_headers:{})))
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body: '{}', response_headers: {})))
JsonAPIExample::Article.find(1).delete
end

Expand All @@ -379,14 +405,13 @@ class ArticleAlias < Flexirest::Base
hash = MultiJson.load(data)
expect(hash['data']['type']).to eq(JsonAPIExample::ArticleAlias.alias_type.to_s)
expect(hash['data']['relationships']['author']['data']['type']).to eq(JsonAPIExample::AuthorAlias.alias_type.to_s)
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{}', response_headers:{})))
}.and_return(::FaradayResponseMock.new(OpenStruct.new(body: '{}', response_headers: {})))
author = JsonAPIExample::AuthorAlias.new
author.id = 1
article = JsonAPIExample::ArticleAlias.find(1)
article.item = 'item one'
article.author = author
article.update
end

end
end

0 comments on commit 0f61efa

Please sign in to comment.