serverspec-extended-types 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7323a24b2db29288fb1c5b48d10444b3febf582d
4
- data.tar.gz: 8c6cc0312da2d38b244a933cba6b930c6f67d779
3
+ metadata.gz: 43a6ac8ec254f443ee6fff4fe712a2144c673e4b
4
+ data.tar.gz: 01ee9a58e00b00064cd3d5565f6aca55f276b4f0
5
5
  SHA512:
6
- metadata.gz: de53147b38a00edfd945ed14569e0713fc2045b11ab798cfb1a0431e9fafa4df904f0fcce1256204ed64fcd3d23340ff9d3fe5e3772c9d3347b02bf9fcb4f57d
7
- data.tar.gz: 64026f353890298a85bb60b59e0fb2dfe288e042166936002c4fe7938862a72f1f454130398dc19c2c9eeb7f6c64233376b2eba7e90e0d04aafd6af519daea58
6
+ metadata.gz: 5d647e721a7d0502c79c2ab79108660241d992dbddb5a1c7ce4fc401f075e66bc02f004ab15314181f45f715a0f48c7df14cbeb8527a01f2eeea3c9b125ca041
7
+ data.tar.gz: 3595abbf99f225406d923b144cec2470a4ee891d184b39050d979fd27e08ea34b9fe021bd6a6ab8f26e8e02c884e6f3ac167b7356b4c783bc067b2cf44da4cf4
data/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # serverspec-extended-types, changelog
2
2
 
3
+ ## 0.1.1 2017-07-28 ojizero <z.acnts.816@protonmail.ch>
4
+
5
+ * Add `redirected_to` and `redirected` matchers for http_get type.
6
+
3
7
  ## 0.1.0 2017-05-15 Jason Antman <jason@jasonantman.com>
4
8
 
5
9
  * Add `protocol` (http|https) and `bypass_ssl_verify` parameters for http_get.
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  [![Gem Version](https://img.shields.io/gem/v/serverspec-extended-types.svg)](https://rubygems.org/gems/serverspec-extended-types)
7
7
  [![Total Downloads](https://img.shields.io/gem/dt/serverspec-extended-types.svg)](https://rubygems.org/gems/serverspec-extended-types)
8
8
  [![Github Issues](https://img.shields.io/github/issues/jantman/serverspec-extended-types.svg)](https://github.com/jantman/serverspec-extended-types/issues)
9
- [![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/0.1.0/active.svg)](http://www.repostatus.org/#active)
9
+ [![Project Status: Inactive The project has reached a stable, usable state but is no longer being actively developed; support/maintenance will be provided as time allows.](http://www.repostatus.org/badges/2.0.0/inactive.svg)](http://www.repostatus.org/#inactive)
10
10
 
11
11
  serverspec-extended-types provides some purpose-specific types to be used with [serverspec](http://serverspec.org/) 2.x for
12
12
  testing various things on a host, as well as some types for high-level integration tests that make actual requests against
@@ -134,7 +134,7 @@ Returns the String body content of the HTTP response.
134
134
 
135
135
  describe http_get(80, 'myhostname', '/') do
136
136
  its(:body) { should match /<html>/ }
137
- end
137
+ end
138
138
 
139
139
  ##### headers
140
140
 
@@ -167,7 +167,27 @@ True if the request timed out, false otherwise.
167
167
 
168
168
  describe http_get(80, 'myhostname', '/') do
169
169
  it { should_not be_timed_out }
170
- end
170
+ end
171
+
172
+ ##### redirected?
173
+
174
+ True if the request was redirected, false otherwise.
175
+
176
+ describe http_get(80, 'myhostname', '/') do
177
+ it { should be_redirected }
178
+ end
179
+
180
+ ##### redirected_to?
181
+
182
+ True if the request was redirected to the specified location, false otherwise.
183
+
184
+ describe http_get(80, 'myhostname', '/') do
185
+ it { should be_redirected_to 'https://myhostname/newpath' }
186
+ end
187
+
188
+ describe http_get(80, 'myhostname', '/') do
189
+ it { should be_redirected_to 'http://mynewhost/' }
190
+ end
171
191
 
172
192
  ### virtualenv
173
193
 
@@ -20,6 +20,8 @@ module Serverspec
20
20
  # Perform an HTTP GET request against the serverspec target
21
21
  # using {http://www.rubydoc.info/gems/faraday/ Faraday}.
22
22
  class Http_Get < Base
23
+ # Http status codes that are tested against for redirect test
24
+ @@redirect_codes = [301, 302, 307, 308]
23
25
 
24
26
  # Initialize a bunch of instance variables, then call {#getpage}
25
27
  #
@@ -52,6 +54,8 @@ module Serverspec
52
54
  @response_json = nil
53
55
  @protocol = protocol
54
56
  @bypass_ssl_verify = bypass_ssl_verify
57
+ @redirects = false
58
+ @redirect_path = nil
55
59
  begin
56
60
  Timeout::timeout(timeout_sec) do
57
61
  getpage
@@ -84,6 +88,8 @@ module Serverspec
84
88
  response.headers.each do |header, val|
85
89
  @headers_hash[header] = val
86
90
  end
91
+ @redirects = @@redirect_codes.include? @response_code_int
92
+ @redirect_path = @redirects ? @headers_hash['location'] : nil
87
93
  # try to JSON decode
88
94
  begin
89
95
  @response_json = JSON.parse(@content_str)
@@ -130,7 +136,7 @@ module Serverspec
130
136
  def json
131
137
  @response_json
132
138
  end
133
-
139
+
134
140
  # Returns the HTTP status code, or 0 if timed out
135
141
  #
136
142
  # @example
@@ -161,6 +167,32 @@ module Serverspec
161
167
  @content_str
162
168
  end
163
169
 
170
+ # Whether or not it redirects to some other page
171
+ #
172
+ # @example
173
+ # describe http_get(80, 'myhostname', '/') do
174
+ # it { should be_redirected_to 'https://myhostname/' }
175
+ # end
176
+ #
177
+ # @api public
178
+ # @return [Boolean]
179
+ def redirected_to? (redirect_path)
180
+ @redirects and @redirect_path == redirect_path
181
+ end
182
+
183
+ # Whether or not it redirects to any other page
184
+ #
185
+ # @example
186
+ # describe http_get(80, 'myhostname', '/') do
187
+ # it { should be_redirected }
188
+ # end
189
+ #
190
+ # @api public
191
+ # @return [Boolean]
192
+ def redirected?
193
+ @redirects
194
+ end
195
+
164
196
  private :getpage
165
197
  end
166
198
 
@@ -1,3 +1,3 @@
1
1
  module ServerspecExtendedTypes
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -96,9 +96,9 @@ describe 'Serverspec::Type::Bitlbee' do
96
96
  expect(sslsock_dbl).to receive(:puts).ordered.with("QUIT :\"outta here\"\n")
97
97
  expect(sslsock_dbl).to receive(:close).ordered
98
98
  expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(sock_dbl, sslctx_dbl).and_return(sslsock_dbl)
99
- #expect_any_instance_of(Serverspec::Type::Bitlbee).to receive(:communicate)
99
+ # expect_any_instance_of(Serverspec::Type::Bitlbee).to receive(:communicate)
100
100
  x = bitlbee(1, 'foo', 'bar')
101
- x.stub(:communicate) do
101
+ allow(x).to receive(:communicate) do
102
102
  x.instance_variable_get(:@socket).puts("communicate")
103
103
  end
104
104
  x.connect_ssl
@@ -114,7 +114,7 @@ describe 'Serverspec::Type::Bitlbee' do
114
114
  expect(sock_dbl).to receive(:puts).ordered.with("QUIT :\"outta here\"\n")
115
115
  expect(sock_dbl).to receive(:close).ordered
116
116
  x = bitlbee(1, 'foo', 'bar')
117
- x.stub(:communicate) do
117
+ allow(x).to receive(:communicate) do
118
118
  x.instance_variable_get(:@socket).puts("communicate")
119
119
  end
120
120
  x.connect
@@ -28,6 +28,8 @@ describe 'http_get()' do
28
28
  expect(x.instance_variable_get(:@response_json)).to be nil
29
29
  expect(x.instance_variable_get(:@protocol)).to eq 'http'
30
30
  expect(x.instance_variable_get(:@bypass_ssl_verify)).to eq false
31
+ expect(x.instance_variable_get(:@redirects)).to eq false
32
+ expect(x.instance_variable_get(:@redirect_path)).to be nil
31
33
  end
32
34
  it 'calls getpage' do
33
35
  expect_any_instance_of(Serverspec::Type::Http_Get).to receive(:getpage).once
@@ -125,5 +127,55 @@ describe 'http_get()' do
125
127
  expected_json = {"foo" => "bar", "baz" => {"blam" => "blarg"}}
126
128
  expect(x.json).to eq expected_json
127
129
  end
130
+ it 'without redirects' do
131
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'myhost'))
132
+
133
+ headers = double('header')
134
+ conn = double('conn', headers: headers)
135
+
136
+ expect(headers).to receive(:[]=).with(:user_agent, %r"Serverspec::Type::Http_Get/\d+\.\d+\.\d+ \(https://github.com/jantman/serverspec-extended-types\)")
137
+ expect(headers).to receive(:[]=).with(:Host, 'hostheader')
138
+
139
+ response = double('resp', status: 200, body: 'OK', headers: ({}))
140
+
141
+ expect(conn).to receive(:get).with('mypath').and_return(response)
142
+
143
+ expect(Faraday).to receive(:new).with('http://myhost:1/').and_return(conn)
144
+ x = http_get(1, 'hostheader', 'mypath')
145
+
146
+ # test internal parameters
147
+ expect(x.instance_variable_get(:@redirects)).to eq false
148
+ expect(x.instance_variable_get(:@redirect_path)).to be nil
149
+
150
+ # test exposed API
151
+ expect(x).to_not be_redirected
152
+ expect(x).to_not be_redirected_to 'https://myhost:1/mynewpath'
153
+ end
154
+ it 'supports redirects' do
155
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'myhost'))
156
+
157
+ headers = double('header')
158
+ conn = double('conn', headers: headers)
159
+
160
+ expect(headers).to receive(:[]=).with(:user_agent, %r"Serverspec::Type::Http_Get/\d+\.\d+\.\d+ \(https://github.com/jantman/serverspec-extended-types\)")
161
+ expect(headers).to receive(:[]=).with(:Host, 'hostheader')
162
+
163
+ redirect_location = 'https://myhost:1/mynewpath'
164
+ response = double('resp', status: 301, body: 'OK', headers: {'location' => redirect_location})
165
+
166
+ expect(conn).to receive(:get).with('mypath').and_return(response)
167
+
168
+ expect(Faraday).to receive(:new).with('http://myhost:1/').and_return(conn)
169
+ x = http_get(1, 'hostheader', 'mypath')
170
+
171
+ # test internal parameters
172
+ expect(x.instance_variable_get(:@redirects)).to eq true
173
+ expect(x.instance_variable_get(:@redirect_path)).to_not be nil
174
+ expect(x.instance_variable_get(:@redirect_path)).to eq redirect_location
175
+
176
+ # test exposed API
177
+ expect(x).to be_redirected
178
+ expect(x).to be_redirected_to redirect_location
179
+ end
128
180
  end
129
181
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serverspec-extended-types
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Antman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-16 00:00:00.000000000 Z
11
+ date: 2017-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: serverspec