dock_test 0.4.6 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7d02f2bd497b1164ea2681c4d3e511d80ba5d4e
4
- data.tar.gz: 76b280e326bb795ac5a5346bff4cfa4d8e929221
3
+ metadata.gz: 69b4418dfb475cb98e6a1f5fb3c06c42cf5ad7d9
4
+ data.tar.gz: 84b502628f75912d52ab1bfd4c853e1011dfab14
5
5
  SHA512:
6
- metadata.gz: f38ac82284f77aeeaf6f36c57d789d0bf4dcfac4664adbcbe37727ec8520628dc92901ac52feab7f50d16fd5e274a59d34e75c85c3c4ce360e947013eb50f27e
7
- data.tar.gz: 0f48db876fea3d8b463ecc3b019cdc89c89ae4ddf4aaf32b0c389cfec13a18252acca600e4580e14cda53bdaa14968cb04c3ddaeabbe62e29f8321529334c334
6
+ metadata.gz: ed1c9a530b71541d4d7fc6c44c81dc1b3f3bbee98539c8bd9bdeff84f3e48cf4179cf782f5ad0484b4646fa311833f34ffb6a2de00b94eeafb1eefab3409f8a2
7
+ data.tar.gz: f7dd09585cf31346a3ce24c382495d6ac81edceb4ba36aba578c41436d8386bdb9ff4f2a6f5f689997ac8367b33989322bcaca79c2efc0cc0a23dddbc080e13f
@@ -2,6 +2,7 @@ require 'multi_json'
2
2
  require 'nokogiri'
3
3
  require 'json-schema'
4
4
  require 'dock_test/version'
5
+ require 'dock_test/request_context'
5
6
  require 'dock_test/methods'
6
7
  require 'dock_test/dsl'
7
8
 
@@ -6,13 +6,16 @@ module DockTest
6
6
  %w(get post put patch delete options head).each do |meth_name|
7
7
  define_method meth_name do |path, params = '', headers = {}, &block|
8
8
 
9
- if verb_has_side_effects?(meth_name) && DockTest.skippy?
10
- skip_test_to_avoid_side_efforts
9
+ context = RequestContext.new(verb: meth_name, path: path, params: params, headers: headers)
10
+
11
+ if DockTest.skippy? && context.verb_has_side_effects
12
+ skip('this test is skipped in order to avoid potential side effects.')
11
13
  end
12
14
 
13
- construct_request(meth_name, path, params, headers)
15
+ @last_request = context.http_request
16
+ @last_response = context.execute
14
17
 
15
- execute_request
18
+ puts context.curl_command if ENV['OUTPUT_CURL']
16
19
 
17
20
  yield @last_response if block_given?
18
21
 
@@ -23,71 +26,6 @@ module DockTest
23
26
 
24
27
  private
25
28
 
26
- def execute_request
27
- # execute the request
28
- @last_response = Net::HTTP.start(@uri.hostname, @uri.port,
29
- :use_ssl => (@uri.scheme == 'https'),
30
- :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
31
- # processing oauth signing
32
- if DockTest.oauth?
33
- @last_request['Authorization'] =
34
- SimpleOAuth::Header.new(@last_request.method,
35
- request_url,
36
- {},
37
- :consumer_key => DockTest.oauth_consumer_key,
38
- :consumer_secret => DockTest.oauth_consumer_secret)
39
- end
40
-
41
- if ENV['OUTPUT_CURL']
42
- headers_string = @last_request.to_hash.map {|key, vals| vals.map {|val| [key, val]}.flatten}.map {|x| "-H '#{x[0].capitalize}: #{x[1]}'"}.join(' ')
43
- puts "curl -vv -X #{@last_request.method.upcase} -d '#{@last_request.body}' #{headers_string} '#{request_url}'"
44
- end
45
-
46
- http.request(@last_request)
47
- end
48
- end
49
-
50
- def construct_request(meth_name, path, params, headers)
51
-
52
- construct_uri(meth_name, path, params)
53
-
54
- @last_request = Net::HTTP.const_get(meth_name.capitalize).new(@uri.request_uri).tap do |req|
55
-
56
- # add the params to the body of other requests
57
- if verb_has_side_effects?(meth_name)
58
- req.body = params
59
- end
60
-
61
- # sets the headers
62
- headers.each do |key, value|
63
- req[key] = value
64
- end
65
- end
66
- end
67
-
68
- def construct_uri(meth_name, path, params)
69
- request_url = full_url(DockTest.url, path)
70
- @uri = URI.parse(request_url).tap do |uri|
71
-
72
- if DockTest.localhost?
73
- uri.port = DockTest.port
74
- end
75
-
76
- # add the params to the GET requests
77
- if !verb_has_side_effects?(meth_name) && !params.empty?
78
- if(params.is_a?(Hash))
79
- uri.query = URI.encode_www_form(URI.decode_www_form(uri.query || '') + params.to_a)
80
- else
81
- uri.query = uri.query.nil? ? params : "#{uri.query}&#{params}"
82
- end
83
- end
84
- end
85
- end
86
-
87
- def verb_has_side_effects?(verb)
88
- %w(post put patch delete).include?(verb)
89
- end
90
-
91
29
  def last_response
92
30
  @last_response
93
31
  end
@@ -100,19 +38,5 @@ module DockTest
100
38
  @last_request
101
39
  end
102
40
 
103
- def skip_test_to_avoid_side_efforts
104
- skip('this test is skipped in order to avoid potential side effects.')
105
- end
106
-
107
- # cleanse and combine url and path to retrieve a valid full url
108
- def full_url(url, path)
109
- if path.start_with?('http')
110
- path
111
- else
112
- url = url[0..1] if url.end_with?('/')
113
- path = path[1..-1] if path.start_with?('/')
114
- "#{url}/#{path}"
115
- end
116
- end
117
41
  end
118
42
  end
@@ -0,0 +1,92 @@
1
+ require 'simple_oauth'
2
+
3
+ module DockTest
4
+ class RequestContext
5
+
6
+ def initialize(options)
7
+ @options = options
8
+ end
9
+
10
+ def http_request
11
+ @_request ||= Net::HTTP.const_get(verb.capitalize).new(uri.request_uri).tap do |req|
12
+
13
+ # add the params to the body of other requests
14
+ req.body = params if verb_has_side_effects?
15
+
16
+ # sets the headers
17
+ headers.each do |key, value|
18
+ req[key] = value
19
+ end
20
+ end
21
+ end
22
+
23
+ def curl_command
24
+ headers_string = http_request.to_hash.
25
+ map {|key, vals| vals.map {|val| [key, val]}.flatten}.
26
+ map {|x| "-H '#{x[0].capitalize}: #{x[1]}'"}.join(' ')
27
+ "curl -vv -X #{http_request.method.upcase} -d '#{http_request.body}' #{headers_string} '#{request_url}'"
28
+ end
29
+
30
+ def execute
31
+ Net::HTTP.start(uri.hostname, uri.port,
32
+ :use_ssl => (uri.scheme == 'https'),
33
+ :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
34
+
35
+ oauth_sign! if DockTest.oauth? # processing oauth signing
36
+
37
+ http.request(http_request)
38
+ end
39
+
40
+ end
41
+
42
+ def verb_has_side_effects?
43
+ %w(post put patch delete).include?(verb)
44
+ end
45
+
46
+ private
47
+
48
+ def verb() @options[:verb]; end
49
+ def path() @options[:path]; end
50
+ def headers() @options[:headers]; end
51
+ def params() @options[:params]; end
52
+ def endpoint() DockTest.url; end
53
+
54
+ def oauth_sign!
55
+ http_request['Authorization'] =
56
+ SimpleOAuth::Header.new(verb,
57
+ request_url,
58
+ {},
59
+ :consumer_key => DockTest.oauth_consumer_key,
60
+ :consumer_secret => DockTest.oauth_consumer_secret)
61
+ end
62
+
63
+ def uri
64
+ @_uri ||= URI.parse(request_url).tap do |uri|
65
+
66
+ uri.port = DockTest.port if DockTest.localhost?
67
+
68
+ # add the params to the GET requests
69
+ if !verb_has_side_effects? && !params.empty?
70
+ if(params.is_a?(Hash))
71
+ uri.query = URI.encode_www_form(URI.decode_www_form(uri.query || '') + params.to_a)
72
+ else
73
+ uri.query = uri.query.nil? ? params : "#{uri.query}&#{params}"
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def request_url
80
+ end_path = path().dup
81
+ if end_path.start_with?('http')
82
+ end_path
83
+ else
84
+ url = endpoint.dup
85
+ url = url[0..1] if url.end_with?('/')
86
+ end_path = end_path[1..-1] if end_path.start_with?('/')
87
+ "#{url}#{path}"
88
+ end
89
+ end
90
+
91
+ end
92
+ end
@@ -1,3 +1,3 @@
1
1
  module DockTest
2
- VERSION = "0.4.6"
2
+ VERSION = "0.4.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dock_test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Xu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-21 00:00:00.000000000 Z
11
+ date: 2015-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -128,6 +128,7 @@ files:
128
128
  - lib/dock_test/assertions.rb
129
129
  - lib/dock_test/dsl.rb
130
130
  - lib/dock_test/methods.rb
131
+ - lib/dock_test/request_context.rb
131
132
  - lib/dock_test/version.rb
132
133
  - schemas/response.schema.json
133
134
  - schemas/response.schema.xsd