Skip to content

Commit

Permalink
Merge pull request #38 from dsalahutdinov/feature/more_rspec_matchers
Browse files Browse the repository at this point in the history
feature: add response chunks rspec matchers
  • Loading branch information
dsalahutdinov committed Nov 12, 2018
2 parents c33ca1f + e753db2 commit 6506691
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 63 deletions.
2 changes: 1 addition & 1 deletion app/controllers/logux_controller.rb
Expand Up @@ -4,8 +4,8 @@ class LoguxController < ActionController::Base
include ActionController::Live

def create
Logux.verify_request_meta_data(meta_params)
logux_stream.write('[')
Logux.verify_request_meta_data(meta_params)
Logux.process_batch(stream: logux_stream, batch: command_params)
rescue => ex
handle_processing_errors(ex)
Expand Down
36 changes: 34 additions & 2 deletions lib/logux/test/helpers.rb
Expand Up @@ -19,8 +19,40 @@ def send_to_logux(*commands)
Logux::Test::Matchers::SendToLogux.new(*commands)
end

def be_approved(*meta)
Logux::Test::Matchers::BeApproved.new(*meta)
def be_approved(meta = nil)
Logux::Test::Matchers::ResponseChunks.new(
meta: meta, includes: ['approved'], excludes: %w[forbidden error]
)
end

def be_processed(meta = nil)
Logux::Test::Matchers::ResponseChunks.new(
meta: meta, includes: ['processed'], excludes: %w[forbidden error]
)
end

def be_forbidden(meta = nil)
Logux::Test::Matchers::ResponseChunks.new(
meta: meta, includes: ['forbidden']
)
end

def be_errored(meta = nil)
Logux::Test::Matchers::ResponseChunks.new(
meta: meta, includes: ['error']
)
end

def be_authenticated(meta = nil)
Logux::Test::Matchers::ResponseChunks.new(
meta: meta, includes: ['authenticated']
)
end

def be_unauthorized(meta = nil)
Logux::Test::Matchers::ResponseChunks.new(
meta: meta, includes: ['unauthorized']
)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/logux/test/matchers.rb
Expand Up @@ -4,7 +4,7 @@ module Logux
module Test
module Matchers
autoload :SendToLogux, 'logux/test/matchers/send_to_logux'
autoload :BeApproved, 'logux/test/matchers/be_approved'
autoload :ResponseChunks, 'logux/test/matchers/response_chunks'
end
end
end
37 changes: 0 additions & 37 deletions lib/logux/test/matchers/be_approved.rb

This file was deleted.

48 changes: 48 additions & 0 deletions lib/logux/test/matchers/response_chunks.rb
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require_relative 'base'

module Logux
module Test
module Matchers
class ResponseChunks < Base
attr_reader :includes, :excludes, :meta

def initialize(meta:, includes:, excludes: [])
@meta = meta
@includes = includes
@excludes = excludes
end

def matches?(actual)
@actual = JSON.parse(actual.body)

match_includes? && match_excludes?
end

def failure_message
data = "expected that #{pretty(@actual)} to has " \
"#{includes.join(', ')} chunks"
!excludes.empty? && data += " and doesn't" \
" has #{excludes.join(', ')} chunks"
data
end

private

def match_includes?
@actual.any? do |command|
command.first.in?(includes) &&
(meta.nil? || (meta.present? && command[1] == meta))
end
end

def match_excludes?
@actual.empty? || @actual.none? do |command|
command.first.in?(excludes)
end
end
end
end
end
end
13 changes: 3 additions & 10 deletions spec/requests/request_logux_server_spec.rb
Expand Up @@ -32,9 +32,7 @@
end

it 'returns processed chunk' do
expect(response.stream).to have_chunk(
['processed', '219_856_768 clientid 0']
)
expect(response).to be_processed('219_856_768 clientid 0')
end
end

Expand All @@ -50,12 +48,9 @@
{ time: Time.now.to_i, id: '219_856_768 clientid 0', userId: 1 }]
] }
end
let(:logux_response) do
['forbidden', '219_856_768 clientid 0']
end

it 'returns correct body' do
expect(response.stream).to have_chunk(logux_response)
expect(response).to be_forbidden
end
end

Expand All @@ -65,9 +60,7 @@
let(:password) { '12345' }

it 'returns error' do
expect(response.stream).to start_from_chunk(
['unauthorized', 'Incorrect password']
)
expect(response).to be_unauthorized
end
end

Expand Down
8 changes: 2 additions & 6 deletions spec/requests/request_without_action_spec.rb
Expand Up @@ -40,9 +40,7 @@

it 'returns processed' do
request_logux
expect(response.stream).to have_chunk(
['processed', '219_856_768 clientid 0']
)
expect(response).to be_processed('219_856_768 clientid 0')
end
end
end
Expand All @@ -54,9 +52,7 @@

it 'returns processed' do
request_logux
expect(response.stream).to have_chunk(
['processed', '219_856_768 clientid 0']
)
expect(response).to be_processed('219_856_768 clientid 0')
end
end
end
8 changes: 2 additions & 6 deletions spec/requests/request_without_subscribe_spec.rb
Expand Up @@ -40,9 +40,7 @@

it 'returns processed' do
request_logux
expect(response.stream).to have_chunk(
['processed', '219_856_768 clientid 0']
)
expect(response).to be_processed('219_856_768 clientid 0')
end
end
end
Expand All @@ -54,9 +52,7 @@

it 'returns processed' do
request_logux
expect(response.stream).to have_chunk(
['processed', '219_856_768 clientid 0']
)
expect(response).to be_processed('219_856_768 clientid 0')
end
end
end

0 comments on commit 6506691

Please sign in to comment.