Sha256: 8e6a3f78d47f19bda60597c53f854080a9d7dfc7dee84d128e9dfd7556a1f494

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

# encoding: utf-8

require "spec_helper"

describe Restorm::Middleware::JsonApiParser do
  subject { described_class.new }

  context "with valid JSON body" do
    let(:body) { '{"data": {"type": "foo", "id": "bar", "attributes": {"baz": "qux"} }, "meta": {"api": "json api"} }' }
    let(:env) { { body: body } }

    it "parses body as json" do
      subject.on_complete(env)
      env.fetch(:body).tap do |json|
        expect(json[:data]).to eql(
          type: "foo",
          id: "bar",
          attributes: { baz: "qux" }
        )
        expect(json[:errors]).to eql([])
        expect(json[:metadata]).to eql(api: "json api")
      end
    end
  end

  context "with status code 204" do
    it "returns an empty body" do
      env = { status: 204 }
      subject.on_complete(env)
      env[:body].tap do |json|
        expect(json[:data]).to eq({})
      end
    end
  end

  context 'with status code 304' do
    it 'returns an empty body' do
      env = { :status => 304 }
      subject.on_complete(env)
      env[:body].tap do |json|
        expect(json[:data]).to eq({})
      end
    end
  end

  # context "with invalid JSON body" do
  #  let(:body) { '"foo"' }
  #  it 'ensures that invalid JSON throws an exception' do
  #    expect { subject.parse(body) }.to raise_error(Restorm::Errors::ParseError, 'Response from the API must behave like a Hash or an Array (last JSON response was "\"foo\"")')
  #  end
  # end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
restorm-1.0.0 spec/middleware/json_api_parser_spec.rb