Skip to content
This repository
Browse code

The less hacky way to get the stream behaviour working for em-http-re…

…quest. Suggested by @mloughran
  • Loading branch information...
commit 15f888ee91ee386c3ba2879788e325ba4e3c6413 1 parent 26cb92f
Bartosz Blimke authored
4  CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,9 @@
1 1
 #Changelog
2 2
 
  3
+## 1.3.1
  4
+
  5
+* The less hacky way to get the stream behaviour working for em-http-request. Suggested by Martyn Loughran
  6
+
3 7
 ## 1.3.0
4 8
 
5 9
 * Added support for [em-http-request](http://github.com/igrigorik/em-http-request)
1  README.md
Source Rendered
@@ -498,6 +498,7 @@ People who submitted patches and new features or suggested improvements. Many th
498 498
 * Razic
499 499
 * Steve Tooke
500 500
 * Nathaniel Bibler
  501
+* Martyn Loughran
501 502
 
502 503
 ## Background
503 504
 
12  lib/webmock/http_lib_adapters/em_http_request.rb
@@ -11,14 +11,16 @@ def setup(response, uri, error = nil)
11 11
             on_error(error)
12 12
             fail(self)
13 13
           else
14  
-            receive_data(response)
15  
-            succeed(self)
  14
+            EM.next_tick do
  15
+              receive_data(response)
  16
+              succeed(self)
  17
+            end
16 18
           end
17 19
         end
18 20
         
19  
-        def stream(&blk)
20  
-          blk.call(@response)
21  
-        end
  21
+        # def stream(&blk)
  22
+          # blk.call(@response)
  23
+        # end
22 24
 
23 25
         def unbind
24 26
         end
24  spec/em_http_request_spec_helper.rb
@@ -21,17 +21,10 @@ def http_request(method, uri, options = {}, &block)
21 21
         error = http.errors         
22 22
         failed 
23 23
       }
24  
-      headers = {}
25  
-      if http.response_header
26  
-        http.response_header.each do |k,v|
27  
-          v = v.join(", ") if v.is_a?(Array)
28  
-          headers[k] = v 
29  
-        end
30  
-      end
31  
-      http.callback {
  24
+      http.callback {    
32 25
         response = OpenStruct.new({
33 26
           :body => http.response,
34  
-          :headers => WebMock::Util::Headers.normalize_headers(headers),          
  27
+          :headers => WebMock::Util::Headers.normalize_headers(extract_response_headers(http)),          
35 28
           :message => http.response_header.http_reason,
36 29
           :status => http.response_header.status.to_s
37 30
         })
@@ -60,5 +53,18 @@ def setup_expectations_for_real_request(options = {})
60 53
   def http_library
61 54
     :em_http_request
62 55
   end
  56
+  
  57
+  private
  58
+  
  59
+  def extract_response_headers(http)
  60
+    headers = {}
  61
+    if http.response_header
  62
+      http.response_header.each do |k,v|
  63
+        v = v.join(", ") if v.is_a?(Array)
  64
+        headers[k] = v 
  65
+      end
  66
+    end
  67
+    headers
  68
+  end
63 69
 
64 70
 end

0 notes on commit 15f888e

Please sign in to comment.
Something went wrong with that request. Please try again.