hobby-test 0.0.3 → 0.0.4

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: 41d3a46cd1003417175fef9910b7983f1d755b4f
4
- data.tar.gz: a93d9a3cf7e609d21c3f65b78e18b14bb96245a9
3
+ metadata.gz: 61cd3a457c60be67607b061852aef03d00d6fcd7
4
+ data.tar.gz: e9fae2b5ebbc5a727032b715a784323ec3e817c9
5
5
  SHA512:
6
- metadata.gz: 6e695d0c7f351339dc0a1038653e5b1d72e6b239a378f1daa4d9e5c44821dddb18acf65a05ecbca5b97c42ec5e4867046384ef6ee227409f7607321d98290124
7
- data.tar.gz: 51b35c5e433319f648a4fdaa45c27b0962a2064a442527f313e133dc6322b3c43a5454f5b3e5913a7ba80279d74825909576bd53d732d59e013d19d84b664f39
6
+ metadata.gz: 256a68e3c67766a20b0719e2a23a39d966938bf460f3619e3251755a208d867ba0f1f566f9bb94e04ca614a96b5e42d078757c939357fe929417e3c893cb2494
7
+ data.tar.gz: 7e0eacc4fb1cbf4a702ab5b0615a44f97e5df84115290ec3f3cab7bf9cf718d165b73930893b370246ec4a6b84dda6cc85c9f38c420855b135a36ea5b9446b84
data/hobby-test.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |g|
2
2
  g.name = 'hobby-test'
3
3
  g.files = `git ls-files`.split($/)
4
- g.version = '0.0.3'
4
+ g.version = '0.0.4'
5
5
  g.summary = 'A way to test HTTP exchanges via YAML specifications'
6
6
  g.authors = ['Anatoly Chernow']
7
7
 
data/lib/hobby/test.rb CHANGED
@@ -8,6 +8,7 @@ require 'ostruct'
8
8
 
9
9
  require 'hobby/test/exchange'
10
10
  require 'hobby/test/report'
11
+ require 'hobby/test/env'
11
12
 
12
13
  module Hobby
13
14
  class Test
@@ -29,8 +30,9 @@ module Hobby
29
30
  else
30
31
  Excon.new 'unix:///', socket: address
31
32
  end
33
+ env = Env.new connection
32
34
 
33
- Report.new @exchanges.map &[connection]
35
+ Report.new @exchanges.map &[env]
34
36
  end
35
37
  end
36
38
  end
@@ -0,0 +1,17 @@
1
+ module Hobby
2
+ class Test
3
+ class Env < OpenStruct
4
+ def initialize connection
5
+ super connection: connection, responses: []
6
+ end
7
+
8
+ def last_response
9
+ responses.last
10
+ end
11
+
12
+ def uri *all
13
+ "/#{all.join '/'}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -9,8 +9,8 @@ module Hobby
9
9
  end
10
10
  attr_reader :request, :asserts
11
11
 
12
- def [] connection
13
- dup.run_against connection
12
+ def [] env
13
+ dup.perform_in env
14
14
  end
15
15
 
16
16
  def ok?
@@ -18,9 +18,9 @@ module Hobby
18
18
  end
19
19
 
20
20
  protected
21
- def run_against connection
22
- response = connection.public_send request.verb, **request
23
- @asserts = asserts.map &[response]
21
+ def perform_in env
22
+ request.perform_in env
23
+ @asserts = asserts.map &[env.last_response]
24
24
  self
25
25
  end
26
26
  end
@@ -28,4 +28,6 @@ module Hobby
28
28
  end
29
29
 
30
30
  require 'hobby/test/exchange/request'
31
+ require 'hobby/test/exchange/response'
31
32
  require 'hobby/test/exchange/assert'
33
+ require 'hobby/test/template'
@@ -26,33 +26,34 @@ class Hobby::Test::Exchange
26
26
  end
27
27
  end
28
28
 
29
+ def assert response
30
+ @actual_value = response.public_send key
31
+ compare
32
+ self
33
+ end
34
+
29
35
  attr_reader :actual_value, :specified_value, :chain, :key
30
36
 
31
37
  class Status
32
38
  include Assert
33
39
 
34
- def assert response
35
- @actual_value = response.public_send key
40
+ def compare
36
41
  @ok = actual_value == specified_value
37
- self
38
42
  end
39
43
  end
40
44
 
41
45
  class Headers
42
46
  include Assert
43
47
 
44
- def assert response
45
- @actual_value = response.public_send key
48
+ def compare
46
49
  @ok = chain.empty? ? actual_value == specified_value : compare_chain
47
- self
48
50
  end
49
51
  end
50
52
 
51
53
  class Body
52
54
  include Assert
53
55
 
54
- def assert response
55
- @actual_value = response.public_send key
56
+ def compare
56
57
  @actual_value = begin
57
58
  JSON.parse actual_value
58
59
  rescue JSON::ParserError
@@ -64,8 +65,6 @@ class Hobby::Test::Exchange
64
65
  else
65
66
  compare_chain
66
67
  end
67
-
68
- self
69
68
  end
70
69
  end
71
70
  end
@@ -1,16 +1,36 @@
1
1
  class Hobby::Test::Exchange
2
2
  class Request < OpenStruct
3
3
  VERBS = %w[delete get head options patch post put]
4
- def initialize array
5
- @verb = array[0]
6
- super array[1]
4
+ def initialize pair
5
+ @verb, hash = pair
6
+
7
+ template_fields, regular_fields = hash.partition do |key, _|
8
+ key.start_with? 'template.'
9
+ end
10
+ @templates = template_fields.map &Hobby::Test::Template
11
+
12
+ super regular_fields.to_h
7
13
  end
8
14
  attr_reader :verb
9
15
 
10
- def to_hash
16
+ def regular_fields body_serializer:
11
17
  hash = to_h
12
- hash[:body] = hash[:body].to_json
18
+
19
+ if body && body_serializer
20
+ hash[:body] = body_serializer.dump body
21
+ end
22
+
13
23
  hash
14
24
  end
25
+
26
+ def perform_in env
27
+ body_serializer = JSON if body.is_a? Hash
28
+ params = regular_fields(body_serializer: body_serializer)
29
+ .merge @templates.map(&[env]).to_h
30
+
31
+ excon_response = env.connection.public_send verb, **params
32
+ response = Response.new excon_response, body_serializer: body_serializer
33
+ env.responses << response
34
+ end
15
35
  end
16
36
  end
@@ -0,0 +1,16 @@
1
+ class Hobby::Test::Exchange
2
+ class Response
3
+ def initialize excon_response, body_serializer:
4
+ @excon_response, @body_serializer = excon_response, body_serializer
5
+ end
6
+ attr_reader :body_serializer
7
+
8
+ def [] key
9
+ @serialized ||= JSON.load body
10
+ @serialized[key]
11
+ end
12
+
13
+ extend Forwardable
14
+ delegate [:status, :headers, :body] => :@excon_response
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module Hobby
2
+ class Test
3
+ class Template
4
+ def initialize pair
5
+ @key = pair.first.partition('.').last.to_sym
6
+ @string = pair.last
7
+ end
8
+
9
+ def [] env
10
+ value = env.instance_eval @string
11
+ [@key, value]
12
+ end
13
+ end
14
+ end
15
+ end
data/readme.adoc ADDED
@@ -0,0 +1,8 @@
1
+ = hobby-test
2
+
3
+ :uri-for-rack-test: https://github.com/brynary/rack-test
4
+
5
+ A way to test HTTP exchanges via YAML specifications. In contrast to {uri-for-rack-test}[rack-test], it works via real HTTP requests, which makes it applicable to HTTP services written in any language, not only Ruby.
6
+
7
+ == How it works
8
+ I'll write it later. For now, I just want to see how Github renders it.
data/spec/apps/main.rb CHANGED
@@ -66,4 +66,11 @@ class MainApp
66
66
  end
67
67
  end
68
68
  map('/array') { run ArrayApp.new }
69
+
70
+ class ForLastResponse
71
+ include Hobby::App
72
+ post { 42 }
73
+ get '/:id' do my[:id].to_i * 2 end
74
+ end
75
+ map('/for_last_response') { run ForLastResponse.new }
69
76
  end
@@ -0,0 +1,11 @@
1
+ - post:
2
+ path: /for_last_response
3
+
4
+ response:
5
+ body: "42"
6
+
7
+ - get:
8
+ template.path: uri 'for_last_response', last_response.body
9
+
10
+ response:
11
+ body: "84"
@@ -0,0 +1,9 @@
1
+ - get:
2
+ path: /hash
3
+
4
+ - get:
5
+ template.path: uri 'for_last_response', last_response['a']
6
+
7
+ response:
8
+ status: 200
9
+ body: '2'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobby-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoly Chernow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-25 00:00:00.000000000 Z
11
+ date: 2016-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon
@@ -49,28 +49,30 @@ files:
49
49
  - debug.rb
50
50
  - hobby-test.gemspec
51
51
  - lib/hobby/test.rb
52
+ - lib/hobby/test/env.rb
52
53
  - lib/hobby/test/exchange.rb
53
54
  - lib/hobby/test/exchange/assert.rb
54
55
  - lib/hobby/test/exchange/request.rb
56
+ - lib/hobby/test/exchange/response.rb
55
57
  - lib/hobby/test/report.rb
58
+ - lib/hobby/test/template.rb
59
+ - readme.adoc
56
60
  - spec/apps/main.rb
57
61
  - spec/auto_spec.rb
58
62
  - spec/helper.rb
59
- - spec/http/basic.yml
60
- - spec/http/event.yml
61
- - spec/http/reverse.yml
62
63
  - spec/run_mutant.rb
63
64
  - spec/setup/mutant.rb
64
65
  - spec/setup/power_assert.rb
65
66
  - spec/tcp_spec.rb
66
67
  - spec/yml/failing/0.yml
67
68
  - spec/yml/passing/0.yml
69
+ - spec/yml/passing/access_last_response.yml
68
70
  - spec/yml/passing/chain_assertions.yml
69
71
  - spec/yml/passing/counter.yml
70
72
  - spec/yml/passing/echo.yml
71
73
  - spec/yml/passing/headers.yml
72
74
  - spec/yml/passing/query.yml
73
- - useful_links
75
+ - spec/yml/passing/referring_to_json_body.yml
74
76
  homepage:
75
77
  licenses: []
76
78
  metadata: {}
data/spec/http/basic.yml DELETED
@@ -1,14 +0,0 @@
1
- - request:
2
- verb: get
3
- path: /
4
-
5
- response:
6
- status: 200
7
- body: root-only app
8
-
9
- - request:
10
- verb: get
11
- path: /non-existent
12
-
13
- response:
14
- status: 404
data/spec/http/event.yml DELETED
@@ -1,11 +0,0 @@
1
- request:
2
- verb: POST
3
- data:
4
- text: text
5
- username: username
6
-
7
- response:
8
- code: 201
9
- body:
10
- id: event-endpoint
11
- text: txet
@@ -1,24 +0,0 @@
1
- - default:
2
- verb: post
3
- path: /event
4
- format: json
5
-
6
- - request:
7
- body:
8
- text: text
9
-
10
- response:
11
- status: 201
12
- body:
13
- name: ReverseApp
14
- text: txet
15
-
16
- - request:
17
- body:
18
- text: reverse
19
-
20
- response:
21
- status: 201
22
- body:
23
- name: ReverseApp
24
- text: something entirely else
data/useful_links DELETED
@@ -1,3 +0,0 @@
1
- https://github.com/excon/excon
2
- http://blog.paulrugelhiatt.com/ruby/testing/rspec/2014/11/28/improving-rspec-test-clarity-with-yaml-expectations.html
3
- http://rspec-api.github.io/