tshield 0.0.1.1 → 0.1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1931f4eb396f51ee1d67346b6b777d8b849bc08c
4
- data.tar.gz: f6dc3dd79275e3d70e7825b45992b997789c48cb
3
+ metadata.gz: 5a7d294c42976d89c07cca825390e6e844e3e56c
4
+ data.tar.gz: d87640effc137257bde62a4c72c818b71d757833
5
5
  SHA512:
6
- metadata.gz: 0e9a71e00555c0b8ba4a05c2c6284d0adf74b4cae34b98a0018430e8f5327be7138b550ba9825874f089d0ca37c4235b4fce6a59f063f15f8a8a98b4ff622edb
7
- data.tar.gz: cd4e178f54f946aad64fb6dd50e9b9d42223c2ec41aef610cff97609c85812558c1d1f5402232c656bc2e079373c267e76a046582b643fcae27fbb998d651276
6
+ metadata.gz: 30a24c64d4d1ac312b2243e953790a62f48ac71e723ba498d12805be3f33b9f9aac3045c7d1016fb4b40db6c9fe3f67daa7d5cd8d209b14cc388f0e7ffc34cf5
7
+ data.tar.gz: 18c76dbd343735d4dfeb47a0068cdc3580448c0d07dc157d8cd55fa269020d93fac91da6ff062311def15710daf02021db6afcb15da7e794466b36df74627ed4
@@ -14,12 +14,16 @@ module TShield
14
14
  end
15
15
 
16
16
  def get_domain_for(path)
17
- domains.each do |url, paths|
18
- return url if paths.include?(path)
17
+ domains.each do |url, config|
18
+ config['paths'].each { |p| return url if path =~ Regexp.new(p) }
19
19
  end
20
20
  nil
21
21
  end
22
22
 
23
+ def get_headers(domain)
24
+ domains[domain]['headers'] || {}
25
+ end
26
+
23
27
  private
24
28
  def self.load_configuration
25
29
  config_path = File.join('config', 'tshield.yml')
@@ -21,7 +21,11 @@ module TShield
21
21
  end
22
22
 
23
23
  def request
24
- @url = "#{@configuration.get_domain_for(@path)}/#{@path}"
24
+ if not (@options[:raw_query].nil? or @options[:raw_query].empty?)
25
+ @path = "#{@path}?#{@options[:raw_query]}"
26
+ end
27
+
28
+ @url = "#{domain}#{@path}"
25
29
 
26
30
  if exists
27
31
  @response = get_current_response
@@ -33,6 +37,10 @@ module TShield
33
37
  end
34
38
 
35
39
  private
40
+ def domain
41
+ @domain ||= @configuration.get_domain_for(@path)
42
+ end
43
+
36
44
  def method
37
45
  @options[:method].downcase
38
46
  end
@@ -71,9 +79,21 @@ module TShield
71
79
  end
72
80
 
73
81
  def destiny
74
- destiny_path = File.join('requests')
75
- Dir.mkdir('requests') unless File.exists?(destiny_path)
76
- File.join(destiny_path, key)
82
+ return @destiny_path if @destiny_path
83
+
84
+ request_path = File.join('requests')
85
+ Dir.mkdir(request_path) unless File.exists?(request_path)
86
+
87
+ domain_path = File.join(request_path, domain.gsub(/.*:\/\//, ''))
88
+ Dir.mkdir(domain_path) unless File.exists?(domain_path)
89
+
90
+ path_path = File.join(domain_path, @path.gsub(/\//, '-'))
91
+ Dir.mkdir(path_path) unless File.exists?(path_path)
92
+
93
+ method_path = File.join(path_path, method)
94
+ Dir.mkdir(method_path) unless File.exists?(method_path)
95
+
96
+ @destiny_path = File.join(method_path, 'requests.json')
77
97
  end
78
98
 
79
99
  def write(content)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'sinatra'
4
4
 
5
+ require 'tshield/configuration'
5
6
  require 'tshield/request'
6
7
 
7
8
  module TShield
@@ -10,8 +11,8 @@ module TShield
10
11
  configure :production, :development do
11
12
  enable :logging
12
13
  end
13
-
14
- PATHP = /([a-zA-Z\/\.-]+)/
14
+
15
+ PATHP = /([a-zA-Z\/\.-_]+)/
15
16
 
16
17
  get (PATHP) do
17
18
  treat(params, request)
@@ -41,12 +42,15 @@ module TShield
41
42
  path = params.fetch('captures', [])[0]
42
43
 
43
44
  headers = {
44
- 'Content-Type' => request.content_type
45
+ 'Content-Type' => request.content_type || 'application/json'
45
46
  }
46
47
 
48
+ add_headers(headers, path)
49
+
47
50
  options = {
48
51
  method: method,
49
- headers: headers
52
+ headers: headers,
53
+ raw_query: request.env['QUERY_STRING']
50
54
  }
51
55
 
52
56
  if ['POST', 'PUT', 'PATCH'].include? method
@@ -70,6 +74,14 @@ module TShield
70
74
  content_type :json
71
75
  end
72
76
 
77
+ def add_headers(headers, path)
78
+ @configuration ||= TShield::Configuration.singleton
79
+ domain = @configuration.get_domain_for(path)
80
+ @configuration.get_headers(domain).each do |source, destiny|
81
+ headers[destiny] = request.env[source] unless request.env[source].nil?
82
+ end
83
+ end
84
+
73
85
  end
74
86
  end
75
87
 
@@ -1,9 +1,9 @@
1
1
  module TShield
2
2
  class Version
3
3
  MAJOR = 0
4
- MINOR = 0
5
- PATCH = 1
6
- PRE = 1
4
+ MINOR = 1
5
+ PATCH = 0
6
+ PRE = 0
7
7
 
8
8
  class << self
9
9
  def to_s
@@ -9,7 +9,7 @@ describe TShield::Configuration do
9
9
 
10
10
  describe 'load configurations from yaml' do
11
11
  it 'recover domains' do
12
- expect(@configuration.domains['example.org']).to(
12
+ expect(@configuration.domains['example.org']['paths']).to(
13
13
  include('/api/one', '/api/two'))
14
14
  end
15
15
  end
@@ -1,7 +1,9 @@
1
1
  domains:
2
2
  'example.org':
3
- - '/api/one'
4
- - '/api/two'
3
+ paths:
4
+ - '/api/one'
5
+ - '/api/two'
5
6
  'example.com':
6
- - '/api/three'
7
+ paths:
8
+ - '/api/three'
7
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tshield
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.1
4
+ version: 0.1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Rubin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-30 00:00:00.000000000 Z
11
+ date: 2016-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -178,5 +178,5 @@ specification_version: 4
178
178
  summary: Proxy for mocks API responses
179
179
  test_files:
180
180
  - spec/spec_helper.rb
181
- - spec/tshield/configuration_spec.rb
182
181
  - spec/tshield/fixtures/config/tshield.yml
182
+ - spec/tshield/configuration_spec.rb