Skip to content

Commit 1126252

Browse files
authored
Merge pull request #1 from karaggeorge/add-support-for-body-types
Add support for other Body types
2 parents 564ea24 + 3bf36bc commit 1126252

29 files changed

+293
-122
lines changed

.rubocop.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
AllCops:
2+
TargetRubyVersion: 2.3
3+
Include:
4+
- Rakefile
5+
Exclude:
6+
- Gemfile
7+
8+
Style/SymbolArray:
9+
EnforcedStyle: brackets
110
Style/LineLength:
211
Max: 180
312
Style/ClassAndModuleChildren:
413
EnforcedStyle: compact
514
Style/FileName:
615
Exclude:
716
- '**/mockserver-client.rb'
17+
- 'mockserver-client.gemspec'

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: ruby
2+
rvm:
3+
- 2.3
4+
script:
5+
- bundle exec rubocop
6+
- ./build.sh

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
# frozen_string_literal: true
12
# encoding: UTF-8
3+
24
require 'bundler/gem_tasks'
35
require 'rubocop/rake_task'
46
require 'rspec/core/rake_task'

lib/cli.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'thor'
35
require 'colorize'
46
require_relative './mockserver-client'
@@ -88,7 +90,7 @@ class MockServerCLI < Thor
8890
def retrieve
8991
execute_command do |client, _|
9092
result = options.data ? client.retrieve(read_file(options.data)) : client.retrieve
91-
puts "RESULT:\n".bold + "#{result.to_json}".green
93+
puts "RESULT:\n".bold + result.to_json.green
9294
end
9395
end
9496

lib/mockserver-client.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require_relative './mockserver/version'
35
require_relative './mockserver/mock_server_client'
46
require_relative './mockserver/proxy_client'
@@ -7,6 +9,7 @@
79
require 'json/pure'
810

911
# To fix serialization bugs. See: http://prettystatemachine.blogspot.com/2010/09/typeerrors-in-tojson-make-me-briefly.html
12+
# rubocop:disable Lint/UnifiedInteger
1013
class Fixnum
1114
def to_json(_)
1215
to_s

lib/mockserver/abstract_client.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'rest-client'
35
require 'logging_factory'
46
require_relative './model/times'
@@ -24,8 +26,8 @@ class AbstractClient
2426
attr_accessor :logger
2527

2628
def initialize(host, port)
27-
fail 'Cannot instantiate AbstractClient class. You must subclass it.' if self.class == AbstractClient
28-
fail 'Host/port must not be nil' unless host && port
29+
raise 'Cannot instantiate AbstractClient class. You must subclass it.' if self.class == AbstractClient
30+
raise 'Host/port must not be nil' unless host && port
2931
protocol = ('https' if port == 443) || 'http'
3032
@base = RestClient::Resource.new("#{protocol}://#{host}:#{port}", headers: { 'Content-Type' => 'application/json' })
3133
@logger = ::LoggingFactory::DEFAULT_FACTORY.log(self.class)
@@ -34,7 +36,7 @@ def initialize(host, port)
3436
# Clear all expectations with the given request
3537
# @param request [Request] the request to use to clear an expectation
3638
# @return [Object] the response from the clear action
37-
def clear(request)
39+
def clear(request) # rubocop:disable Metrics/AbcSize
3840
request = camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request)))
3941

4042
logger.debug("Clearing expectation with request: #{request}")
@@ -61,7 +63,7 @@ def reset
6163
# Retrieve the list of requests that have been processed by the server
6264
# @param request [Request] to filter requests
6365
# @return [Object] the list of responses processed by the server
64-
def retrieve(request = nil)
66+
def retrieve(request = nil) # rubocop:disable Metrics/AbcSize
6567
request = request ? camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request))) : {}
6668

6769
logger.debug('Retrieving request list from mockserver')
@@ -95,7 +97,7 @@ def dump_log(request = nil, java = false)
9597
# @param request [Request] to filter requests
9698
# @param times [Times] expected number of times
9799
# @return [Object] the list of responses processed by the server that match the request
98-
def verify(request, times = exactly(1))
100+
def verify(request, times = exactly(1)) # rubocop:disable Metrics/AbcSize
99101
logger.debug('Sending query for verify to mockserver')
100102
results = retrieve(request)
101103

@@ -105,7 +107,7 @@ def verify(request, times = exactly(1))
105107
is_exact = !times.unlimited
106108

107109
fulfilled = is_exact ? (num_times == results.size) : (num_times <= results.size)
108-
fail "Expected request to be present: [#{num_times}] (#{is_exact ? 'exactly' : 'at least'}). But found: [#{results.size}]" unless fulfilled
110+
raise "Expected request to be present: [#{num_times}] (#{is_exact ? 'exactly' : 'at least'}). But found: [#{results.size}]" unless fulfilled
109111
results
110112
end
111113
end

lib/mockserver/mock_server_client.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require_relative './model/expectation'
35
require_relative './abstract_client'
46
require_relative './utility_methods'
@@ -18,8 +20,8 @@ class MockServerClient < AbstractClient
1820
# Registers an expectation with the mockserver
1921
# @param expectation [Expectation] the expectation to create the request from
2022
# @return [Object] the response from the register action
21-
def register(expectation)
22-
fail 'Expectation passed in is not valid type' unless expectation.is_a?(Expectation)
23+
def register(expectation) # rubocop:disable Metrics/AbcSize
24+
raise 'Expectation passed in is not valid type' unless expectation.is_a?(Expectation)
2325
request = create_expectation_request(expectation)
2426

2527
logger.debug('Registering new expectation')
@@ -35,11 +37,10 @@ def register(expectation)
3537
# Create an expecation request to send to the expectation endpoint of
3638
# @param expectation [Expectation] the expectation to create the request from
3739
# @return [Hash] a hash representing the request to use in registering an expectation with the mock server
38-
# rubocop:disable Lint/LiteralInInterpolation
3940
def create_expectation_request(expectation)
4041
expectation_request = camelized_hash(expectation)
4142
logger.debug("Expectation JSON: #{expectation_request.to_json}")
42-
fail "You can only set either of #{[HTTP_RESPONSE, HTTP_FORWARD]}. But not both" if expectation_request[HTTP_RESPONSE] && expectation_request[HTTP_FORWARD]
43+
raise "You can only set either of #{[HTTP_RESPONSE, HTTP_FORWARD]}. But not both" if expectation_request[HTTP_RESPONSE] && expectation_request[HTTP_FORWARD]
4344
expectation_request
4445
end
4546
end

lib/mockserver/model/array_of.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
# frozen_string_literal: true
12
# encoding: UTF-8
3+
24
#
35
# The ArrayOf class stores instances of a given class only.
46
# It enforces this by intercepting the methods :<<, :[]= and :insert on the Array class
@@ -15,9 +17,9 @@ module MockServer::Model
1517

1618
# The ArrayOf class stores instances of a given class only.
1719
class ArrayOf < Array
18-
alias_method :add_element, :<<
19-
alias_method :set_element, :[]=
20-
alias_method :insert_element, :insert
20+
alias add_element <<
21+
alias set_element []=
22+
alias insert_element insert
2123

2224
# Create an array from the elements passed in
2325
def initialize(items)
@@ -28,7 +30,7 @@ def initialize(items)
2830

2931
# The class/type that this array stores
3032
def child_class
31-
fail 'Subclass should override method :child_class'
33+
raise 'Subclass should override method :child_class'
3234
end
3335

3436
# Add the item to the array
@@ -75,7 +77,7 @@ def convert_to_child_class(item)
7577
if item && item.class != child_class
7678
begin
7779
item = child_class.new(item)
78-
rescue Exception => e # rubocop:disable Lint/RescueException
80+
rescue StandardError => e
7981
raise "Failed to convert element: #{item} to required type #{child_class}. Error: #{e.message}"
8082
end
8183
end

lib/mockserver/model/body.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'hashie'
35
require_relative './parameter'
46
require_relative './enum'
@@ -11,7 +13,7 @@ module MockServer::Model
1113
# An enum for body type
1214
class BodyType < SymbolizedEnum
1315
def allowed_values
14-
[:STRING, :REGEX, :XPATH, :PARAMETERS, :BINARY]
16+
[:STRING, :REGEX, :XPATH, :PARAMETERS, :BINARY, :JSON, :JSON_SCHEMA, :XML_SCHEMA]
1517
end
1618
end
1719

@@ -41,6 +43,18 @@ def exact(value)
4143
Body.new(type: :STRING, value: value)
4244
end
4345

46+
def json(value)
47+
Body.new(type: :JSON, value: value)
48+
end
49+
50+
def json_schema(value)
51+
Body.new(type: :JSON_SCHEMA, value: value)
52+
end
53+
54+
def xml_schema(value)
55+
Body.new(type: :XML_SCHEMA, value: value)
56+
end
57+
4458
def regex(value)
4559
Body.new(type: :REGEX, value: value)
4660
end

lib/mockserver/model/cookie.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'hashie'
35
require_relative './array_of'
46

lib/mockserver/model/delay.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require_relative './enum'
35

46
#

lib/mockserver/model/enum.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
#
35
# A class to model a Java-like Enum.
46
# To create an Enum extend this class and override :allowed_values method with allowed enum values.
@@ -13,13 +15,13 @@ class Enum
1315
# @raise [Exception] if the supplied value is not valid for this enum
1416
def initialize(supplied_value)
1517
supplied_value = pre_process_value(supplied_value)
16-
fail "Supplied value: #{supplied_value} is not valid. Allowed values are: #{allowed_values.inspect}" unless allowed_values.include?(supplied_value)
18+
raise "Supplied value: #{supplied_value} is not valid. Allowed values are: #{allowed_values.inspect}" unless allowed_values.include?(supplied_value)
1719
@value = supplied_value
1820
end
1921

2022
# @return [Array] a list of values allowed by this enum
2123
def allowed_values
22-
fail 'Override :allowed_values in Enum class'
24+
raise 'Override :allowed_values in Enum class'
2325
end
2426

2527
# A pre-process hook for a value before it is stored

lib/mockserver/model/expectation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require_relative './array_of'
35
require_relative './request'
46
require_relative './response'

lib/mockserver/model/forward.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'hashie'
35
require_relative './enum'
46

@@ -36,6 +38,6 @@ def forward(&_)
3638
obj
3739
end
3840

39-
alias_method :http_forward, :forward
41+
alias :http_forward forward
4042
end
4143
end

lib/mockserver/model/header.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'hashie'
35
require_relative './array_of'
46

lib/mockserver/model/parameter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'hashie'
35
require_relative './array_of'
46

lib/mockserver/model/request.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'hashie'
35
require_relative './parameter'
46
require_relative './header'
@@ -24,8 +26,6 @@ class Request < Hashie::Trash
2426
include Hashie::Extensions::IgnoreUndeclared
2527
include Hashie::Extensions::Coercion
2628

27-
ALLOWED_METHODS = [:GET, :POST, :PUT, :DELETE, :PATCH]
28-
2929
property :method, required: true, default: :GET
3030
property :path, required: true, default: ''
3131
property :query_string_parameters, default: Parameters.new([])
@@ -77,14 +77,14 @@ def request_from_json(payload)
7777
body = payload['body']
7878

7979
if body && body.is_a?(String)
80-
payload.merge!('body' => { 'type' => :STRING, 'value' => body })
80+
payload['body'] = { 'type' => :STRING, 'value' => body }
8181
end
8282

8383
request = Request.new(symbolize_keys(payload))
8484
yield request if block_given?
8585
request
8686
end
8787

88-
alias_method :http_request, :request
88+
alias http_request request
8989
end
9090
end

lib/mockserver/model/response.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require_relative './body'
35
require_relative './delay'
46
require_relative './header'
@@ -40,6 +42,6 @@ def decode(string)
4042
Base64.decode64(string)
4143
end
4244

43-
alias_method :http_response, :response
45+
alias http_response response
4446
end
4547
end

lib/mockserver/model/times.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'hashie'
35
require_relative './enum'
46

lib/mockserver/proxy_client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require_relative './abstract_client'
35

46
#

lib/mockserver/utility_methods.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'active_support/inflector'
35
require 'json'
46

@@ -12,8 +14,7 @@ module MockServer::UtilityMethods
1214
# - camelize the keys of the hash
1315
# @param obj [Object] an object which will be used to create the hash. Must support :to_hash method
1416
# @return [Hash] the transformed hash
15-
# rubocop:disable Style/MethodLength
16-
# rubocop:disable Style/CyclomaticComplexity
17+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
1718
def camelized_hash(obj)
1819
obj = obj && obj.respond_to?(:to_hash) ? obj.to_hash : obj
1920

lib/mockserver/version.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
# Version for this gem
35
module MockServer
46
VERSION = '1.0.8.pre'

0 commit comments

Comments
 (0)