serverspec-extended-types 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- N2QzMDBhYzA5MjU0M2RkOTA3MGU1ZTgxNDFlMzg3ZWE4YWQyOWQ2OQ==
4
+ Yzc2OWUzM2M5NDljNTNkM2I1ZjE5ZWUwYjZlMzUzM2NhNTAyYjliMg==
5
5
  data.tar.gz: !binary |-
6
- OWYxYTE2NGM5NmExZWNhY2JlYWZjNmZhYThlNDdjZTFiMzM4YTZjNA==
6
+ OGRiMjQ5NWZhNGMxZjg4MzQ2ZmM0MDA1MTllM2M4NDUzZWE5ZWU4Yw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTk1ZTA4NzQ4NzQ4YTM0YmQ4MTE0ZTQ5ZTEwNjgxZThhNzExODk0MGM4MDg0
10
- MjcyN2NkNDRhMTJlNmE1ZGE4YmUxNTJmZGE3NDY2ZjQyM2E0YmEyZDA0Nzlk
11
- ZDE2YjEzYjJlMTVjNTU5YWUyN2YzNzIwZjBjMzFhZmJhYWFlZGM=
9
+ YWE2ZWRlOGZmNzlkZjM0OWIwODZhODUwMmQzNzkxNjYwYWJjYjFhZWUxNTYw
10
+ ZjA0NmVjNTkzYmQ5OTI3ZjA4ZDFkYjIxZTQwM2NjNzE0ZjNiNjJhMDAzNjBm
11
+ NzkzYjY3NDE4NDcwZGNkNmQzMmQ0ZGFjY2EzZTcxYWQ4OTQ5YzY=
12
12
  data.tar.gz: !binary |-
13
- ZTczY2FlZTQxZWUxNDkwNDVmMzA3MjA4MmU2YzIxMzdjZTQ3ODFjZTg2ODYx
14
- YWMxNzU4ODE2Y2UxMTE0NWY0NTE5MjhiNWQwYjJhOTAyYTE2NTcwNzdjYzk4
15
- NzgxYTZhYWZmMmRkNDQ1YjBmZTQ0NTFjZWFlZTY2M2ZkYTJlNmY=
13
+ OWFhMTJmOTE5YzU3NDI3ODZmNTZkZjQyNGFhMTk4NzQ4MjZmMDQ3Yzc0ODRi
14
+ ZTUzMTJkMmM5YjFjYzllNjZhMWQ2ZWY1MmQwNjQ0YzU3ZGIwMWVlNTgyNjc0
15
+ YzM1ODRkYTRjYzZhOWVmMmRjMGJmZjY4NTJlNzEwMDM0NTY1NmQ=
data/CHANGES.md CHANGED
@@ -1,9 +1,13 @@
1
1
  # serverspec-extended-types, changelog
2
2
 
3
- ## 0.0.1 2015-03-14 Jason Antman <jason@jasonantman.com>
3
+ ## 0.0.3 2015-03-23 Jason Antman <jason@jasonantman.com>
4
4
 
5
- * Initial release with bitlbee, http_get and virtualenv types
5
+ * Add ``json`` matcher for http_get type.
6
6
 
7
7
  ## 0.0.2 2015-03-14 Jason Antman <jason@jasonantman.com>
8
8
 
9
9
  * Minor bugfix in Serverspec::Types::Virtualenv#virtualenv?
10
+
11
+ ## 0.0.1 2015-03-14 Jason Antman <jason@jasonantman.com>
12
+
13
+ * Initial release with bitlbee, http_get and virtualenv types
data/README.md CHANGED
@@ -17,7 +17,8 @@ Current types include:
17
17
  * A ``virtualenv`` type to make expectations about a Python Virtualenv, its ``pip`` and ``python`` versions, and packages
18
18
  installed in it. This also works for system-wide Python if ``pip`` is installed.
19
19
  * A ``http_get`` type to perform an HTTP GET request (specifying a port number, the ``Host:`` header to set, and the path
20
- to request) and make expectations about whether the request times out, its status code, headers, and response body.
20
+ to request) and make expectations about whether the request times out, its status code, headers, response body, and
21
+ JSON content (if the response body is parsable as JSON).
21
22
  * A ``bitlbee`` type to actually connect via IRC to a running [bitlbee](http://www.bitlbee.org/) IRC gateway and authenticate
22
23
  to it, and make expectations that the connection and authentication worked, and what version of Bitlbee is running.
23
24
 
@@ -141,6 +142,15 @@ Returns the HTTP response headers as a hash.
141
142
  its(:headers) { should include('HeaderName' => /value regex/) }
142
143
  end
143
144
 
145
+ ##### json
146
+
147
+ If the response body is deserializable as JSON, contains the JSON hash,
148
+ otherwise an empty hash.
149
+
150
+ describe http_get(80, 'myhostname', '/') do
151
+ its(:json) { should include('Key' => /value regex/) }
152
+ end
153
+
144
154
  ##### status
145
155
 
146
156
  Returns the HTTP status code, or 0 if timed out.
@@ -12,6 +12,7 @@
12
12
  require 'timeout'
13
13
  require 'faraday'
14
14
  require 'serverspec_extended_types/version'
15
+ require 'json'
15
16
 
16
17
  module Serverspec
17
18
  module Type
@@ -46,6 +47,7 @@ module Serverspec
46
47
  @content_str = nil
47
48
  @headers_hash = nil
48
49
  @response_code_int = nil
50
+ @response_json = nil
49
51
  begin
50
52
  Timeout::timeout(timeout_sec) do
51
53
  getpage
@@ -75,6 +77,12 @@ module Serverspec
75
77
  response.headers.each do |header, val|
76
78
  @headers_hash[header] = val
77
79
  end
80
+ # try to JSON decode
81
+ begin
82
+ @response_json = JSON.parse(@content_str)
83
+ rescue
84
+ @response_json = {}
85
+ end
78
86
  end
79
87
 
80
88
  # Whether or not the request timed out
@@ -103,6 +111,19 @@ module Serverspec
103
111
  @headers_hash
104
112
  end
105
113
 
114
+ # Returns the decoded JSON response, or an empty hash
115
+ #
116
+ # @example
117
+ # describe http_get(80, 'myhostname', '/') do
118
+ # its(:json) { should include('foo' => /value regex/) }
119
+ # end
120
+ #
121
+ # @api public
122
+ # @return [Hash]
123
+ def json
124
+ @response_json
125
+ end
126
+
106
127
  # Returns the HTTP status code, or 0 if timed out
107
128
  #
108
129
  # @example
@@ -1,3 +1,3 @@
1
1
  module ServerspecExtendedTypes
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -25,6 +25,7 @@ describe 'http_get()' do
25
25
  expect(x.instance_variable_get(:@content_str)).to be nil
26
26
  expect(x.instance_variable_get(:@headers_hash)).to be nil
27
27
  expect(x.instance_variable_get(:@response_code_int)).to be nil
28
+ expect(x.instance_variable_get(:@response_json)).to be nil
28
29
  end
29
30
  it 'calls getpage' do
30
31
  expect_any_instance_of(Serverspec::Type::Http_Get).to receive(:getpage).once
@@ -70,6 +71,31 @@ describe 'http_get()' do
70
71
  expect(x.body).to eq 'foo bar'
71
72
  expected_headers = {'h1' => 'h1val', 'h2' => 'h2val'}
72
73
  expect(x.headers).to eq expected_headers
74
+ expect(x.json).to be_empty
75
+ expect(x.json).to be_a_kind_of(Hash)
76
+ end
77
+ it 'sets JSON if parsable' do
78
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'myhost'))
79
+ conn = double
80
+ headers = double
81
+ expect(headers).to receive(:[]=).with(:user_agent, %r"Serverspec::Type::Http_Get/\d+\.\d+\.\d+ \(https://github.com/jantman/serverspec-extended-types\)")
82
+ expect(conn).to receive(:headers).and_return(headers)
83
+ expect(headers).to receive(:[]=).with(:Host, 'hostheader')
84
+ expect(conn).to receive(:headers).and_return(headers)
85
+ response = double
86
+ expect(response).to receive(:status).and_return(200)
87
+ expect(response).to receive(:body).and_return('{"foo": "bar", "baz": {"blam": "blarg"}}')
88
+ expect(response).to receive(:headers).and_return({'h1' => 'h1val', 'h2' => 'h2val'})
89
+ expect(conn).to receive(:get).with('mypath').and_return(response)
90
+ expect(Faraday).to receive(:new).with("http://myhost:1/").and_return(conn)
91
+ x = http_get(1, 'hostheader', 'mypath')
92
+ expect(x.timed_out?).to eq false
93
+ expect(x.status).to eq 200
94
+ expect(x.body).to eq '{"foo": "bar", "baz": {"blam": "blarg"}}'
95
+ expected_headers = {'h1' => 'h1val', 'h2' => 'h2val'}
96
+ expect(x.headers).to eq expected_headers
97
+ expected_json = {"foo" => "bar", "baz" => {"blam" => "blarg"}}
98
+ expect(x.json).to eq expected_json
73
99
  end
74
100
  end
75
101
  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.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Antman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-14 00:00:00.000000000 Z
11
+ date: 2015-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: serverspec