Skip to content

Commit 79441c1

Browse files
authored
Merge pull request #13 from securenative/dev
Fix httpclient collision
2 parents 752c555 + 07ffbb0 commit 79441c1

File tree

8 files changed

+67
-22
lines changed

8 files changed

+67
-22
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ source "https://rubygems.org"
33
gemspec
44
gem "rspec"
55
gem "rake"
6-
gem "httpclient"
76
gem "parseconfig"
87
gem "simplecov", :require => false, :group => :test
8+
gem "codecov", :require => false, :group => :test
99
gem "webmock", :require => false, :group => :test
1010
gem "rails", :require => false, :group => :test
1111
gem "hanami", :require => false, :group => :test

Gemfile.lock

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
securenative (0.1.18)
4+
securenative (0.1.19)
55

66
GEM
77
remote: https://rubygems.org/
@@ -64,6 +64,11 @@ GEM
6464
addressable (2.7.0)
6565
public_suffix (>= 2.0.2, < 5.0)
6666
builder (3.2.4)
67+
codecov (0.2.6)
68+
colorize
69+
json
70+
simplecov
71+
colorize (0.8.1)
6772
concurrent-ruby (1.1.7)
6873
crack (0.4.3)
6974
safe_yaml (~> 1.0.0)
@@ -148,10 +153,10 @@ GEM
148153
http_router (0.11.2)
149154
rack (>= 1.0.0)
150155
url_mount (~> 0.2.1)
151-
httpclient (2.8.3)
152156
i18n (1.8.5)
153157
concurrent-ruby (~> 1.0)
154158
inflecto (0.0.2)
159+
json (2.3.1)
155160
loofah (2.6.0)
156161
crass (~> 1.0.2)
157162
nokogiri (>= 1.5.9)
@@ -256,8 +261,8 @@ PLATFORMS
256261

257262
DEPENDENCIES
258263
bundler (~> 2.0)
264+
codecov
259265
hanami
260-
httpclient
261266
parseconfig
262267
rails
263268
rake

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ gem 'securenative'
3636

3737
Then execute:
3838

39-
$ bundle
39+
$ bundle install
4040

4141
Or install it yourself as:
4242

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.18
1+
0.1.19

lib/event_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def send_sync(event, resource_path, retry_sending)
6464
SecureNativeLogger.debug("Attempting to send event #{event}")
6565
res = @http_client.post(resource_path, EventManager.serialize(event).to_json)
6666

67-
if res.status_code != 200
67+
if res.code != 200
6868
SecureNativeLogger.info("SecureNative failed to call endpoint #{resource_path} with event #{event}. adding back to queue")
6969
item = QueueItem.new(resource_path, EventManager.serialize(event).to_json, retry_sending)
7070
@queue.append(item)
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# frozen_string_literal: true
22

3-
require 'httpclient'
3+
require 'net/http'
4+
require 'uri'
5+
require 'json'
46

57
class SecureNativeHttpClient
68
AUTHORIZATION_HEADER = 'Authorization'
79
VERSION_HEADER = 'SN-Version'
810
USER_AGENT_HEADER = 'User-Agent'
9-
USER_AGENT_HEADER_VALUE = 'SecureNative-python'
11+
USER_AGENT_HEADER_VALUE = 'SecureNative-ruby'
1012
CONTENT_TYPE_HEADER = 'Content-Type'
1113
CONTENT_TYPE_HEADER_VALUE = 'application/json'
1214

1315
def initialize(securenative_options)
1416
@options = securenative_options
15-
@client = HTTPClient.new
1617
end
1718

1819
def _headers
@@ -25,8 +26,13 @@ def _headers
2526
end
2627

2728
def post(path, body)
28-
url = "#{@options.api_url}/#{path}"
29+
uri = URI.parse("#{@options.api_url}/#{path}")
2930
headers = _headers
30-
@client.post(url, body, headers)
31+
32+
client = Net::HTTP.new(uri.host, uri.port)
33+
request = Net::HTTP::Post.new(uri.request_uri, headers)
34+
request.body = body.to_json
35+
36+
client.request(request)
3137
end
3238
end

spec/spec_event_manager.rb

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,60 @@ def initialize
2727
event = SampleEvent.new
2828

2929
res_body = '{"data": true}'
30-
stub_request(:any, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api').to_return(body: res_body.to_json, status: 200)
30+
stub_request(:any, 'http://api.securenative-stg.com:443/collector/api/v1/some-path/to-api')
31+
.with(headers: {
32+
'Accept' => '*/*',
33+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
34+
'Authorization' => 'YOUR_API_KEY',
35+
'Content-Type' => 'application/json',
36+
'Sn-Version' => '0.1.19',
37+
'User-Agent' => 'SecureNative-ruby'
38+
}).to_return(status: 200, body: '', headers: {})
3139
event_manager = EventManager.new(options)
3240

3341
event_manager.start_event_persist
34-
data = event_manager.send_sync(event, 'some-path/to-api', false)
42+
res = event_manager.send_sync(event, 'some-path/to-api', false)
3543
event_manager.stop_event_persist
3644

37-
expect('"{\"data\": true}"').to eq(data.body.to_s)
45+
expect(res.code).to eq('200')
3846
end
3947

4048
it 'fails when send sync event status code is 401' do
4149
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
4250
event = SampleEvent.new
4351

44-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api').to_return(status: 401)
52+
stub_request(:any, 'http://api.securenative-stg.com:443/collector/api/v1/some-path/to-api')
53+
.with(headers: {
54+
'Accept' => '*/*',
55+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
56+
'Authorization' => 'YOUR_API_KEY',
57+
'Content-Type' => 'application/json',
58+
'Sn-Version' => '0.1.19',
59+
'User-Agent' => 'SecureNative-ruby'
60+
}).to_return(status: 401, body: '', headers: {})
61+
4562
event_manager = EventManager.new(options)
4663
res = event_manager.send_sync(event, 'some-path/to-api', false)
4764

48-
expect(res.status_code).to eq(401)
65+
expect(res.code).to eq('401')
4966
end
5067

5168
it 'fails when send sync event status code is 500' do
5269
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
5370
event = SampleEvent.new
5471

55-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api').to_return(status: 500)
72+
stub_request(:post, 'http://api.securenative-stg.com:443/collector/api/v1/some-path/to-api')
73+
.with(headers: {
74+
'Accept' => '*/*',
75+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
76+
'Authorization' => 'YOUR_API_KEY',
77+
'Content-Type' => 'application/json',
78+
'Sn-Version' => '0.1.19',
79+
'User-Agent' => 'SecureNative-ruby'
80+
}).to_return(status: 500, body: '', headers: {})
5681
event_manager = EventManager.new(options)
5782
res = event_manager.send_sync(event, 'some-path/to-api', false)
5883

59-
expect(res.status_code).to eq(500)
84+
expect(res.code).to eq('500')
6085
end
6186
end

spec/spec_securenative_http_client.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@
99
it 'makes a simple post call' do
1010
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
1111

12-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
13-
.with(body: { event: 'SOME_EVENT_NAME' }).to_return(status: 200)
12+
stub_request(:post, 'http://api.securenative-stg.com:443/collector/api/v1/track')
13+
.with(body: '"{\\"event\\": \\"SOME_EVENT_NAME\\"}"',
14+
headers: {
15+
'Accept': '*/*',
16+
'Accept-Encoding': 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
17+
'Authorization': 'YOUR_API_KEY',
18+
'Content-Type': 'application/json',
19+
'Sn-Version': '0.1.19',
20+
'User-Agent': 'SecureNative-ruby'
21+
})
22+
.to_return(status: 200, body: '', headers: {})
1423
client = SecureNativeHttpClient.new(options)
1524
payload = '{"event": "SOME_EVENT_NAME"}'
1625

1726
res = client.post('track', payload)
1827

19-
expect(res.status_code).to eq(200)
28+
expect(res.code).to eq('200')
2029
end
2130
end

0 commit comments

Comments
 (0)