Last active
August 29, 2015 13:57
-
-
Save ramarnat/9520306 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'grape' | |
| require 'rspec' | |
| require 'rack/test' | |
| module Example | |
| module TransactionTestHelper | |
| include Rack::Test::Methods | |
| def app | |
| Example::Root | |
| end | |
| def transaction | |
| { | |
| 'transactions' => [ | |
| { | |
| 'order_number' => 1, | |
| 'product' => 'widget', | |
| 'customer' => 'John', | |
| 'quantity' => 1 | |
| }, | |
| { | |
| 'order_number' => 2, | |
| 'product' => 'widget', | |
| 'customer' => 'Mary' | |
| } | |
| ] | |
| } | |
| end | |
| def put_transaction_does_not_work | |
| put "/v1/simple", | |
| MultiJson.dump(transaction), | |
| "CONTENT_TYPE" => "application/json" | |
| pp JSON.parse(last_response.body) | |
| JSON.parse(last_response.body) | |
| end | |
| def put_transaction_works | |
| put "/v1/simple2", | |
| MultiJson.dump(transaction), | |
| "CONTENT_TYPE" => "application/json" | |
| pp JSON.parse(last_response.body) | |
| JSON.parse(last_response.body) | |
| end | |
| end | |
| class APIv1 < Grape::API | |
| format :json | |
| version 'v1', :using => :path | |
| params do | |
| requires :transactions, type: Array, desc: "An array of transactions" | |
| group :transactions do | |
| requires :order_number, type: Integer, desc: "blah" | |
| requires :product, type: String, desc: "blah" | |
| optional :customer, type: String, desc: "blah" | |
| optional :quantity, type: Integer, desc: "blah", default: 1 | |
| end | |
| end | |
| put '/simple' do | |
| a = [] | |
| params[:transactions].each do |trans| | |
| a << trans | |
| end | |
| return a | |
| end | |
| params do | |
| requires :transactions, type: Array, desc: "An array of transactions" | |
| group :transactions do | |
| requires :order_number, type: Integer, desc: "blah" | |
| requires :product, type: String, desc: "blah" | |
| optional :customer, type: String, desc: "blah" | |
| optional :quantity, type: Integer, desc: "blah" | |
| end | |
| end | |
| put '/simple2' do | |
| a = [] | |
| params[:transactions].each do |trans| | |
| a << trans | |
| end | |
| return a | |
| end | |
| end | |
| class Root < Grape::API | |
| format :json | |
| mount Example::APIv1 | |
| end | |
| end | |
| RSpec.configure do |config| | |
| config.include Example::TransactionTestHelper | |
| end | |
| module Example | |
| describe APIv1 do | |
| it "PUTs multiple simple order transactions - works" do | |
| put_transaction_works | |
| expect(last_response.status).to eql 200 | |
| expect(JSON.parse(last_response.body).length).to eql 2 | |
| end | |
| it "PUTs multiple simple order transactions - fails" do | |
| put_transaction_does_not_work | |
| expect(last_response.status).to eql 200 | |
| expect(JSON.parse(last_response.body).length).to eql 2 | |
| end | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment