class NEC::MockServer::Application
Class represent trivial application to handle requests by defined router.
Constants
- MATCHERS
- REGISTER_AWID_RESOURCE
Attributes
Public Class Methods
The method create new instance of Application
@param [MockServer::Router] router @param [Hash] opts @option opts [Regexp] :matcher Define how to split parts of all request URL. Default is to #MATCHERS @option opts [Boolean] :only_registered_awid If set to true all requests that wasn't call on registered awid (by sys/iniAppWorkspace) will be rejected @option opts [Hash{TID => Array<AWID>}] :awids @option opts [Hash{TID => Array<ASIDS>}] :asids
# File lib/nec_mock_server/mock_server.rb, line 60 def initialize(router, opts = {}) @router = router @only_registered_awid = opts[:only_registered_awid] @url_parts_matcher = opts.fetch(:matcher) {MATCHERS[:uaf]} @awids = opts.fetch(:awids) {{}} @asids = opts.fetch(:asids) {{}} end
Public Instance Methods
The method catch request and process it
@param [Hash] env
# File lib/nec_mock_server/mock_server.rb, line 72 def call(env) request = Rack::Request.new(env) identity = get_identity(env['HTTP_AUTHORIZATION']) serve_request(request, identity) end
The method helps to get identity information from Bearer token
@param [String] authorization
# File lib/nec_mock_server/mock_server.rb, line 82 def get_identity(authorization) if authorization begin token = authorization.split('Bearer ')[1] parts = token.split('.') JSON.parse(Base64.decode64(parts[1]), symbolize_names: true) rescue => e warn e.message end end end
The method handle received request.
@param [Rack::Request] request
# File lib/nec_mock_server/mock_server.rb, line 98 def serve_request(request, identity = nil) parts = get_url_parts(request.url) begin io = request.body data = io.read ensure io.close if io && io.respond_to?(:close) && io.respond_to?(:closed?) && !io.closed? end if parts[:resource] && parts[:resource] == REGISTER_AWID_RESOURCE process_sys_init_app_workspace(parts, data) end if @only_registered_awid return @router.no_registered_awid(parts[:tid], parts[:awid]) unless is_registered_resource?(parts[:tid], parts[:awid]) end @router.route(parts, data, identity) end
Private Instance Methods
The method helps to parse request url
# File lib/nec_mock_server/mock_server.rb, line 147 def get_url_parts(path) result = path.match(@url_parts_matcher) result ? result.names.map {|name| name.to_sym}.zip(result.captures).to_h : {resource: ''} end
# File lib/nec_mock_server/mock_server.rb, line 125 def is_registered_asid?(tid, asid) @awids[tid] && !(asids[tid] & [asid]).empty? end
# File lib/nec_mock_server/mock_server.rb, line 121 def is_registered_awid?(tid, awid) @awids[tid] && !(@awids[tid] & [awid]).empty? end
# File lib/nec_mock_server/mock_server.rb, line 129 def is_registered_resource?(tid, awid) is_registered_awid?(tid, awid) || is_registered_asid?(tid, awid) end
# File lib/nec_mock_server/mock_server.rb, line 133 def process_sys_init_app_workspace(parts, data) hash = data.is_a?(Hash) ? data : JSON.parse(data.to_s, symbolize_names: true) @awids[parts[:tid]] ||= [] @awids[parts[:tid]] << hash[:awid] if (@awids[parts[:tid]] & [hash[:awid]]).empty? @asids[parts[:tid]] ||= [] @asids[parts[:tid]] << parts[:awid] if (@awids[parts[:tid]] & [parts[:awid]]).empty? nil end