cucumber-http 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ # Cucumber::Http
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cucumber/http`.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cucumber-http'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cucumber-http
20
+
21
+ ## Usage
22
+
23
+ ## Development
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/[my-github-username]/cucumber-http/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'cucumber/http/transforms'
2
+ require 'cucumber/http/world_extensions'
3
+ require 'cucumber/http/hooks'
4
+ require 'cucumber/http/http_steps'
5
+ require 'cucumber/http/benchmark_steps'
6
+ require 'cucumber/http/debug_steps'
@@ -0,0 +1,9 @@
1
+ Given /^I am benchmarking$/ do
2
+ @scenario_start_time = Time.now
3
+ end
4
+
5
+ Then /^the elapsed time should be less than (#{CAPTURE_FLOAT}) seconds?$/ do |time|
6
+ elapsed = Time.now - @scenario_start_time
7
+
8
+ expect(elapsed).to be < time
9
+ end
@@ -0,0 +1,16 @@
1
+ Then /^show me the( unparsed)? response$/ do |unparsed|
2
+ if unparsed
3
+ puts response[:body]
4
+ elsif response[:headers]['Content-Type'] =~ /json/
5
+ puts JSON.pretty_generate(JSON.parse(response['body']))
6
+ elsif response[:headers]['Content-Type'] =~ /xml/
7
+ puts Nokogiri::XML(response[:body])
8
+ else
9
+ puts response[:headers]
10
+ puts response[:body]
11
+ end
12
+ end
13
+
14
+ Then /^show me the kept values?$/ do
15
+ puts JsonSpec.memory
16
+ end
@@ -0,0 +1,7 @@
1
+ After do
2
+ clear_url
3
+ clear_parameters
4
+ clear_headers
5
+ clear_payload
6
+ clear_response
7
+ end
@@ -0,0 +1,103 @@
1
+ require 'nokogiri'
2
+ require 'json_spec'
3
+
4
+ Given /^I set headers?:$/ do |hdrs|
5
+ hdrs.rows_hash.each { |name, value| add_header(name, value) }
6
+ end
7
+
8
+ Given /^I send "(.*?)" and accept (XML|JSON)$/ do |content_type, accept_type|
9
+ steps %Q{
10
+ Given I set headers:
11
+ | Content-Type | #{content_type} |
12
+ | Accept | application/#{accept_type.downcase} |
13
+ }
14
+ end
15
+
16
+ Given /^I send and accept (XML|JSON)$/ do |type|
17
+ steps %Q{
18
+ Given I set headers:
19
+ | Content-Type | application/#{type.downcase} |
20
+ | Accept | application/#{type.downcase} |
21
+ }
22
+ end
23
+
24
+ Given(/^I set the JSON request payload to '(.*?)'$/) do |payload|
25
+ set_payload(JSON.parse(resolve(payload)).to_json)
26
+ end
27
+
28
+ Given(/^I set the JSON request payload to:$/) do |payload|
29
+ set_payload(JSON.parse(resolve(payload)).to_json)
30
+ end
31
+
32
+ Given(/^I set the JSON request payload from "(.*?)"$/) do |filename|
33
+ path = "#{Dir.pwd}/features/support/data/#{filename}"
34
+
35
+ if File.file? path
36
+ set_payload(JSON.parse(resolve(File.read(path))).to_json)
37
+ else
38
+ raise "File not found: '#{path}'"
39
+ end
40
+ end
41
+
42
+ When /^I send a (GET|POST|PATCH|PUT|DELETE) request to "([^"]*)"(?: with parameters?:)?$/ do |*args|
43
+ method = args.shift
44
+ endpoint = resolve(args.shift)
45
+ params = args.shift
46
+
47
+ request_url = URI.join(url, endpoint).to_s
48
+
49
+ unless params.nil?
50
+ if params.class == Cucumber::MultilineArgument::DataTable
51
+ params_hash = params.rows_hash
52
+ else
53
+ params_hash = Hash[params.split('&').inject([]) { |result, param| result << param.split('=') }]
54
+ end
55
+
56
+ params_hash.each { |name, value| add_parameter(name, value) }
57
+ end
58
+
59
+ perform_request(method, request_url)
60
+ end
61
+
62
+ When /^(?:I )?keep the value of the (?:JSON|json)(?: response)?(?: at "(.*)")? as "(.*)"$/ do |path, key|
63
+ JsonSpec.memorize(key, parse_json(last_json, path))
64
+ end
65
+
66
+ Then /^the response status should( not)? be "(#{CAPTURE_INTEGER})"$/ do |negative, status_code|
67
+ if negative
68
+ expect(response[:status]).not_to eq(status_code)
69
+ else
70
+ expect(response[:status]).to eq(status_code)
71
+ end
72
+ end
73
+
74
+ Then /^the response body should be valid (XML|JSON)$/ do |type|
75
+ case type
76
+ when 'XML'
77
+ expect { Nokogiri::XML(response[:body]) { |config| config.strict } }.not_to raise_error
78
+ when 'JSON'
79
+ expect { JSON.parse(response[:body]) }.not_to raise_error
80
+ end
81
+ end
82
+
83
+ Then /^the JSON response should( not)? be '([^']*)'$/ do |negative, expected_response|
84
+ expected = JSON.parse(expected_response)
85
+ actual = JSON.parse(response[:body])
86
+
87
+ if negative
88
+ expect(actual).not_to eq(expected)
89
+ else
90
+ expect(actual).to eq(expected)
91
+ end
92
+ end
93
+
94
+ Then /^the JSON response should( not)? be:$/ do |negative, expected_response|
95
+ expected = JSON.parse(expected_response)
96
+ actual = JSON.parse(response[:body])
97
+
98
+ if negative
99
+ expect(actual).not_to eq(expected)
100
+ else
101
+ expect(actual).to eq(expected)
102
+ end
103
+ end
@@ -0,0 +1,7 @@
1
+ CAPTURE_INTEGER = Transform /^(-?\d+)$/ do |number_string|
2
+ number_string.to_i
3
+ end
4
+
5
+ CAPTURE_FLOAT = Transform /^(-?[\d.]+)$/ do |number_string|
6
+ number_string.to_f
7
+ end
@@ -0,0 +1,5 @@
1
+ module Cucumber
2
+ module Http
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'world_extensions/json_spec_interface'
2
+ require_relative 'world_extensions/url'
3
+ require_relative 'world_extensions/headers'
4
+ require_relative 'world_extensions/parameters'
5
+ require_relative 'world_extensions/payload'
6
+ require_relative 'world_extensions/request'
7
+ require_relative 'world_extensions/response'
8
+
9
+ extend Cucumber::Http::Url
10
+ extend Cucumber::Http::Headers
11
+ extend Cucumber::Http::Parameters
12
+ extend Cucumber::Http::Payload
13
+ extend Cucumber::Http::Request
14
+ extend Cucumber::Http::Response
@@ -0,0 +1,23 @@
1
+ module Cucumber
2
+ module Http
3
+ module Headers
4
+ def headers
5
+ @headers ||= {}
6
+ end
7
+
8
+ def add_header(key, value)
9
+ headers[key.to_sym] = value
10
+ end
11
+
12
+ def remove_header(key)
13
+ headers.tap { |hdrs| hdrs.delete(key.to_sym)}
14
+ end
15
+
16
+ def clear_headers
17
+ headers.clear
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ World(Cucumber::Http::Headers)
@@ -0,0 +1,11 @@
1
+ module JsonSpecInterface
2
+ def last_json
3
+ response[:body]
4
+ end
5
+
6
+ def resolve(string)
7
+ JsonSpec.remember(string)
8
+ end
9
+ end
10
+
11
+ World(JsonSpecInterface)
@@ -0,0 +1,23 @@
1
+ module Cucumber
2
+ module Http
3
+ module Parameters
4
+ def parameters
5
+ @parameters ||= {}
6
+ end
7
+
8
+ def add_parameter(key, value)
9
+ parameters[key.to_sym] = value
10
+ end
11
+
12
+ def remove_parameter(key)
13
+ parameters.tap { |pars| pars.delete(key.to_sym)}
14
+ end
15
+
16
+ def clear_parameters
17
+ parameters.clear
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ World(Cucumber::Http::Parameters)
@@ -0,0 +1,19 @@
1
+ module Cucumber
2
+ module Http
3
+ module Payload
4
+ def payload
5
+ @payload ||= ''
6
+ end
7
+
8
+ def set_payload(data)
9
+ payload.replace(data)
10
+ end
11
+
12
+ def clear_payload
13
+ payload.clear
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ World(Cucumber::Http::Payload)
@@ -0,0 +1,28 @@
1
+ require 'rest-client'
2
+
3
+ module Cucumber
4
+ module Http
5
+ module Request
6
+ def perform_request(method, path)
7
+ add_header('params', parameters)
8
+
9
+ begin
10
+ r = RestClient::Request.execute(
11
+ method: method.downcase,
12
+ url: path,
13
+ headers: headers,
14
+ payload: payload
15
+ )
16
+ rescue RestClient::Exception => e
17
+ r = e.response
18
+ end
19
+
20
+ set_response('status', r.code)
21
+ set_response('body', r.body)
22
+ set_response('headers', r.headers)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ World(Cucumber::Http::Request)
@@ -0,0 +1,19 @@
1
+ module Cucumber
2
+ module Http
3
+ module Response
4
+ def response
5
+ @response ||= {}
6
+ end
7
+
8
+ def set_response(key, value)
9
+ response[key.to_sym] = value
10
+ end
11
+
12
+ def clear_response
13
+ response.clear
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ World(Cucumber::Http::Response)
@@ -0,0 +1,19 @@
1
+ module Cucumber
2
+ module Http
3
+ module Url
4
+ def url
5
+ @url ||= ''
6
+ end
7
+
8
+ def set_url(string)
9
+ url.replace(string)
10
+ end
11
+
12
+ def clear_url
13
+ url.clear
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ World(Cucumber::Http::Url)
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cucumber-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristof Willaert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-09-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cucumber
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json_spec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.1'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.1'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rest-client
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.8'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ - !ruby/object:Gem::Dependency
63
+ name: nokogiri
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.6'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.6'
78
+ - !ruby/object:Gem::Dependency
79
+ name: faker
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '1.6'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '1.6'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '10.0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '10.0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: bundler
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '1.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '1.9'
126
+ description: Cucumber steps to easily test external XML and JSON APIs
127
+ email:
128
+ - kristof.willaert@cultuurnet.be
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - lib/cucumber/http/benchmark_steps.rb
134
+ - lib/cucumber/http/debug_steps.rb
135
+ - lib/cucumber/http/hooks.rb
136
+ - lib/cucumber/http/http_steps.rb
137
+ - lib/cucumber/http/transforms.rb
138
+ - lib/cucumber/http/version.rb
139
+ - lib/cucumber/http/world_extensions/headers.rb
140
+ - lib/cucumber/http/world_extensions/json_spec_interface.rb
141
+ - lib/cucumber/http/world_extensions/parameters.rb
142
+ - lib/cucumber/http/world_extensions/payload.rb
143
+ - lib/cucumber/http/world_extensions/request.rb
144
+ - lib/cucumber/http/world_extensions/response.rb
145
+ - lib/cucumber/http/world_extensions/url.rb
146
+ - lib/cucumber/http/world_extensions.rb
147
+ - lib/cucumber/http.rb
148
+ - README.md
149
+ homepage: https://github.com/cultuurnet/cucumber-http
150
+ licenses:
151
+ - MIT
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: 1.9.3
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ! '>='
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 1.8.23.2
171
+ signing_key:
172
+ specification_version: 3
173
+ summary: Cucumber steps to easily test XML and JSON APIs
174
+ test_files: []