dock_test 0.0.2 → 0.0.3

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: ab62525e9ffb9c9ad2f3dcd1e10f5d7b335a5a5f
4
- data.tar.gz: 1fd814ff7d00075e55c6e3c1db86a0cada15cd91
3
+ metadata.gz: 5c43981a27f80adac84538b16c9ae454d8bb3eed
4
+ data.tar.gz: eb96234292816cd2308d78441ef9894c4006d716
5
5
  SHA512:
6
- metadata.gz: 467ad61c6ea2b4ad5c3e0a133d4e35eb18c55450a3dd2ece31e7de297a712438ca7684baa6589a63549d0ae6ff3852d23fd4475c931309aee56dd186bc319765
7
- data.tar.gz: 27085dd0a2532b84953b1622177d1d61a5a2610d5f69b507389d0c1f6a10ff5ead1757c950e13eddefcd6a237991e313b6e4ad69785cc815f4243ce3591eaff0
6
+ metadata.gz: d662ffa52961ac51da3f33f1a08f0b48af5181857938a34b626654ff035f2fbe8ee4479409d81ef66479c8022d9c66d08532ad89e39e3d09ea0cebc782e1f305
7
+ data.tar.gz: 93cd0d97755211dbca00c9f0ed640492d1acd1c80371e75a250993858d9d1b3ae456e1d674c9b52c5b86bb6f5da656a85f2b221c26fcac6d807c51d4800b9746
data/Gemfile CHANGED
@@ -6,3 +6,4 @@ gemspec
6
6
  gem 'minitest'
7
7
  gem 'guard-jruby-minitest', '0.1.6'
8
8
  gem 'terminal-notifier-guard'
9
+ gem 'pry'
data/dock_test.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_dependency 'multi_json'
21
21
  spec.add_dependency 'rack'
22
+ spec.add_dependency 'json-schema'
22
23
  spec.add_development_dependency "bundler", "~> 1.6"
23
24
  spec.add_development_dependency "rake"
24
25
  end
@@ -1,5 +1,23 @@
1
- module DockTest
2
- module Assertions
1
+ module Minitest::Assertions
2
+ def assert_response_status(status)
3
+ assert last_response.code == status.to_s, "The response status is expected to be #{last_response.code}"
4
+ end
5
+
6
+ def assert_response_content_type(content_type)
7
+ assert last_response['Content-Type'] == content_type.to_s, "The response content_type is expected to be #{last_response['Content-Type']}"
8
+ end
9
+
10
+ def assert_response_headers(headers, options = {})
11
+ exclude_keys = Array(options[:exclude] || [])
12
+ response_headers = last_response.to_hash.delete_if {|k, _| exclude_keys.include?(k)}
13
+ end
14
+
15
+ def assert_response_body(body_string)
16
+ assert last_response.body == body_string, "The response body is expected to be #{last_response.body}"
17
+ end
3
18
 
19
+ def assert_response_json_schema(schema_path)
20
+ schema = File.open(schema_path).read
21
+ assert JSON::Validator.validate(schema, last_response.body), "The response is expected to have a schema defined in #{schema_path}"
4
22
  end
5
23
  end
@@ -4,6 +4,12 @@ module DockTest
4
4
  %w(get post put patch delete options head).each do |meth_name|
5
5
  define_method meth_name do |path, params = '', headers = {}, &block|
6
6
 
7
+ with_side_effects = verb_has_side_effects?(meth_name)
8
+
9
+ if with_side_effects && DockTest.skippy_envs.include?(dock_env)
10
+ skip_test_to_avoid_side_efforts
11
+ end
12
+
7
13
  uri = URI.join(DockTest.url, path)
8
14
 
9
15
  if DockTest.localhost?
@@ -11,7 +17,7 @@ module DockTest
11
17
  end
12
18
 
13
19
  # add the params to the GET requests
14
- if meth_name == 'get' && !params.empty?
20
+ if !with_side_effects && !params.empty?
15
21
  if(params.is_a?(Hash))
16
22
  uri.query = URI.encode_www_form(URI.decode_www_form(uri.query || '') + params.to_a)
17
23
  else
@@ -22,7 +28,7 @@ module DockTest
22
28
  @last_request = Net::HTTP.const_get(meth_name.capitalize).new(uri.request_uri)
23
29
 
24
30
  # add the params to the body of other requests
25
- if meth_name != 'get'
31
+ if with_side_effects
26
32
  @last_request.body = params
27
33
  end
28
34
 
@@ -45,6 +51,15 @@ module DockTest
45
51
  end
46
52
 
47
53
  private
54
+
55
+ def dock_env
56
+ ENV['DOCK_ENV']
57
+ end
58
+
59
+ def verb_has_side_effects?(verb)
60
+ %w(post put patch delete).include?(verb)
61
+ end
62
+
48
63
  def last_response
49
64
  @last_response
50
65
  end
@@ -53,5 +68,8 @@ module DockTest
53
68
  @last_request
54
69
  end
55
70
 
71
+ def skip_test_to_avoid_side_efforts
72
+ skip('this test is skipped in order to avoid potential side effects.')
73
+ end
56
74
  end
57
75
  end
@@ -1,3 +1,3 @@
1
1
  module DockTest
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/dock_test.rb CHANGED
@@ -1,28 +1,30 @@
1
+ require 'multi_json'
2
+ require 'json-schema'
1
3
  require "dock_test/version"
2
4
  require "dock_test/methods"
3
- require "dock_test/assertions"
5
+
6
+ if defined?(::Minitest)
7
+ require 'dock_test/assertions'
8
+ else
9
+ end
4
10
 
5
11
  module DockTest
6
12
 
7
13
  class << self
8
14
 
9
- attr_writer :port
10
- def port
11
- @port ||= 9999
12
- end
13
-
14
15
  attr_reader :url
15
16
  # sets the test url
16
17
  # also creates a new webrick server process
17
18
  def url=(value)
18
19
  @url = value
19
- if localhost?
20
+
21
+ if localhost? && @server_thread.nil?
20
22
  require "rack"
21
23
  require 'webrick'
22
24
  server = WEBrick::HTTPServer.new(:Port => port).tap do |server|
23
25
  server.mount '/', Rack::Handler::WEBrick, Rack::Server.new.app
24
26
  end
25
- t = Thread.new { server.start }
27
+ @server_thread = Thread.new { server.start }
26
28
  trap('INT') do
27
29
  server.shutdown
28
30
  exit
@@ -30,8 +32,21 @@ module DockTest
30
32
  end
31
33
  end
32
34
 
35
+ def port
36
+ URI.parse(@url).port
37
+ end
38
+
33
39
  def localhost?
34
40
  @url && ['127.0.0.1', 'localhost'].include?(URI.parse(@url).host)
35
41
  end
42
+
43
+ attr_reader :skippy_envs
44
+ def skippy=(envs)
45
+ @skippy_envs = Array(envs).map(&:to_s)
46
+ end
47
+
48
+ def config(&block)
49
+ block.call(DockTest)
50
+ end
36
51
  end
37
52
  end
@@ -0,0 +1,49 @@
1
+ {
2
+ "type":"object",
3
+ "$schema": "http://json-schema.org/draft-03/schema",
4
+ "required":true,
5
+ "properties":{
6
+ "body": {
7
+ "type":"string",
8
+ "required":true
9
+ },
10
+ "headers": {
11
+ "type":"object",
12
+ "required":true,
13
+ "properties":{
14
+ "ACCEPT": {
15
+ "type":"string",
16
+ "required":true
17
+ },
18
+ "CONTENT_TYPE": {
19
+ "type":"string",
20
+ "required":true
21
+ },
22
+ "HOST": {
23
+ "type":"string",
24
+ "required":true
25
+ },
26
+ "USER_AGENT": {
27
+ "type":"string",
28
+ "required":true
29
+ },
30
+ "VERSION": {
31
+ "type":"string",
32
+ "required":true
33
+ }
34
+ }
35
+ },
36
+ "protcol": {
37
+ "type":"string",
38
+ "required":true
39
+ },
40
+ "uri": {
41
+ "type":"string",
42
+ "required":true
43
+ },
44
+ "verb": {
45
+ "type":"string",
46
+ "required":true
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ class TestAssertions < Minitest::Test
4
+
5
+ include DockTest::Methods
6
+
7
+ def test_assert_response_status_method
8
+ get '/path?foo=bar', {a: :b}, {'CONTENT_TYPE' => 'application/json'}
9
+ assert_response_status 200
10
+ end
11
+
12
+ def test_assert_response_content_type_method
13
+ get '/path?foo=bar', {a: :b}, {'CONTENT_TYPE' => 'application/json'}
14
+ assert_response_content_type 'application/json'
15
+ end
16
+
17
+ def test_assert_response_headers_method
18
+ get '/path?foo=bar', {a: :b}, {'CONTENT_TYPE' => 'application/json'}
19
+ assert_response_headers({"content-type"=>["application/json"], "content-length"=>["218"], "connection"=>["Keep-Alive"]} , {exclude: ['server', 'date']})
20
+ end
21
+
22
+ def test_assert_response_body_method
23
+ skip unless ENV['DOCK_ENV'] == 'development'
24
+ get '/path?foo=bar', {a: :b}, {'CONTENT_TYPE' => 'application/json'}
25
+ assert_response_body '{"verb":"GET","uri":"http://localhost:9871/path?foo=bar&a=b","body":"","protcol":"HTTP/1.1","headers":{"ACCEPT":"*/*","USER_AGENT":"Ruby","CONTENT_TYPE":"application/json","HOST":"localhost:9871","VERSION":"HTTP/1.1"}}'
26
+ end
27
+
28
+ def test_assert_response_json_schema_method
29
+ get '/path?foo=bar', {a: :b}, {'CONTENT_TYPE' => 'application/json'}
30
+ assert_response_json_schema 'schemas/response.schema.json'
31
+ end
32
+
33
+ end
@@ -1,15 +1,13 @@
1
1
  require 'test_helper'
2
- require 'multi_json'
3
2
 
4
3
  class TestMethods < Minitest::Test
5
4
 
6
5
  include DockTest::Methods
7
- DockTest.url = 'http://localhost:3000'
8
6
 
9
7
  def test_get_method_with_hash_body
10
8
  get '/path?foo=bar', {a: :b}, {'CONTENT_TYPE' => 'application/json'}
11
9
 
12
- assert_equal last_response_json['uri'], 'http://localhost:9999/path?foo=bar&a=b'
10
+ assert_equal last_response_json['uri'], "#{DockTest.url}/path?foo=bar&a=b"
13
11
  assert_equal last_response_json['verb'], 'GET'
14
12
  assert_equal last_response_json['body'], ''
15
13
  assert_equal last_response_json['headers']['CONTENT_TYPE'], 'application/json'
@@ -18,7 +16,7 @@ class TestMethods < Minitest::Test
18
16
  def test_get_method_with_string_body
19
17
  get '/path?foo=bar', 'a=b', {'CONTENT_TYPE' => 'application/json'}
20
18
 
21
- assert_equal last_response_json['uri'], 'http://localhost:9999/path?foo=bar&a=b'
19
+ assert_equal last_response_json['uri'], "#{DockTest.url}/path?foo=bar&a=b"
22
20
  assert_equal last_response_json['verb'], 'GET'
23
21
  assert_equal last_response_json['body'], ''
24
22
  assert_equal last_response_json['headers']['CONTENT_TYPE'], 'application/json'
@@ -27,7 +25,7 @@ class TestMethods < Minitest::Test
27
25
  def test_get_method_to_root
28
26
  get '/', 'a=b', {'CONTENT_TYPE' => 'application/json'}
29
27
 
30
- assert_equal last_response_json['uri'], 'http://localhost:9999/?a=b'
28
+ assert_equal last_response_json['uri'], "#{DockTest.url}/?a=b"
31
29
  assert_equal last_response_json['verb'], 'GET'
32
30
  assert_equal last_response_json['body'], ''
33
31
  assert_equal last_response_json['headers']['CONTENT_TYPE'], 'application/json'
@@ -36,7 +34,7 @@ class TestMethods < Minitest::Test
36
34
  def test_post_method
37
35
  post '/path?foo=bar', MultiJson.dump({guid: '12345'}), {'CONTENT_TYPE' => 'application/json'}
38
36
 
39
- assert_equal last_response_json['uri'], 'http://localhost:9999/path?foo=bar'
37
+ assert_equal last_response_json['uri'], "#{DockTest.url}/path?foo=bar"
40
38
  assert_equal last_response_json['verb'], 'POST'
41
39
  assert_equal last_response_json['body'], "{\"guid\":\"12345\"}"
42
40
  assert_equal last_response_json['headers']['CONTENT_TYPE'], 'application/json'
@@ -45,7 +43,7 @@ class TestMethods < Minitest::Test
45
43
  def test_put_method
46
44
  put '/path?foo=bar', MultiJson.dump({guid: '12345'}), {'CONTENT_TYPE' => 'application/json'}
47
45
 
48
- assert_equal last_response_json['uri'], 'http://localhost:9999/path?foo=bar'
46
+ assert_equal last_response_json['uri'], "#{DockTest.url}/path?foo=bar"
49
47
  assert_equal last_response_json['verb'], 'PUT'
50
48
  assert_equal last_response_json['body'], "{\"guid\":\"12345\"}"
51
49
  assert_equal last_response_json['headers']['CONTENT_TYPE'], 'application/json'
@@ -54,7 +52,7 @@ class TestMethods < Minitest::Test
54
52
  def test_patch_method
55
53
  patch '/path?foo=bar', MultiJson.dump({guid: '12345'}), {'CONTENT_TYPE' => 'application/json'}
56
54
 
57
- assert_equal last_response_json['uri'], 'http://localhost:9999/path?foo=bar'
55
+ assert_equal last_response_json['uri'], "#{DockTest.url}/path?foo=bar"
58
56
  assert_equal last_response_json['verb'], 'PATCH'
59
57
  assert_equal last_response_json['body'], "{\"guid\":\"12345\"}"
60
58
  assert_equal last_response_json['headers']['CONTENT_TYPE'], 'application/json'
@@ -63,7 +61,7 @@ class TestMethods < Minitest::Test
63
61
  def test_delete_method
64
62
  delete '/path?foo=bar', '', {'CONTENT_TYPE' => 'application/json'}
65
63
 
66
- assert_equal last_response_json['uri'], 'http://localhost:9999/path?foo=bar'
64
+ assert_equal last_response_json['uri'], "#{DockTest.url}/path?foo=bar"
67
65
  assert_equal last_response_json['verb'], 'DELETE'
68
66
  assert_equal last_response_json['body'], ''
69
67
  assert_equal last_response_json['headers']['CONTENT_TYPE'], 'application/json'
data/test/test_helper.rb CHANGED
@@ -1,5 +1,16 @@
1
- require 'dock_test'
2
-
3
1
  require 'bundler/setup'
4
- Bundler.require(:default, :test)
5
2
  require 'minitest/autorun'
3
+ Bundler.require(:default, :test)
4
+
5
+ require 'dock_test'
6
+
7
+ DockTest.config do |c|
8
+ c.url = case ENV['DOCK_ENV']
9
+ when 'production'
10
+ 'http://floating-mesa-6194.herokuapp.com'
11
+ else
12
+ 'http://localhost:9871'
13
+ end
14
+ c.skippy = :production
15
+ end
16
+
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.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Xu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-17 00:00:00.000000000 Z
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -38,6 +38,20 @@ dependencies:
38
38
  version: '0'
39
39
  prerelease: false
40
40
  type: :runtime
41
+ - !ruby/object:Gem::Dependency
42
+ name: json-schema
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ prerelease: false
54
+ type: :runtime
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  version_requirements: !ruby/object:Gem::Requirement
@@ -85,7 +99,9 @@ files:
85
99
  - lib/dock_test/assertions.rb
86
100
  - lib/dock_test/methods.rb
87
101
  - lib/dock_test/version.rb
88
- - test/dock_test/test_localhost_methods.rb
102
+ - schemas/response.schema.json
103
+ - test/dock_test/test_assertions.rb
104
+ - test/dock_test/test_methods.rb
89
105
  - test/test_helper.rb
90
106
  homepage: https://github.com/jackxxu/dock_test
91
107
  licenses:
@@ -112,5 +128,6 @@ signing_key:
112
128
  specification_version: 4
113
129
  summary: an outside-in service api test framework.
114
130
  test_files:
115
- - test/dock_test/test_localhost_methods.rb
131
+ - test/dock_test/test_assertions.rb
132
+ - test/dock_test/test_methods.rb
116
133
  - test/test_helper.rb