http-double 0.1.8 → 0.1.9

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: 1a721d997225c62ba548ee917eae40eefdb4d8e3
4
- data.tar.gz: 5fe372379cdeec4b1256e51a8b649afb9d59ef61
3
+ metadata.gz: f23faa1f6653d54edc6a1b1584c02720859b5f56
4
+ data.tar.gz: 23e07dbfc8058a0f85ac642cc6ec2b1e466743c3
5
5
  SHA512:
6
- metadata.gz: 51c09d187246bf27b45ddb38222652ffd42d0a4269a3ebc461ffff4da8c4cada55c02b2cf5ff763acee724a2e9c686411b0742d95b4e8d24e72be0744a2b98e1
7
- data.tar.gz: a4205a95567b1862a7133496aaa7b385022b8281bda15e75646924c1c157224dc578d0fddb9b8f4d793c06ea172d9c2cee80e17d7238ebe2816b38b25e104115
6
+ metadata.gz: 052c0e4cadc430e204ae4a0e00554d0eafd1d3e1f37586200bdce216dc1546907a6f45e5edacaa49374d22d014561ed71511ddfa0a99c583c269207c42f6e072
7
+ data.tar.gz: 593c08637600a28ca534f2a3d6b2b51f0cc51fb04edca47dd33af0a43c2e950e76e21fd4fee60b6f2cdef5b7fbbf26371ac4a003146c31f406b8bb964a7cbee4
data/README.md CHANGED
@@ -1 +1,95 @@
1
- TODO
1
+ This gem allows you to double external HTTP services using [Sinatra](http://www.sinatrarb.com/). Your server(s) will be run in the background using [Thin](http://code.macournoyer.com/thin/) each time you run your tests/specs.
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ $ gem install http-double
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ First, define a server double:
12
+
13
+ ```ruby
14
+ require 'http-double'
15
+
16
+ class HelloDouble < HttpDouble::Base
17
+ get '/hello' do
18
+ [
19
+ 200,
20
+ {'Content-Type' => 'application/json'},
21
+ ['{"greeting":"Hello!"}']
22
+ ]
23
+ end
24
+ end
25
+ ```
26
+
27
+ Then background it in your spec helper or test setup routine, specifying a local address and port:
28
+
29
+ ```ruby
30
+ HelloDouble.background '127.0.0.1', 1357
31
+ ```
32
+
33
+ You can now write functional tests against your double, using no-nonsense HTTP over honest-to-goodness TCP.
34
+
35
+ ```ruby
36
+ describe 'The "Hello" server' do
37
+ it 'should greet me politely' do
38
+ response = Net::HTTP.get_response(URI 'http://127.0.0.1:1357/hello').body
39
+ expect(response).to include 'Hello!'
40
+ end
41
+ end
42
+ ```
43
+
44
+ ### Expectations on Traffic
45
+
46
+ Every request/response to/from an HTTP double is recorded in its `log`. To use the log effectively, clear it before each test.
47
+
48
+ ```ruby
49
+ before :each do
50
+ HelloDouble.log.clear
51
+ end
52
+ ```
53
+
54
+ Each member of the `log` array contains a `request` and a `response`. Here's a contrived example:
55
+
56
+ ```ruby
57
+ describe 'The "Hello" server' do
58
+ it 'should record traffic' do
59
+ log = HelloDouble.log
60
+ Net::HTTP.get_response URI 'http://127.0.0.1:1357/hello'
61
+
62
+ expect(log.count).to be 1
63
+
64
+ expect(log.last.request.verb).to be :get
65
+ expect(log.last.request.path).to eq '/hello'
66
+ expect(log.last.request.body).to be_nil
67
+
68
+ expect(log.last.response.code).to be 200
69
+ expect(log.last.response.headers['Content-Type']).to eq 'application/json'
70
+ expect(JSON.parse(log.last.response.body)['greeting']).to eq 'Hello!'
71
+ end
72
+ end
73
+ ```
74
+
75
+ In functional tests, you will tend to mostly want to test what your application sends. The log's request object lets you set expectations using indexed access:
76
+
77
+ ```ruby
78
+ def be_a_cowboy!
79
+ uri = URI 'http://127.0.0.1:1357/hello'
80
+ Net::HTTP.post_form uri, 'new_greeting' => 'Howdy!'
81
+ end
82
+
83
+ describe 'Something that changes my greeting' do
84
+ it 'should act like a cowboy' do
85
+ be_a_cowboy!
86
+ request = HelloDouble.log.last.request
87
+
88
+ expect(request.verb).to be :post
89
+ expect(request.path).to eq '/hello'
90
+ expect(request['new_greeting']).to eq 'Howdy!'
91
+ end
92
+ end
93
+ ```
94
+
95
+ You can use indexed access on any request sent as valid `application/x-www-form-urlencoded` or `application/json`.
@@ -12,7 +12,7 @@ module HttpDouble
12
12
  @initializer = block
13
13
  end
14
14
 
15
- def foreground(addr, port, log_path: '/dev/null', logger: nil)
15
+ def foreground(addr, port, log_path: '/dev/null', logger: nil, &block)
16
16
  server_class = self
17
17
  app_class = @app_class || server_class
18
18
 
@@ -26,13 +26,14 @@ module HttpDouble
26
26
  Thin::Server.start(addr, port) do
27
27
  initializer.call if initializer
28
28
  use RequestLogger, server_class.log
29
+ instance_exec &block if block
29
30
  run Class === app_class ? app_class.new : app_class
30
31
  end
31
32
 
32
33
  end
33
34
 
34
- def background(addr, port, **args)
35
- thread = Thread.new{ foreground addr, port, **args }
35
+ def background(addr, port, **args, &block)
36
+ thread = Thread.new{ foreground addr, port, **args, &block }
36
37
  thread.abort_on_exception = true
37
38
  sleep 0.05 until test_background addr, port
38
39
  thread
@@ -1,3 +1,3 @@
1
1
  module HttpDouble
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-double
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-20 00:00:00.000000000 Z
11
+ date: 2015-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -98,30 +98,30 @@ dependencies:
98
98
  name: rake
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
103
  version: '10.1'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '10.1'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rspec
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ">="
115
+ - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 3.0.0
117
+ version: '3.0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ">="
122
+ - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 3.0.0
124
+ version: '3.0'
125
125
  description: Provides a simple way to double HTTP services, APIs etc, for testing.
126
126
  email:
127
127
  - neil@helium.net.au
@@ -156,9 +156,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  version: '0'
157
157
  requirements: []
158
158
  rubyforge_project:
159
- rubygems_version: 2.2.2
159
+ rubygems_version: 2.4.6
160
160
  signing_key:
161
161
  specification_version: 4
162
162
  summary: Sinatra-based HTTP test doubling
163
163
  test_files: []
164
- has_rdoc: