Sha256: f1fd30334312ee8cce6d58e938514c65f92c8a471060aa74c1b4fbf1a347cbfe

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'spec_helper'
require 'rack/test'
require 'grape-entity'
require 'grape/msgpack'

class MockModel
  def initialize(name, age)
    @name = name
    @age = age
  end
  attr_reader :name, :age
end

class MockEntity < Grape::Entity
  expose :name
end

class MockAPI < Grape::API
  rescue_from :all

  default_format :msgpack

  get :test do
    [1, 2, 3]
  end

  get :model do
    present MockModel.new('test_user', 21), with: MockEntity
  end

  get :exception do
    raise StandardError, 'an error occurred'
  end
end

describe MockAPI do
  include Rack::Test::Methods

  def app
    MockAPI
  end

  describe 'GET /test' do
    subject(:response) do
      get '/test'
      last_response
    end

    it { expect(response.status).to eq(200) }
    it { expect(response.headers['Content-Type']).to eq('application/x-msgpack') }
    it { expect(MessagePack.unpack(response.body)).to eq([1, 2, 3]) }
  end

  describe 'GET /model' do
    subject(:response) do
      get '/model'
      last_response
    end

    it { expect(response.status).to eq(200) }
    it { expect(response.headers['Content-Type']).to eq('application/x-msgpack') }
    it { expect(MessagePack.unpack(response.body)).to eq('name' => 'test_user') }
  end

  describe 'GET /exception' do
    subject(:response) do
      get '/exception'
      last_response
    end

    it { expect(response.status).to eq(500) }
    it { expect(response.headers['Content-Type']).to eq('application/x-msgpack') }
    it { expect(MessagePack.unpack(response.body)).to eq('error' => 'an error occurred') }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-msgpack-0.1.1 spec/grape/msgpack_spec.rb