rspec-httpd 0.0.8 → 0.0.9
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 +4 -4
- data/VERSION +1 -1
- data/lib/rspec/httpd.rb +17 -5
- data/lib/rspec/httpd/expectation_failed.rb +37 -0
- metadata +16 -2
- data/lib/rspec/httpd/expectation.rb +0 -40
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8a74da790f8631d6274b9b21573c5f100e8c407bdbafbec1f30589a15ed8a51
|
4
|
+
data.tar.gz: 709c1999177711fdbd3d3444e6539cd41a39d6be7c90c70aca71824a88226fca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1355bd07032124833b0d3df6a54328800fa6c0f20606c49634d9223d74084e8afde82e8149795ab718c0075b1f8b8cdd806d81c6334a4782688886f59448db39
|
7
|
+
data.tar.gz: 2b8c8b181cce3c37be518711b628567a8cab86904299fd1d2a94e73fd132a5e977702b89ed96b348f7aa9b4438d42d5e0cc2ebe46278a5171e15f8fdbd5683dc
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
data/lib/rspec/httpd.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
require "rspec/core"
|
2
2
|
require "forwardable"
|
3
3
|
require "logger"
|
4
|
+
require "expectation"
|
5
|
+
|
6
|
+
# rubocop:disable Metrics/AbcSize
|
7
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
8
|
+
# rubocop:disable Metrics/PerceivedComplexity
|
4
9
|
|
5
10
|
module RSpec::Httpd
|
6
11
|
end
|
@@ -9,7 +14,7 @@ require_relative "httpd/version"
|
|
9
14
|
require_relative "httpd/server"
|
10
15
|
require_relative "httpd/client"
|
11
16
|
require_relative "httpd/config"
|
12
|
-
require_relative "httpd/
|
17
|
+
require_relative "httpd/expectation_failed"
|
13
18
|
|
14
19
|
module RSpec::Httpd
|
15
20
|
extend self
|
@@ -54,6 +59,10 @@ module RSpec::Httpd
|
|
54
59
|
public
|
55
60
|
|
56
61
|
def expect_response(expected = nil, status: nil, client: nil)
|
62
|
+
if expected.nil? && block_given?
|
63
|
+
expected = yield
|
64
|
+
end
|
65
|
+
|
57
66
|
client ||= http
|
58
67
|
|
59
68
|
# only check status? This lets us write
|
@@ -69,10 +78,13 @@ module RSpec::Httpd
|
|
69
78
|
# here, because it needs access to the expect() implementation.
|
70
79
|
|
71
80
|
expect(client.status).to eq(status || 200)
|
72
|
-
|
73
|
-
|
81
|
+
return if expected.nil?
|
82
|
+
|
83
|
+
begin
|
84
|
+
# expect! comes from the expectation gem
|
85
|
+
expect! client.result => expected
|
86
|
+
rescue ::Expectation::Matcher::Mismatch
|
87
|
+
raise ExpectationFailed.new($!, client.request)
|
74
88
|
end
|
75
89
|
end
|
76
|
-
|
77
|
-
include RSpec::Httpd::Expectation
|
78
90
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class RSpec::Httpd::ExpectationFailed < RuntimeError
|
2
|
+
attr_reader :original_error, :request
|
3
|
+
|
4
|
+
def initialize(original_error, request)
|
5
|
+
@original_error, @request = original_error, request
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_s
|
9
|
+
"#{request_info}: #{original_error}"
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def request_info
|
15
|
+
request_info = "#{request.method} #{request.path}"
|
16
|
+
return request_info unless request.body
|
17
|
+
|
18
|
+
"#{request_info}#{body_info(parsed_body(body) || body)}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def parsed_body(body)
|
22
|
+
JSON.parse(body)
|
23
|
+
rescue StandardError
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def body_info(body)
|
28
|
+
body = parsed_body(body) || body
|
29
|
+
|
30
|
+
case body
|
31
|
+
when Hash
|
32
|
+
body.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")
|
33
|
+
else
|
34
|
+
body.inspect
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-httpd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Enrico Thierbach
|
@@ -10,6 +10,20 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2019-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: expectation
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.1
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rake
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,7 +78,7 @@ files:
|
|
64
78
|
- lib/rspec/httpd.rb
|
65
79
|
- lib/rspec/httpd/client.rb
|
66
80
|
- lib/rspec/httpd/config.rb
|
67
|
-
- lib/rspec/httpd/
|
81
|
+
- lib/rspec/httpd/expectation_failed.rb
|
68
82
|
- lib/rspec/httpd/server.rb
|
69
83
|
- lib/rspec/httpd/version.rb
|
70
84
|
homepage: https://github.com/radiospiel/rspec-httpd
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require "rspec/core"
|
2
|
-
require "forwardable"
|
3
|
-
require "logger"
|
4
|
-
|
5
|
-
module RSpec::Httpd::Expectation
|
6
|
-
def do_expect_last_request(expected:, client:)
|
7
|
-
actual = client.result
|
8
|
-
|
9
|
-
ctx = {
|
10
|
-
request: client.request, actual: actual, expected: expected
|
11
|
-
}
|
12
|
-
|
13
|
-
case expected
|
14
|
-
when Regexp then expect(actual).to match(expected), -> { format_failure SHOULD_MATCH_ERROR, ctx }
|
15
|
-
when Hash then expect(actual).to include(expected), -> { format_failure SHOULD_INCLUDE_ERROR, ctx }
|
16
|
-
else expect(actual).to eq(expected), -> { format_failure SHOULD_EQUAL_ERROR, ctx }
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
SHOULD_MATCH_ERROR = "%request -- %actual should match %expected".freeze
|
21
|
-
SHOULD_INCLUDE_ERROR = "%request -- %actual should include %expected".freeze
|
22
|
-
SHOULD_EQUAL_ERROR = "%request -- %actual should equal %expected".freeze
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def format_failure(format, ctx)
|
27
|
-
format.gsub(/\%(\S+)/) do
|
28
|
-
value = ctx.fetch(Regexp.last_match(1).to_sym)
|
29
|
-
case value
|
30
|
-
when Net::HTTPGenericRequest
|
31
|
-
request = value
|
32
|
-
s = "#{request.method} #{request.path}"
|
33
|
-
s += " body:#{request.body.inspect}" if request.body
|
34
|
-
s
|
35
|
-
else
|
36
|
-
value.inspect
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|