cucumber-rest-api 0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2919988920dfbf95c04f1eff5ac0da636e6511c
4
+ data.tar.gz: c8f94e16217deda646354635b025ed1dcb734f5d
5
+ SHA512:
6
+ metadata.gz: 9c9843b9ee3cad6c3f0aebac78e9c4c9264b7b5118b1c5f23df16317aa4263aeb8d962c88026dab78066b1e2875deefbc578859004080978914c183bd2ba7753
7
+ data.tar.gz: 6a51d0c477d986489bec6aa0c749e1ee05cd620edeb407eccf6804262c17ad5ae3f6a18b421b7a67111deebfaf4db33f8b4f3f0e0494f5f0b65743650dd73b21
@@ -0,0 +1,163 @@
1
+ require 'jsonpath'
2
+ require 'nokogiri'
3
+
4
+ require 'cucumber'
5
+ require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support
6
+
7
+ require 'cucumber/rest_api/http_client.rb'
8
+
9
+ Given /^I set headers:$/ do |headers|
10
+ headers.rows_hash.each {|k,v| header k, v }
11
+ end
12
+
13
+ Given /^I send and accept (XML|JSON)$/ do |type|
14
+ header 'Accept', "application/#{type.downcase}"
15
+ header 'Content-Type', "application/#{type.downcase}"
16
+ end
17
+
18
+ Given /^I send and accept HTML$/ do
19
+ header 'Accept', "text/html"
20
+ header 'Content-Type', "application/x-www-form-urlencoded"
21
+ end
22
+
23
+ When /^I authenticate as the user "([^"]*)" with the password "([^"]*)"$/ do |user, pass|
24
+ authorize user, pass
25
+ end
26
+
27
+ When /^I digest\-authenticate as the user "(.*?)" with the password "(.*?)"$/ do |user, pass|
28
+ digest_authorize user, pass
29
+ end
30
+
31
+ When /^I send a (GET|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |*args|
32
+ request_type = args.shift
33
+ path = args.shift
34
+ input = args.shift
35
+
36
+ request_opts = {method: request_type.downcase.to_sym}
37
+
38
+ unless input.nil?
39
+ if input.class == Cucumber::Ast::Table
40
+ request_opts[:params] = input.rows_hash
41
+ else
42
+ request_opts[:input] = input
43
+ end
44
+ end
45
+
46
+ request path, request_opts
47
+ end
48
+
49
+ Then /^show me the (unparsed)?\s?response$/ do |unparsed|
50
+ if unparsed == 'unparsed'
51
+ puts last_response.body
52
+ elsif last_response.headers['Content-Type'] =~ /json/
53
+ json_response = JSON.parse(last_response.body)
54
+ puts JSON.pretty_generate(json_response)
55
+ elsif last_response.headers['Content-Type'] =~ /xml/
56
+ puts Nokogiri::XML(last_response.body)
57
+ else
58
+ puts last_response.headers
59
+ puts last_response.body
60
+ end
61
+ end
62
+
63
+ Then /^the response status should be "([^"]*)"$/ do |status|
64
+ if self.respond_to? :should
65
+ last_response.code.should == status
66
+ else
67
+ assert_equal status, last_response.code
68
+ end
69
+ end
70
+
71
+ Then /^the JSON response should (not)?\s?have "([^"]*)"$/ do |negative, json_path|
72
+ json = JSON.parse(last_response.body)
73
+ results = JsonPath.new(json_path).on(json).to_a.map(&:to_s)
74
+ if self.respond_to?(:should)
75
+ if negative.present?
76
+ results.should be_empty
77
+ else
78
+ results.should_not be_empty
79
+ end
80
+ else
81
+ if negative.present?
82
+ assert results.empty?
83
+ else
84
+ assert !results.empty?
85
+ end
86
+ end
87
+ end
88
+
89
+
90
+ Then /^the JSON response should (not)?\s?have "([^"]*)" with the text "([^"]*)"$/ do |negative, json_path, text|
91
+ json = JSON.parse(last_response.body)
92
+ results = JsonPath.new(json_path).on(json).to_a.map(&:to_s)
93
+ if self.respond_to?(:should)
94
+ if negative.present?
95
+ results.should_not include(text), "Expected #{text}, Got #{results}"
96
+ else
97
+ results.should include(text) , "Expected #{text}, Got #{results}"
98
+ end
99
+ else
100
+ if negative.present?
101
+ assert !results.include?(text), "Expected #{text}, Got #{results}"
102
+ else
103
+ assert results.include?(text), "Expected #{text}, Got #{results}"
104
+ end
105
+ end
106
+ end
107
+
108
+ Then /^the XML response should (not)?\s?have "([^"]*)"$/ do |negative, xpath|
109
+ parsed_response = Nokogiri::XML(last_response.body)
110
+ elements = parsed_response.xpath(xpath)
111
+ if self.respond_to?(:should)
112
+ if negative.present?
113
+ elements.should be_empty
114
+ else
115
+ elements.should_not be_empty
116
+ end
117
+ else
118
+ if negative.present?
119
+ assert elements.empty?
120
+ else
121
+ assert !elements.empty?
122
+ end
123
+ end
124
+ end
125
+
126
+ Then /^the XML response should have "([^"]*)" with the text "([^"]*)"$/ do |xpath, text|
127
+ parsed_response = Nokogiri::XML(last_response.body)
128
+ elements = parsed_response.xpath(xpath)
129
+ if self.respond_to?(:should)
130
+ elements.should_not be_empty, "could not find #{xpath} in:\n#{last_response.body}"
131
+ elements.find { |e| e.text == text }.should_not be_nil, "found elements but could not find #{text} in:\n#{elements.inspect}"
132
+ else
133
+ assert !elements.empty?, "could not find #{xpath} in:\n#{last_response.body}"
134
+ assert elements.find { |e| e.text == text }, "found elements but could not find #{text} in:\n#{elements.inspect}"
135
+ end
136
+ end
137
+
138
+ Then /^the JSON response should be:$/ do |json|
139
+ expected = JSON.parse(json)
140
+ actual = JSON.parse(last_response.body)
141
+
142
+ if self.respond_to?(:should)
143
+ actual.should == expected
144
+ else
145
+ assert_equal actual, response
146
+ end
147
+ end
148
+
149
+ Then(/^the JSON should have "(.*)" elements$/) do |element|
150
+ json = JSON.parse(last_response.body)
151
+ total_elements = json.count
152
+ total_elements.should == element.to_i
153
+ end
154
+
155
+ Then /^the JSON response should have "([^"]*)" with a length of (\d+)$/ do |json_path, length|
156
+ json = JSON.parse(last_response.body)
157
+ results = JsonPath.new(json_path).on(json)
158
+ if self.respond_to?(:should)
159
+ results.length.should == length.to_i
160
+ else
161
+ assert_equal length.to_i, results.length
162
+ end
163
+ end
@@ -0,0 +1,57 @@
1
+ require "net/http"
2
+ require "uri"
3
+ require 'json'
4
+
5
+ class Object
6
+
7
+ @headers == nil
8
+
9
+ def blank?
10
+ respond_to?(:empty?) ? empty? : !self
11
+ end
12
+
13
+ def present?
14
+ !blank?
15
+ end
16
+
17
+ def header key, value
18
+ if @headers == nil
19
+ @headers = Hash.new(0)
20
+ end
21
+
22
+ @headers[key] = value
23
+ end
24
+
25
+ def request path,request_opts
26
+ req = "#{$SERVER_PATH}" + path
27
+
28
+ uri = URI.parse(req)
29
+ http = Net::HTTP.new(uri.host, uri.port)
30
+
31
+ if request_opts[:method] == :post
32
+ request = Net::HTTP::Post.new(uri.request_uri)
33
+
34
+ body = nil
35
+ if request_opts[:params]
36
+ body = request_opts[:params].to_json
37
+ else
38
+ body = request_opts[:input]
39
+ end
40
+ else
41
+ request = Net::HTTP::Get.new(uri.request_uri)
42
+ end
43
+
44
+ #do we have any headers to add?
45
+ if @headers != nil
46
+ @headers.each { |k,v| request.add_field(k, v) }
47
+ @headers = nil
48
+ end
49
+
50
+ @response = http.request(request,body)
51
+ end
52
+
53
+ def last_response
54
+ return @response
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cucumber-rest-api
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Anupama Vijaykumar,Nic Jackson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jsonpath
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: cucumber
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.12.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.12.0
69
+ description: Cucumber steps to easily test REST-based XML and JSON APIs
70
+ email:
71
+ - anupama.vijaykumar@marks-and-spencer.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/cucumber/rest_api.rb
77
+ - lib/cucumber/rest_api/http_client.rb
78
+ homepage: https://github.com/DigitalInnovation/cucumber_rest_api
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: 1.9.3
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.1.11
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Cucumber steps to easily test REST-based XML and JSON APIs
102
+ test_files: []