Skip to content

Commit

Permalink
fix bugs and add test to ensure error message is updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
xinghengwang committed Nov 13, 2024
1 parent 242d038 commit fbcc17a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/moesif_api/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class Configuration

@user_agent = "moesifapi-ruby/2.1.1"

@retry_ccount = 5
@retry_count = 5

# create the getters and setters
class << self
attr_accessor :base_uri
attr_accessor :application_id
attr_accessor :version
attr_accessor :user_agent
attr_accessor :retry_count
end
end
end
3 changes: 2 additions & 1 deletion lib/moesif_api/controllers/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ def build_request_headers
def validate_response(context)
return if context.response.status_code.between?(200, 208) # [200,208] = HTTP OK
res = context.response;

if res.nil?
raise APIException.new 'No Http Response, maybe timeout', context
else
raise APIException.new 'HTTP Response Not OK ' + res.status_code + ' ' + res.body, context
raise APIException.new 'HTTP Response Not OK ' + res.status_code.to_s + ' ' + res.raw_body.to_s, context
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/moesif_api/moesif_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def health
end

# Initializer with authentication and configuration parameters
def initialize(application_id, user_agent=nil, retry_ccount=5)
def initialize(application_id, user_agent=nil, retry_count=5)
Configuration.application_id = application_id
Configuration.retry_ccount = retry_count
Configuration.retry_count = retry_count
unless user_agent.nil?
Configuration.user_agent = user_agent
end
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/controller_test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'json'
require 'test/unit'
require 'moesif_api.rb'
require_relative '../../lib/moesif_api.rb'
require_relative '../test_helper.rb'
require_relative '../http_response_catcher.rb'

Expand All @@ -15,7 +15,7 @@ class << self

# Called only once for a test class before any test has executed.
def self.startup
@@api_client = MoesifAPIClient.new("your application id")
@@api_client = MoesifAPIClient.new("Your Moesif Application Id")
@@request_timeout = 30
@@assert_precision = 0.01
end
Expand Down
77 changes: 77 additions & 0 deletions test/controllers/test_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,83 @@ def test_add_event()
assert_equal(@response_catcher.response.status_code, 201)
end

def test_add_bad_event()
# Parameters for the API call

req_headers = JSON.parse('{'\
'"Host": "api.acmeinc.com",'\
'"Accept": "*/*",'\
'"Connection": "Keep-Alive",'\
'"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 5.0.2; C6906 Build/14.5.A.0.242)",'\
'"Content-Type": "application/json",'\
'"Content-Length": "126",'\
'"Accept-Encoding": "gzip"'\
'}')

req_body = JSON.parse( '{'\
'"items": ['\
'{'\
'"type": 1,'\
'"id": "fwfrf"'\
'},'\
'{'\
'"type": 2,'\
'"id": "d43d3f"'\
'}'\
']'\
'}')

rsp_headers = JSON.parse('{'\
'"Date": "Tue, 6 Jan 2019 23:46:49 GMT",'\
'"Vary": "Accept-Encoding",'\
'"Pragma": "no-cache",'\
'"Expires": "-1",'\
'"Content-Type": "application/json; charset=utf-8",'\
'"Cache-Control": "no-cache"'\
'}')

rsp_body = JSON.parse('{'\
'"Error": "InvalidArgumentException",'\
'"Message": "Missing field field_a"'\
'}')

metadata = JSON.parse('{'\
'"foo": "rubytestmeta",'\
'"bar": "rubytestmedat2"'\
'}')

event_req = EventRequestModel.new()
event_req.time = Time.now.utc.iso8601
event_req.uri = nil
event_req.verb = "POST"
event_req.api_version = "1.1.0"
event_req.ip_address = "61.48.220.123"
event_req.headers = req_headers
event_req.body = req_body

event_rsp = EventResponseModel.new()
event_rsp.time = (Time.now.utc + 2).iso8601
event_rsp.status = 400
event_rsp.headers = rsp_headers
event_rsp.body = rsp_body

event_model = EventModel.new()
event_model.request = event_req
event_model.response = event_rsp
event_model.user_id = "my_user_id"
event_model.company_id = "my_company_id"
event_model.metadata = metadata
event_model.session_token = "random"

# Perform the API call through the SDK function

error = assert_raises(APIException) do
self.class.controller.create_event(event_model)
end
puts 'error: ' + error.message;
assert_match(/422|400/, error.message, "The error message should contain '422' or '400'")
end

# Add Batched Events via Ingestion API
def test_add_batched_events()
# Parameters for the API call
Expand Down

0 comments on commit fbcc17a

Please sign in to comment.