http_baseline 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a1d502502a3e40a4e37ea2de44c208703d4c457a
4
+ data.tar.gz: 0e1b7fc0fe7dbea7217d1921e94f0941b2282e32
5
+ SHA512:
6
+ metadata.gz: b9abdf2f661b21114e8a5c02c4c5978aadcb74df3ad919a5c6d072222270093e60c6d66c6ef03a4c31d1feb25126da561632e519b99e16334a88e073ab988e1e
7
+ data.tar.gz: 2eafae1ccdd1e727e74cf00afd74ad24c44fbebc6d791e651b6ba64c3f551ed809c1a823d5540c688a67fafb0e4ab3a4c03eb84b44731965fd9c1dfbfd190567
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ rspec.xml
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 rlishtaba
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # HTTP
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'http'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install http
18
+
19
+ ## Usage in terms of Cucumber
20
+
21
+ Here is :
22
+
23
+ ### Steps examples for HTTP headers:
24
+
25
+ Given I set headers:
26
+ |Set-Cookie| my secret cookie |
27
+
28
+ Given I send and accept JSON
29
+
30
+ Given I send and accept XML
31
+
32
+ Given I send and accept HTML
33
+
34
+ Given the HTTP response should have header "Set-Cookie" with value "server side secret cookie"
35
+
36
+ Given the HTTP response should have headers:
37
+ | Accept | text/xml |
38
+
39
+ Given the JSON response should have default headers
40
+
41
+
42
+ ### Steps to handle requests or responses:
43
+
44
+ Given I send a :put request to "/open"
45
+
46
+ Given I send a :put request to "/open" with the following:
47
+ |timeout| 15 |
48
+
49
+ Then the response status should be "404"
50
+
51
+ And the JSON response should have "$..Message" with the text "Invalid communication setting"
52
+
53
+ And the JSON response should have "$..Message" with the regexp "Invalid communication setting.+?"
54
+
55
+ And the XML response should have "Message" with the text "Invalid communication setting"
56
+
57
+ Then show me the response
58
+
59
+ Then show me the headers
60
+
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.rspec_opts = '--format RspecJunitFormatter --out rspec.xml --tag ~online'
6
+ t.verbose = true
7
+ end
8
+
9
+ require 'cucumber/rake/task'
10
+ Cucumber::Rake::Task.new do |t|
11
+ t.profile = 'default'
12
+ end
13
+
14
+ task :default => [:spec, :cucumber]
data/cucumber.yml ADDED
@@ -0,0 +1,7 @@
1
+ <%
2
+ defaults = ['--require', 'features/support', '--require', 'features/step_definitions', 'features'].join(' ')
3
+ %>
4
+
5
+ ruby_1_9: <%= defaults %>
6
+ ruby_2_0: <%= defaults %>
7
+ default: <%= defaults %>
@@ -0,0 +1,34 @@
1
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
2
+ #
3
+ # Example:
4
+ #
5
+ # * show me the response
6
+ #
7
+ Then /^show me the response$/ do
8
+ $stdout.puts '------------- show me the response ------------- '
9
+
10
+ if @response.headers['Content-Type'] =~ /json/
11
+ $stdout.puts JSON.pretty_generate(@response.body)
12
+ elsif @response.headers['Content-Type'] =~ /xml/
13
+ $stdout.puts Nokogiri::XML(@response.body.to_s)
14
+ else
15
+ $stdout.puts @response.headers.to_hash
16
+ $stdout.puts @response.body
17
+ end
18
+
19
+ $stdout.puts '------------- end of show me the response ------ '
20
+ end
21
+
22
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
23
+ #
24
+ # Example:
25
+ #
26
+ # * show me the headers
27
+ #
28
+ Then /^show me the headers/ do
29
+ $stdout.puts '------------- show me the headers ------------- '
30
+
31
+ $stdout.puts JSON.pretty_generate @response.headers
32
+
33
+ $stdout.puts '------------- end of show me the headers ------ '
34
+ end
@@ -0,0 +1,56 @@
1
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
2
+ #
3
+ # Example:
4
+ #
5
+ # * I set headers:
6
+ # | Content-Type | application/json |
7
+ # | X-roam-auth | token |
8
+ #
9
+ Given /^I set headers:$/ do |headers|
10
+ headers.rows_hash.each { |k, v| connection.add_header k, v }
11
+ end
12
+
13
+
14
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
15
+ #
16
+ # Example:
17
+ #
18
+ # * I send and accept JSON
19
+ # * I send and accept XML
20
+ # * I send and accept HTML
21
+ #
22
+ Given /^I send and accept (XML|JSON|HTML)$/ do |type|
23
+ case type
24
+ when 'XML', 'JSON'
25
+ connection.add_header 'Accept', "application/#{type.downcase}"
26
+ connection.add_header 'Content-Type', "application/#{type.downcase}"
27
+ when 'HTML'
28
+ connection.add_header 'Accept', 'text/html'
29
+ else
30
+ nil
31
+ end
32
+ end
33
+
34
+
35
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
36
+ #
37
+ # Example:
38
+ #
39
+ # * the HttpBaseline response should have headers:
40
+ # | Accept | text/xml |
41
+ #
42
+ Then /^the HttpBaseline response should have headers:$/ do |table|
43
+ hashed = table.rows_hash
44
+
45
+ hashed.each_pair do |key, expected|
46
+
47
+ actual = @response.headers[key.downcase]
48
+
49
+ if self.respond_to?(:should)
50
+ actual.should == expected
51
+ else
52
+ assert_equal actual, expected
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,69 @@
1
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
2
+ #
3
+ # Example
4
+ #
5
+ # * I send a :get request to "/user/login" with the following:
6
+ # | userId | user |
7
+ # | password | password |
8
+ #
9
+ When /^I send a :(get|post|put|delete) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |*args|
10
+ request_type = args.shift
11
+ path = args.shift
12
+ input = args.shift
13
+ request_opts = {}
14
+
15
+ unless input.nil?
16
+ if input.class == Cucumber::Ast::Table # table
17
+ request_opts.merge!(input.rows_hash)
18
+ else # lines
19
+ request_opts.merge!(input)
20
+ end
21
+ end
22
+
23
+ request_opts.each do |key, value|
24
+ if value =~ /\@default_(token|user|password)/
25
+ request_opts[key] = HttpBaseline.config.comm.http.send($1)
26
+ end
27
+ end
28
+
29
+ transmit(request_type, path, request_opts)
30
+ end
31
+
32
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
33
+ #
34
+ # Example:
35
+ #
36
+ # * I send Form Data via :post request to "/user/changePassword" with the following:
37
+ # | userId | user |
38
+ # | oldPassword | fibonacci_123 |
39
+ # | newPassword | fibonacci_12358 |
40
+ #
41
+ #
42
+ Given /^I send Form Data via :(post|put) request (?:for|to) "([^"]*)" with the following:$/ do |request_type, path, raw_table|
43
+ request_opts = sub_to_default_credentials(raw_table.rows_hash.dup)
44
+
45
+ step 'I set headers:', table('| Content-Type | application/x-www-form-urlencoded |')
46
+
47
+ transmit(request_type, path, request_opts.to_query)
48
+ end
49
+
50
+
51
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
52
+ #
53
+ # Example:
54
+ #
55
+ # * I send Form Data via :post request to "/user/changePassword" with the following raw body:
56
+ # """
57
+ # {
58
+ # "hello": "world"
59
+ # }
60
+ # """
61
+ When /^I send a :(post|put) request (?:for|to) "([^"]*)" with the following raw body:$/ do |*args|
62
+ request_type = args.shift
63
+ path = args.shift
64
+ body = args.shift
65
+
66
+ set_headers_by(body.content_type)
67
+
68
+ transmit(request_type, path, body.lines.to_a.join("\n").dup)
69
+ end
@@ -0,0 +1,133 @@
1
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
2
+ #
3
+ # Example:
4
+ #
5
+ # * the response code should be "404"
6
+ #
7
+ Then /^the response (code|status) should be "([^"]*)"$/ do |_, code|
8
+ if self.respond_to? :should
9
+ @response.status.should == code.to_i
10
+ else
11
+ assert_equal code, @response.code
12
+ end
13
+ end
14
+
15
+
16
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
17
+ #
18
+ # Example:
19
+ #
20
+ # * the JSON response should have "userId" matching with "[a-zA-Z0-9_-]{3,50}"
21
+ #
22
+ Then /^the JSON response should (not)?\s?have "([^"]*)" matching with "([^"]*)"$/ do |*options|
23
+ negative = options.shift
24
+ json_path = options.shift
25
+ regexp_string = options.shift
26
+ regexp = Regexp.compile("^#{regexp_string}$", Regexp::IGNORECASE)
27
+ raw_json = @response.body
28
+ results = JsonPath.new(json_path).on(raw_json).to_a.map(&:to_s)
29
+
30
+ if !negative.nil?
31
+ results.first.should_not =~ regexp
32
+ else
33
+ results.first.should =~ regexp
34
+ end
35
+
36
+ end
37
+
38
+
39
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
40
+ #
41
+ # Example:
42
+ #
43
+ # * the JSON response should have "errCode" with the text "SYSTEM_ERROR"
44
+ #
45
+ Then /^the JSON response should (not)?\s?have "([^"]*)" with the text "([^"]*)"$/ do |negative, json_path, text|
46
+ json = @response.body
47
+ results = JsonPath.new(json_path).on(json).to_a.map(&:to_s)
48
+
49
+ if self.respond_to?(:should)
50
+ if negative.nil?
51
+ results.should include(text)
52
+ else
53
+ results.should_not include(text)
54
+ end
55
+ else
56
+ if negative.nil?
57
+ assert results.include?(text)
58
+ else
59
+ assert !results.include?(text)
60
+ end
61
+ end
62
+ end
63
+
64
+
65
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
66
+ #
67
+ # Example:
68
+ #
69
+ # * the XML response should have "errCode" with the text "SYSTEM_ERROR"
70
+ #
71
+ Then /^the XML response should have "([^"]*)" with the text "([^"]*)"$/ do |xpath, text|
72
+ parsed_response = Nokogiri::XML(@response.body.to_s)
73
+ elements = parsed_response.xpath(xpath)
74
+ if self.respond_to?(:should)
75
+ elements.should_not be_empty, "could not find #{xpath} in:\n#{@response.body.to_s}"
76
+ elements.find { |e| e.text == text }.should_not be_nil, "found elements but could not find #{text} in:\n#{elements.inspect}"
77
+ else
78
+ assert !elements.empty?, "could not find #{xpath} in:\n#{@response.body.to_s}"
79
+ assert elements.find { |e| e.text == text }, "found elements but could not find #{text} in:\n#{elements.inspect}"
80
+ end
81
+ end
82
+
83
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
84
+ #
85
+ # Example:
86
+ #
87
+ # * the JSON response should match:
88
+ # """
89
+ # {
90
+ # "tspUser": {
91
+ # "address": {
92
+ # "address1": "aaa",
93
+ # "address2": "bbb",
94
+ # "address3": "ccc",
95
+ # "city": "Boston",
96
+ # "country": "US",
97
+ # "fullAddress": "fullAddress",
98
+ # "id": "909",
99
+ # "postal": "200231",
100
+ # "state": "MA"
101
+ # }
102
+ # }
103
+ # }
104
+ # """
105
+ #
106
+ Then 'the JSON response should match:' do |json|
107
+ expected = JSON.parse(json)
108
+ actual = @response.body
109
+
110
+ if self.respond_to?(:should)
111
+ actual.should == expected
112
+ else
113
+ assert_equal actual, response
114
+ end
115
+ end
116
+
117
+
118
+ # * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
119
+ #
120
+ # Example:
121
+ #
122
+ # * the JSON response should have "address" node with a elements counter matching to "10"
123
+ #
124
+ #
125
+ Then /^the JSON response should have "([^"]*)" node with a elements counter matching to "(\d+)"$/ do |json_path, length|
126
+ json = @response.body
127
+ results = JsonPath.new(json_path).on(json)
128
+ if self.respond_to?(:should)
129
+ results.length.should == length.to_i
130
+ else
131
+ assert_equal length.to_i, results.length
132
+ end
133
+ end
@@ -0,0 +1,11 @@
1
+ Given /^I am logged in with the following credentials:$/ do |raw_table|
2
+ step 'I send a :get request to "/user/login" with the following:', raw_table
3
+ step 'the JSON response should have "status" with the text "success"'
4
+ end
5
+
6
+ Given /^I am logged in$/ do
7
+ login_to_web_service
8
+ step 'the response code should be "200"'
9
+ step 'the JSON response should have "status" with the text "success"'
10
+ end
11
+
@@ -0,0 +1,42 @@
1
+ When /^I perform the following steps?:$/ do |step_strings|
2
+ steps = step_strings.split("\n")
3
+ steps.each { |step_string| step step_string.strip }
4
+ end
5
+
6
+ Then /^the response should equal:$/ do |response_body|
7
+ @response.body.should eq(response_body)
8
+ end
9
+
10
+ When /^I perform the following step with table:$/ do |step_definition|
11
+ lines = step_definition.split("\n")
12
+ step_string = lines.shift.strip
13
+
14
+ raw = lines.map do |line|
15
+ line.squish.gsub(/^\|/, '').gsub(/\|$/, '').squish.split("|").map(&:squish)
16
+ end
17
+
18
+ step step_string.strip, table(raw)
19
+ end
20
+
21
+ When /^I perform the following step with string:$/ do |step_definition|
22
+ lines = step_definition.split("\n")
23
+ step_string = lines.shift
24
+
25
+ param_string = lines.join("\n")
26
+
27
+ step step_string.strip, param_string
28
+ end
29
+
30
+ Then /^the request headers should be:$/ do |headers|
31
+ headers_hash = headers.rows_hash
32
+ request '/'
33
+ @stack.env.slice(*headers_hash.keys).values.should eq(headers_hash.values)
34
+ end
35
+
36
+ Then /^I should be authenticated$/ do
37
+ @response.env["HTTP_AUTHORIZATION"].should eq("Basic #{Base64.encode64("roman:lishtaba")}")
38
+ end
39
+
40
+ Then /^I should be digest authenticated$/ do
41
+ @response.env["HTTP_AUTHORIZATION"].starts_with?("Digest ").should be_true
42
+ end
@@ -0,0 +1,3 @@
1
+ require 'bundler'
2
+ require 'bundler/setup'
3
+ require 'http_baseline'
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'http_baseline/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'http_baseline'
8
+ spec.version = HttpBaseline::VERSION
9
+ spec.authors = ['Roman Lishtaba']
10
+ spec.email = %w(roman@lishtaba.com)
11
+ spec.description = %q{Baseline library}
12
+ spec.summary = %q{Baseline library for automation}
13
+ spec.homepage = 'https://github.com/rlishtaba'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = %w(env lib)
20
+
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'cucumber'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_runtime_dependency 'json'
26
+ spec.add_runtime_dependency 'builder'
27
+ spec.add_runtime_dependency 'nokogiri'
28
+ spec.add_runtime_dependency 'jsonpath'
29
+ spec.add_development_dependency 'rspec_junit_formatter'
30
+
31
+ spec.add_dependency 'faraday'
32
+ spec.add_dependency 'addressable'
33
+ spec.add_dependency 'faraday_middleware'
34
+ spec.add_dependency 'hashie'
35
+
36
+ end
@@ -0,0 +1,10 @@
1
+ class Array
2
+ # Converts an array into a string suitable for use as a URL query string,
3
+ # using the given +key+ as the param name.
4
+ #
5
+ # ['Ruby', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Ruby&hobbies%5B%5D=coding"
6
+ def to_query(key)
7
+ prefix = "#{key}[]"
8
+ collect { |value| value.to_query(prefix) }.join '&'
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ class Hash
2
+ alias_method :to_query, :to_param
3
+ end
@@ -0,0 +1,9 @@
1
+ class Object
2
+ # Converts an object into a string suitable for use as a URL query string, using the given <tt>key</tt> as the
3
+ # param name.
4
+ #
5
+ # Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.
6
+ def to_query(key)
7
+ "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
8
+ end
9
+ end
@@ -0,0 +1,58 @@
1
+ class Object
2
+ # Alias of <tt>to_s</tt>.
3
+ def to_param
4
+ to_s
5
+ end
6
+ end
7
+
8
+ class NilClass
9
+ # Returns +self+.
10
+ def to_param
11
+ self
12
+ end
13
+ end
14
+
15
+ class TrueClass
16
+ # Returns +self+.
17
+ def to_param
18
+ self
19
+ end
20
+ end
21
+
22
+ class FalseClass
23
+ # Returns +self+.
24
+ def to_param
25
+ self
26
+ end
27
+ end
28
+
29
+ class Array
30
+ # Calls <tt>to_param</tt> on all its elements and joins the result with
31
+ # slashes. This is used by <tt>url_for</tt> in Action Pack.
32
+ def to_param
33
+ collect { |e| e.to_param }.join '/'
34
+ end
35
+ end
36
+
37
+ class Hash
38
+ # Returns a string representation of the receiver suitable for use as a URL
39
+ # query string:
40
+ #
41
+ # {name: 'David', nationality: 'Danish'}.to_param
42
+ # # => "name=David&nationality=Danish"
43
+ #
44
+ # An optional namespace can be passed to enclose the param names:
45
+ #
46
+ # {name: 'David', nationality: 'Danish'}.to_param('user')
47
+ # # => "user[name]=David&user[nationality]=Danish"
48
+ #
49
+ # The string pairs "key=value" that conform the query string
50
+ # are sorted lexicographically in ascending order.
51
+ #
52
+ # This method is also aliased as +to_query+.
53
+ def to_param(namespace = nil)
54
+ collect do |key, value|
55
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
56
+ end.sort * '&'
57
+ end
58
+ end