hyperspec 0.0.3 → 0.0.4
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.
- data/README.md +46 -10
- data/examples/readme/Gemfile +3 -0
- data/examples/readme/service.rb +29 -0
- data/examples/{service/example.ru → readme/service.ru} +1 -1
- data/examples/{spec/example_spec.rb → readme/service_spec.rb} +27 -8
- data/lib/hyperspec.rb +1 -1
- data/lib/hyperspec/version.rb +1 -1
- data/spec/hyperspec_spec.rb +20 -2
- metadata +6 -5
- data/examples/service/example.rb +0 -23
data/README.md
CHANGED
@@ -14,37 +14,60 @@ HTTP APIs from the outside.
|
|
14
14
|
|
15
15
|
## Example
|
16
16
|
|
17
|
-
```ruby
|
17
|
+
```ruby #source: examples/readme/service_spec.rb
|
18
18
|
service "http://localhost:4567" do
|
19
|
-
def
|
19
|
+
def responds_with_json_where
|
20
20
|
JSON.parse(response.body)
|
21
21
|
end
|
22
22
|
|
23
23
|
resource "/lolz" do
|
24
24
|
get do
|
25
|
+
|
25
26
|
it { responds_with.status :ok }
|
26
|
-
|
27
|
+
|
28
|
+
it { responds_with_json_where['lolz'].must_be_instance_of Array }
|
27
29
|
|
28
30
|
with_query("q=monorail") do
|
31
|
+
|
29
32
|
it "only lists lolz that match the query" do
|
30
|
-
|
31
|
-
|
33
|
+
responds_with_json_where['lolz'].wont_be_empty
|
34
|
+
responds_with_json_where['lolz'].each do |lol|
|
32
35
|
lol['title'].must_match /monorail/
|
33
36
|
end
|
34
37
|
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
with_query("q=looong") do
|
42
|
+
|
43
|
+
it "only lists lolz that match the query" do
|
44
|
+
responds_with_json_where['lolz'].wont_be_empty
|
45
|
+
responds_with_json_where['lolz'].each do |lol|
|
46
|
+
lol['title'].must_match /looong/
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
35
50
|
end
|
36
51
|
end
|
37
52
|
|
38
53
|
post do
|
39
54
|
describe "without request body" do
|
55
|
+
|
40
56
|
it { responds_with.status :unprocessable_entity }
|
57
|
+
|
41
58
|
end
|
42
59
|
|
43
60
|
describe "with request body" do
|
44
61
|
with_headers({ 'Content-Type' => 'application/json' }) do
|
45
62
|
with_request_body({ "title" => "Roflcopter!" }.to_json) do
|
63
|
+
|
46
64
|
it { responds_with.status :created }
|
47
|
-
|
65
|
+
|
66
|
+
it do
|
67
|
+
responds_with_json_where['lol']['title'].
|
68
|
+
must_equal 'Roflcopter!'
|
69
|
+
end
|
70
|
+
|
48
71
|
end
|
49
72
|
end
|
50
73
|
end
|
@@ -67,17 +90,17 @@ Sets the `URI` under test. Absolute or relative to the current scope.
|
|
67
90
|
|
68
91
|
Selects the HTTP method for the request.
|
69
92
|
|
70
|
-
### `
|
93
|
+
### `with_query`
|
71
94
|
|
72
|
-
Sets the query
|
95
|
+
Sets the query component of a request. Overrides previously set parameters.
|
73
96
|
|
74
97
|
### `with_headers`
|
75
98
|
|
76
|
-
Sets the headers
|
99
|
+
Sets the headers of a request. Merges with previously set headers.
|
77
100
|
|
78
101
|
### `with_body`
|
79
102
|
|
80
|
-
Sets the body
|
103
|
+
Sets the body of a request. Overrides previously set parameters.
|
81
104
|
|
82
105
|
### `response`
|
83
106
|
|
@@ -119,9 +142,22 @@ Thanks to:
|
|
119
142
|
- [Matthias Georgi](https://github.com/georgi) for suggestions for code improvements.
|
120
143
|
- [Lars Gierth](https://github.com/lgierth) for updating the example server to respond with 415 Unsupported Media Type in the appropriate case.
|
121
144
|
- [Anton Lindqvist](https://github.com/mptre) for adding HTTP Basic Auth
|
145
|
+
- [Josh Devins](https://github.com/joshdevins) for noticing missing HTTP status codes and `with_query` documentation inconsistency.
|
122
146
|
|
123
147
|
## History
|
124
148
|
|
149
|
+
### 0.0.4
|
150
|
+
|
151
|
+
#### 2012 11 22
|
152
|
+
|
153
|
+
- Change behavior of `with_query` to override instead of append.
|
154
|
+
|
155
|
+
### 0.0.3
|
156
|
+
|
157
|
+
#### 2012 08 10
|
158
|
+
|
159
|
+
- Add complete list of HTTP response codes.
|
160
|
+
|
125
161
|
### 0.0.2
|
126
162
|
|
127
163
|
#### 2012 03 30
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
get "/lolz" do
|
5
|
+
title = params[:q]
|
6
|
+
%^{"lolz": [ { "title": "#{title}" } ]}^
|
7
|
+
end
|
8
|
+
|
9
|
+
post "/lolz" do
|
10
|
+
request_body = request.env['rack.input'].read
|
11
|
+
|
12
|
+
if request_body == ''
|
13
|
+
status 422
|
14
|
+
else
|
15
|
+
if request.env['CONTENT_TYPE'] == "application/json"
|
16
|
+
params = JSON.parse(request_body)
|
17
|
+
|
18
|
+
if params.nil? || params.empty?
|
19
|
+
status 422
|
20
|
+
%^{"error": "An error occurred!"}^
|
21
|
+
else
|
22
|
+
status 201
|
23
|
+
%^{"lol": {"title": "Roflcopter!"}}^
|
24
|
+
end
|
25
|
+
else
|
26
|
+
status 415
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -3,47 +3,66 @@ require 'minitest/autorun'
|
|
3
3
|
|
4
4
|
require 'json'
|
5
5
|
|
6
|
-
#
|
6
|
+
# Shell 1:
|
7
|
+
# `cd examples/readme`
|
8
|
+
# Start service: `rackup -p 4567 example.ru`.
|
9
|
+
#
|
10
|
+
# Shell 2:
|
11
|
+
# Run specs: `be ruby -rubygems examples/readme/service_spec.rb`.
|
7
12
|
|
8
13
|
service "http://localhost:4567" do
|
9
|
-
def
|
14
|
+
def responds_with_json_where
|
10
15
|
JSON.parse(response.body)
|
11
16
|
end
|
12
17
|
|
13
18
|
resource "/lolz" do
|
14
19
|
get do
|
20
|
+
|
15
21
|
it { responds_with.status :ok }
|
16
|
-
|
22
|
+
|
23
|
+
it { responds_with_json_where['lolz'].must_be_instance_of Array }
|
17
24
|
|
18
25
|
with_query("q=monorail") do
|
26
|
+
|
19
27
|
it "only lists lolz that match the query" do
|
20
|
-
|
21
|
-
|
28
|
+
responds_with_json_where['lolz'].wont_be_empty
|
29
|
+
responds_with_json_where['lolz'].each do |lol|
|
22
30
|
lol['title'].must_match /monorail/
|
23
31
|
end
|
24
32
|
end
|
33
|
+
|
25
34
|
end
|
26
35
|
|
27
36
|
with_query("q=looong") do
|
37
|
+
|
28
38
|
it "only lists lolz that match the query" do
|
29
|
-
|
30
|
-
|
39
|
+
responds_with_json_where['lolz'].wont_be_empty
|
40
|
+
responds_with_json_where['lolz'].each do |lol|
|
31
41
|
lol['title'].must_match /looong/
|
32
42
|
end
|
33
43
|
end
|
44
|
+
|
34
45
|
end
|
35
46
|
end
|
36
47
|
|
37
48
|
post do
|
38
49
|
describe "without request body" do
|
50
|
+
|
39
51
|
it { responds_with.status :unprocessable_entity }
|
52
|
+
|
40
53
|
end
|
41
54
|
|
42
55
|
describe "with request body" do
|
43
56
|
with_headers({ 'Content-Type' => 'application/json' }) do
|
44
57
|
with_request_body({ "title" => "Roflcopter!" }.to_json) do
|
58
|
+
|
45
59
|
it { responds_with.status :created }
|
46
|
-
|
60
|
+
|
61
|
+
it do
|
62
|
+
responds_with_json_where['lol']['title'].
|
63
|
+
must_equal 'Roflcopter!'
|
64
|
+
end
|
65
|
+
|
47
66
|
end
|
48
67
|
end
|
49
68
|
end
|
data/lib/hyperspec.rb
CHANGED
data/lib/hyperspec/version.rb
CHANGED
data/spec/hyperspec_spec.rb
CHANGED
@@ -23,8 +23,8 @@ describe HyperSpec do
|
|
23
23
|
|
24
24
|
use_vcr_cassette('localhost')
|
25
25
|
|
26
|
-
it "should be of version 0.0.
|
27
|
-
HyperSpec::VERSION.must_equal "0.0.
|
26
|
+
it "should be of version 0.0.4" do
|
27
|
+
HyperSpec::VERSION.must_equal "0.0.4"
|
28
28
|
end
|
29
29
|
|
30
30
|
describe "MiniTest::Spec extensions" do
|
@@ -106,6 +106,24 @@ describe HyperSpec do
|
|
106
106
|
it { subject.base_uri.query.must_equal "q=monorail" }
|
107
107
|
end
|
108
108
|
|
109
|
+
describe "nested with_query" do
|
110
|
+
subject do
|
111
|
+
the_spec do |bound|
|
112
|
+
service("http://localhost") do
|
113
|
+
resource("/lolz") do
|
114
|
+
with_query("q=monorail") do
|
115
|
+
bound.value = with_query("q=lolz") {}
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'should override previously set query values' do
|
123
|
+
subject.base_uri.query.must_equal "q=lolz"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
109
127
|
describe "with_request_body" do
|
110
128
|
subject do
|
111
129
|
the_spec do |bound|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hyperspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
@@ -88,9 +88,10 @@ files:
|
|
88
88
|
- Gemfile
|
89
89
|
- README.md
|
90
90
|
- Rakefile
|
91
|
-
- examples/
|
92
|
-
- examples/service
|
93
|
-
- examples/
|
91
|
+
- examples/readme/Gemfile
|
92
|
+
- examples/readme/service.rb
|
93
|
+
- examples/readme/service.ru
|
94
|
+
- examples/readme/service_spec.rb
|
94
95
|
- hyperspec.gemspec
|
95
96
|
- lib/hyperspec.rb
|
96
97
|
- lib/hyperspec/version.rb
|
data/examples/service/example.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'sinatra'
|
2
|
-
require 'json'
|
3
|
-
|
4
|
-
get "/lolz" do
|
5
|
-
title = params[:q]
|
6
|
-
%^{"lolz": [ { "title": "#{title}" } ]}^
|
7
|
-
end
|
8
|
-
|
9
|
-
post "/lolz" do
|
10
|
-
if request.env['CONTENT_TYPE'] == "application/json"
|
11
|
-
params = JSON.parse(request.env['rack.input'].read)
|
12
|
-
|
13
|
-
if params.nil? || params.empty?
|
14
|
-
status 422
|
15
|
-
%^{"error": "An error occurred!"}^
|
16
|
-
else
|
17
|
-
status 201
|
18
|
-
%^{"lol": {"title": "Roflcopter!"}}^
|
19
|
-
end
|
20
|
-
else
|
21
|
-
status 415
|
22
|
-
end
|
23
|
-
end
|