Class: HTTPStub
- Inherits:
-
Object
- Object
- HTTPStub
- Defined in:
- lib/httpstub.rb
Constant Summary collapse
- @@server_list =
{}
- @@thread_group =
nil- @@disable_logging =
true
Class Method Summary collapse
- .build_server(url) ⇒ Object
- .delete(url, metadata, body) ⇒ Object
- .disable_logging=(value) ⇒ Object
- .disable_logging? ⇒ Boolean
- .get(url, metadata, body) ⇒ Object
- .listen_on(urls) ⇒ Object
- .post(url, metadata, body) ⇒ Object
- .put(url, metadata, body) ⇒ Object
- .stop_server ⇒ Object
- .stub(method, url, metadata, body) ⇒ Object
Class Method Details
.build_server(url) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/httpstub.rb', line 33 def self.build_server(url) uri = URI.parse(url) server = HTTPStubServer.new(uri.port, uri.scheme == "https") @@server_list[uri.port] = server Thread.start do #server.listen "0.0.0.0", uri.port server.start end end |
.delete(url, metadata, body) ⇒ Object
70 71 72 |
# File 'lib/httpstub.rb', line 70 def self.delete(url, , body) stub(:delete, url, , body) end |
.disable_logging=(value) ⇒ Object
13 14 15 |
# File 'lib/httpstub.rb', line 13 def self.disable_logging=(value) @@disable_logging = value end |
.disable_logging? ⇒ Boolean
17 18 19 |
# File 'lib/httpstub.rb', line 17 def self.disable_logging? @@disable_logging end |
.get(url, metadata, body) ⇒ Object
58 59 60 |
# File 'lib/httpstub.rb', line 58 def self.get(url, , body) stub(:get, url, , body) end |
.listen_on(urls) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/httpstub.rb', line 22 def self.listen_on(urls) stop_server unless @@thread_group @@thread_group = ThreadGroup.new [urls].flatten.each do |url| @@thread_group.add build_server(url) end end end |
.post(url, metadata, body) ⇒ Object
62 63 64 |
# File 'lib/httpstub.rb', line 62 def self.post(url, , body) stub(:post, url, , body) end |
.put(url, metadata, body) ⇒ Object
66 67 68 |
# File 'lib/httpstub.rb', line 66 def self.put(url, , body) stub(:put, url, , body) end |
.stop_server ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/httpstub.rb', line 44 def self.stop_server if @@thread_group @@server_list.values.each do |server| server.servlet.clear_responses server.shutdown end @@thread_group.list.each { |th| th.join } @@thread_group = nil @@server_list.clear end end |
.stub(method, url, metadata, body) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/httpstub.rb', line 75 def self.stub(method, url, , body) parsed_url = URI.parse(url) server = @@server_list[parsed_url.port] path = if parsed_url.query.nil? or parsed_url.query == "" parsed_url.path else "#{parsed_url.path}?#{parsed_url.query}" end server.servlet.send("stub_#{method}", path, , body) end |