File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -101,7 +101,7 @@ def self.to_json(string)
101
101
# data: {JSON}
102
102
# ...
103
103
# data: [DONE]
104
-
104
+
105
105
# Only call the user_proc if the chunk contains a JSON object.
106
106
chunk . scan ( /data: (\{ .*\} )/i ) . flatten . each do |data |
107
107
user_proc . call ( JSON . parse ( data ) , bytesize )
Original file line number Diff line number Diff line change 69
69
end
70
70
71
71
context "when called with a string containing that looks like a JSON object but is invalid" do
72
- let ( :chunk ) {
72
+ let ( :chunk ) do
73
73
<<-CHUNK
74
74
data: { "foo": "bar" }
75
75
data: { BAD ]:-> JSON }
76
76
CHUNK
77
- }
77
+ end
78
+
79
+ it "does not raise an error" do
80
+ expect ( user_proc ) . to receive ( :call ) . with ( JSON . parse ( '{"foo": "bar"}' ) , nil )
81
+
82
+ expect do
83
+ stream . call ( chunk )
84
+ end . to_not raise_error ( JSON ::ParserError )
85
+ end
86
+ end
87
+
88
+ context "when called with a string containing an error" do
89
+ let ( :chunk ) do
90
+ <<-CHUNK
91
+ data: { "foo": "bar" }
92
+ error: { "message": "A bad thing has happened!" }
93
+ CHUNK
94
+ end
78
95
79
96
it "does not raise an error" do
80
97
expect ( user_proc ) . to receive ( :call ) . with ( JSON . parse ( '{"foo": "bar"}' ) , nil )
98
+ expect ( user_proc ) . to_not receive ( :call ) . with (
99
+ JSON . parse ( '{ "message": "A bad thing has happened!" }' ) , nil
100
+ )
81
101
82
- expect {
83
- stream . call ( chunk )
84
- } . to_not raise_error ( JSON ::ParserError )
102
+ expect do
103
+ stream . call ( chunk )
104
+ end . to_not raise_error ( JSON ::ParserError )
85
105
end
86
106
end
87
107
end
You can’t perform that action at this time.
0 commit comments