rspec-httpd 0.0.15 → 0.0.17

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
  SHA256:
3
- metadata.gz: 39e7f534f32be091cf91b9a86482a7f6ad77edb2f23f900135e5c3b39f10e390
4
- data.tar.gz: b8c93f5a2f115ed3095bd474bc9a2c63fac4a3324a7c04930b4b0b87b6e9e51e
3
+ metadata.gz: 428238b3dd716da074b3038797776712343d204dd0b15b0aed8dcc92729dbaab
4
+ data.tar.gz: c3df40165e1460560725838e9ac392e4088a1168a8ce27f1ccf383e90facc6a9
5
5
  SHA512:
6
- metadata.gz: 724693c35daa22840ff5a3871ac39fbecad5fdb22a38621e8e704b647683f28c953cdffe454a33a3b86434e6f8b2968528067e7b10c1008bac7b67bd537755f5
7
- data.tar.gz: 28f7cf7303b11c67ebde228ec1a323f4ad88cca6891c6d46881b2fbebaeeed844f5cae2b3cd227dd1f4680e6ac85e8f7cbd36c71e86ff13c4f8dcf114d8665f7
6
+ metadata.gz: '08cd0cccda74339ee925a188a201b8d06136f92c659f5d6e17e4fc68e6b5c3a43f9c4aea07e5957b4c3b2f520a2ab1f9ab90ed26c28db834da356c014cb5f735'
7
+ data.tar.gz: e919cfb7a3ff2ec67fc9dac460642da31bd1351e28aeb2c97de102243bebfa3c119884d7ad7f023b38ed6d8ebe25dc58bfd4b6b2ca89e64f533650a645c55203
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.15
1
+ 0.0.17
@@ -1,135 +1,22 @@
1
- require "net/http"
2
- require "json"
3
-
4
- class Net::HTTPResponse
5
- def headers
6
- @headers ||= HeadersHash.new(self)
7
- end
8
-
9
- class HeadersHash < Hash
10
- def initialize(response)
11
- response.each_header do |k, v|
12
- case self[k]
13
- when Array then self[k].concat v
14
- when nil then self[k] = v
15
- else self[k].concat(v)
16
- end
17
- end
18
- end
19
-
20
- def [](key)
21
- super key.downcase
22
- end
23
-
24
- private
25
-
26
- def []=(key, value)
27
- super key.downcase, value
28
- end
29
- end
30
- end
1
+ require "simple-http"
31
2
 
32
3
  module RSpec::Httpd
33
- class Client
34
- # host and port. Set at initialization
35
- attr_reader :host
36
- attr_reader :port
37
-
4
+ class Client < Simple::HTTP
38
5
  def initialize(host:, port:)
39
- @host, @port = host, port
6
+ super()
7
+ self.base_url = "http://#{host}:#{port}"
40
8
  end
41
9
 
42
- # request, response, and status, set during a request/response cycle
43
- attr_reader :request
44
10
  attr_reader :response
45
11
 
46
- def status
47
- Integer(response.code)
48
- end
49
-
50
- # returns the parsed response of the latest request
51
- def result
52
- @result ||= ResponseParser.parse(response)
53
- end
54
-
55
- # A GET request
56
- def get(url, headers: {})
57
- run_request(::Net::HTTP::Get, url, body: nil, headers: headers)
58
- end
59
-
60
- # A HEAD request
61
- def head(url, headers: {})
62
- run_request(::Net::HTTP::Head, url, body: nil, headers: headers)
63
- end
64
-
65
- # A POST request
66
- def post(url, body, headers: {})
67
- run_request(::Net::HTTP::Post, url, body: body, headers: headers)
68
- end
69
-
70
- # A PUT request
71
- def put(url, body, headers: {})
72
- run_request(::Net::HTTP::Put, url, body: body || {}, headers: headers)
73
- end
74
-
75
- # A DELETE request
76
- def delete(url, headers: {})
77
- run_request(::Net::HTTP::Delete, url, body: nil, headers: headers)
12
+ def content
13
+ @response.content
78
14
  end
79
15
 
80
16
  private
81
17
 
82
- def run_request(request_klass, url, body:, headers:)
83
- # reset all results from previous run
84
- @request = @response = @headers = @result = nil
85
-
86
- @response = Net::HTTP.start(host, port) do |http|
87
- @request = build_request(request_klass, url, body: body, headers: headers)
88
-
89
- log_request(request)
90
- http.request(request)
91
- end
92
- end
93
-
94
- def build_request(request_klass, url, body:, headers:)
95
- request_klass.new(url, headers).tap do |request|
96
- if body
97
- request["Content-Type"] = "application/json"
98
- request.body = JSON.generate(body)
99
- end
100
- end
101
- end
102
-
103
- def log_request(request)
104
- if request.body
105
- RSpec::Httpd.logger.info "#{request.method} #{request.path} #{request.body.inspect[0..100]}"
106
- else
107
- RSpec::Httpd.logger.info "#{request.method} #{request.path}"
108
- end
109
- end
110
-
111
- module ResponseParser
112
- def self.parse(response)
113
- return nil if response.body.nil?
114
-
115
- content_type = response["content-type"]
116
-
117
- result = if content_type&.include?("application/json")
118
- JSON.parse(response.body)
119
- else
120
- response.body.force_encoding("utf-8").encode
121
- end
122
-
123
- result.extend(self)
124
- result.__response__ = response
125
- result
126
- end
127
-
128
- attr_accessor :__response__
129
-
130
- def status
131
- Integer(__response__.code)
132
- end
18
+ def execute_request(request, max_redirections: 10)
19
+ @response = super(request, max_redirections: max_redirections)
133
20
  end
134
21
  end
135
22
  end
@@ -2,10 +2,12 @@ class RSpec::Httpd::Config
2
2
  attr_accessor :host
3
3
  attr_accessor :port
4
4
  attr_accessor :command
5
+ attr_accessor :logger
5
6
 
6
7
  def initialize
7
8
  self.host = "127.0.0.1"
8
9
  self.port = 12_345
9
10
  self.command = "bundle exec rackup -E test -p 12345"
11
+ self.logger = Logger.new(STDERR).tap { |logger| logger.level = Logger::INFO }
10
12
  end
11
13
  end
@@ -1,16 +1,20 @@
1
1
  # rubocop:disable Metrics/AbcSize
2
2
 
3
3
  class RSpec::Httpd::ExpectationFailed < RuntimeError
4
- attr_reader :original_error, :request, :response
4
+ attr_reader :original_error, :response
5
5
 
6
- def initialize(original_error, request:, response:)
7
- @original_error, @request, @response = original_error, request, response
6
+ def initialize(original_error, response:)
7
+ @original_error, @response = original_error, response
8
+ end
9
+
10
+ def request
11
+ response.request
8
12
  end
9
13
 
10
14
  def to_s
11
15
  parts = []
12
16
  parts.push(original_error.to_s)
13
- parts.push("=== #{request.method} #{request.path} =====================")
17
+ parts.push("=== #{request} =====================")
14
18
  parts.push("> " + request.body.gsub("\n", "\n> ")) if request.body
15
19
  parts.push("--- response ------------------------------------")
16
20
  parts.push("< " + response.body.gsub("\n", "\n< ")) if response.body
@@ -3,26 +3,45 @@
3
3
  require "socket"
4
4
  require "timeout"
5
5
 
6
+ module RSpec
7
+ end
8
+
6
9
  module RSpec::Httpd
7
10
  module Server
8
11
  MAX_STARTUP_TIME = 10
9
12
 
10
13
  extend self
11
14
 
15
+ def logger
16
+ # If this file is required as-is, without loading all of rspec/httpd,
17
+ # ::RSpec::Httpd does not provide a logger. In that case we polyfill
18
+ # a default logger to STDERR.
19
+ #
20
+ # Doing so lets use this file as is; which lets one use the
21
+ #
22
+ # RSpec::Httpd::Server.start! ...
23
+ #
24
+ # method.
25
+ if ::RSpec::Httpd.respond_to?(:logger)
26
+ ::RSpec::Httpd.logger
27
+ else
28
+ @logger ||= ::Logger.new(STDERR, level: :info)
29
+ end
30
+ end
31
+
12
32
  # builds and returns a server object.
13
33
  #
14
34
  # You can use this method to retrieve a client connection to a server
15
35
  # specified via host:, port:, and, optionally, a command.
16
- def start!(host:, port:, command:)
36
+ def start!(host: "0.0.0.0", port:, command:, logger: nil)
17
37
  @servers ||= {}
18
38
  @servers[[host, port, command]] ||= do_start(host, port, command)
39
+ @logger = logger if logger
19
40
  end
20
41
 
21
42
  private
22
43
 
23
44
  def do_start(host, port, command)
24
- logger = RSpec::Httpd.logger
25
-
26
45
  if port_open?(host, port)
27
46
  logger.error "A process is already running on #{host}:#{port}"
28
47
  exit 2
data/lib/rspec/httpd.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "rspec/core"
2
- require "forwardable"
3
2
  require "logger"
4
3
  require "expectation"
5
4
 
@@ -29,7 +28,7 @@ module RSpec::Httpd
29
28
  end
30
29
 
31
30
  def logger #:nodoc:
32
- @logger ||= Logger.new(STDERR).tap { |logger| logger.level = Logger::INFO }
31
+ @config.logger
33
32
  end
34
33
 
35
34
  # builds and returns a client.
@@ -73,18 +72,19 @@ module RSpec::Httpd
73
72
  #
74
73
  expected_status = expected.is_a?(Integer) && status.nil? ? expected : status || 200
75
74
 
76
- request, response = client.request, client.response
75
+ response = client.response
76
+ request = response.request
77
77
 
78
- expect(client.status).to eq(expected_status),
79
- "status should be #{expected_status}, but is #{client.status}, on '#{request.method} #{request.path}'"
78
+ expect(response.status).to eq(expected_status),
79
+ "status should be #{expected_status}, but is #{response.status}, on '#{request}'"
80
80
 
81
81
  return if expected.nil? || expected.is_a?(Integer)
82
82
 
83
83
  begin
84
84
  # expect! comes from the expectation gem
85
- expect! client.result => expected
85
+ expect! response.content => expected
86
86
  rescue ::Expectation::Matcher::Mismatch => e
87
- raise ExpectationFailed.new(e, request: request, response: response), cause: nil
87
+ raise ExpectationFailed.new(e, response: response), cause: nil
88
88
  end
89
89
  end
90
90
 
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.15
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Enrico Thierbach
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: simple-http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -100,8 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
114
  - !ruby/object:Gem::Version
101
115
  version: '0'
102
116
  requirements: []
103
- rubyforge_project:
104
- rubygems_version: 2.7.6
117
+ rubygems_version: 3.0.4
105
118
  signing_key:
106
119
  specification_version: 4
107
120
  summary: RSpec testing for HTTP requests