|
92 | 92 | "rack.input" => StringIO.new(my_json_struct_encoded), |
93 | 93 | "CONTENT_TYPE" => "application/cloudevents+json" |
94 | 94 | } |
95 | | - event = http_binding.decode_rack_env env |
| 95 | + event = http_binding.decode_event env |
96 | 96 | assert_equal my_id, event.id |
97 | 97 | assert_equal my_source, event.source |
98 | 98 | assert_equal my_type, event.type |
|
112 | 112 | "rack.input" => StringIO.new(my_json_batch_encoded), |
113 | 113 | "CONTENT_TYPE" => "application/cloudevents-batch+json" |
114 | 114 | } |
115 | | - events = http_binding.decode_rack_env env |
| 115 | + events = http_binding.decode_event env |
116 | 116 | assert_equal 1, events.size |
117 | 117 | event = events.first |
118 | 118 | assert_equal my_id, event.id |
|
151 | 151 | "HTTP_CE_SUBJECT" => my_subject, |
152 | 152 | "HTTP_CE_TIME" => my_time_string |
153 | 153 | } |
154 | | - event = http_binding.decode_rack_env env |
| 154 | + event = http_binding.decode_event env |
155 | 155 | assert_equal my_id, event.id |
156 | 156 | assert_equal my_source, event.source |
157 | 157 | assert_equal my_type, event.type |
|
171 | 171 | "rack.input" => StringIO.new(my_json_data_struct_encoded), |
172 | 172 | "CONTENT_TYPE" => "application/cloudevents+json" |
173 | 173 | } |
174 | | - event = http_binding.decode_rack_env env |
| 174 | + event = http_binding.decode_event env |
175 | 175 | assert_equal my_id, event.id |
176 | 176 | assert_equal my_source, event.source |
177 | 177 | assert_equal my_type, event.type |
|
208 | 208 | "HTTP_CE_SUBJECT" => my_subject, |
209 | 209 | "HTTP_CE_TIME" => my_time_string |
210 | 210 | } |
211 | | - event = http_binding.decode_rack_env env |
| 211 | + event = http_binding.decode_event env |
212 | 212 | assert_equal my_id, event.id |
213 | 213 | assert_equal my_source, event.source |
214 | 214 | assert_equal my_type, event.type |
|
223 | 223 | assert_equal my_json_data_struct_encoded, body |
224 | 224 | end |
225 | 225 |
|
| 226 | + it "decodes and re-encodes a binary JSON rack env using deprecated methods" do |
| 227 | + env = { |
| 228 | + "rack.input" => StringIO.new(my_simple_data), |
| 229 | + "HTTP_CE_ID" => my_id, |
| 230 | + "HTTP_CE_SOURCE" => my_source_string, |
| 231 | + "HTTP_CE_TYPE" => my_type, |
| 232 | + "HTTP_CE_SPECVERSION" => spec_version, |
| 233 | + "CONTENT_TYPE" => my_json_content_type_string, |
| 234 | + "HTTP_CE_DATASCHEMA" => my_schema_string, |
| 235 | + "HTTP_CE_SUBJECT" => my_subject, |
| 236 | + "HTTP_CE_TIME" => my_time_string |
| 237 | + } |
| 238 | + event = http_binding.decode_rack_env env |
| 239 | + assert_equal my_id, event.id |
| 240 | + assert_equal my_source, event.source |
| 241 | + assert_equal my_type, event.type |
| 242 | + assert_equal spec_version, event.spec_version |
| 243 | + assert_equal my_simple_data, event.data |
| 244 | + assert_equal my_json_content_type, event.data_content_type |
| 245 | + assert_equal my_schema, event.data_schema |
| 246 | + assert_equal my_subject, event.subject |
| 247 | + assert_equal my_time, event.time |
| 248 | + headers, body = http_binding.encode_binary_content event, sort: true |
| 249 | + expected_headers = { |
| 250 | + "CE-id" => my_id, |
| 251 | + "CE-source" => my_source_string, |
| 252 | + "CE-type" => my_type, |
| 253 | + "CE-specversion" => spec_version, |
| 254 | + "Content-Type" => my_json_content_type_string, |
| 255 | + "CE-dataschema" => my_schema_string, |
| 256 | + "CE-subject" => my_subject, |
| 257 | + "CE-time" => my_time_string |
| 258 | + } |
| 259 | + assert_equal expected_headers, headers |
| 260 | + assert_equal my_simple_data, body |
| 261 | + end |
| 262 | + |
226 | 263 | it "decodes a binary rack env using an InputWrapper and re-encodes as structured" do |
227 | 264 | env = { |
228 | 265 | "rack.input" => Rack::Lint::InputWrapper.new(StringIO.new(my_simple_data)), |
|
235 | 272 | "HTTP_CE_SUBJECT" => my_subject, |
236 | 273 | "HTTP_CE_TIME" => my_time_string |
237 | 274 | } |
238 | | - event = http_binding.decode_rack_env env |
| 275 | + event = http_binding.decode_event env |
239 | 276 | assert_equal my_id, event.id |
240 | 277 | assert_equal my_source, event.source |
241 | 278 | assert_equal my_type, event.type |
|
257 | 294 | "HTTP_CE_TYPE" => my_type, |
258 | 295 | "HTTP_CE_SPECVERSION" => spec_version |
259 | 296 | } |
260 | | - event = http_binding.decode_rack_env env |
| 297 | + event = http_binding.decode_event env |
261 | 298 | assert_equal my_id, event.id |
262 | 299 | assert_equal my_source, event.source |
263 | 300 | assert_equal my_type, event.type |
|
289 | 326 | "HTTP_CE_SPECVERSION" => spec_version, |
290 | 327 | "HTTP_CE_TRACECONTEXT" => my_trace_context |
291 | 328 | } |
292 | | - event = http_binding.decode_rack_env env |
| 329 | + event = http_binding.decode_event env |
293 | 330 | assert_equal my_trace_context, event["tracecontext"] |
294 | 331 | headers, body = http_binding.encode_event event |
295 | 332 | expected_headers = { |
|
330 | 367 | "HTTP_CE_TYPE" => encoded_weird_type, |
331 | 368 | "HTTP_CE_SPECVERSION" => spec_version |
332 | 369 | } |
333 | | - reconstituted_event = http_binding.decode_rack_env env |
| 370 | + reconstituted_event = http_binding.decode_event env |
334 | 371 | assert_equal event, reconstituted_event |
335 | 372 | end |
336 | 373 |
|
337 | 374 | it "raises UnsupportedFormatError when a format is not recognized" do |
338 | 375 | env = { |
339 | 376 | "rack.input" => StringIO.new(my_json_struct_encoded), |
340 | | - "CONTENT_TYPE" => "application/cloudevents+json" |
| 377 | + "CONTENT_TYPE" => "application/cloudevents+hello" |
341 | 378 | } |
342 | 379 | assert_raises CloudEvents::UnsupportedFormatError do |
343 | | - minimal_http_binding.decode_rack_env env |
| 380 | + http_binding.decode_event env |
344 | 381 | end |
345 | 382 | end |
346 | 383 |
|
|
350 | 387 | "CONTENT_TYPE" => "application/cloudevents+json" |
351 | 388 | } |
352 | 389 | error = assert_raises CloudEvents::FormatSyntaxError do |
353 | | - http_binding.decode_rack_env env |
| 390 | + http_binding.decode_event env |
354 | 391 | end |
355 | 392 | assert_kind_of JSON::ParserError, error.cause |
356 | 393 | end |
|
361 | 398 | "CONTENT_TYPE" => "application/cloudevents-batch+json" |
362 | 399 | } |
363 | 400 | error = assert_raises CloudEvents::FormatSyntaxError do |
364 | | - http_binding.decode_rack_env env |
| 401 | + http_binding.decode_event env |
365 | 402 | end |
366 | 403 | assert_kind_of JSON::ParserError, error.cause |
367 | 404 | end |
|
374 | 411 | "HTTP_CE_SPECVERSION" => "0.1" |
375 | 412 | } |
376 | 413 | assert_raises CloudEvents::SpecVersionError do |
377 | | - http_binding.decode_rack_env env |
| 414 | + http_binding.decode_event env |
378 | 415 | end |
379 | 416 | end |
380 | 417 |
|
| 418 | + it "raises NotCloudEventError when a content-type is not recognized" do |
| 419 | + env = { |
| 420 | + "rack.input" => StringIO.new(my_json_struct_encoded), |
| 421 | + "CONTENT_TYPE" => "application/json" |
| 422 | + } |
| 423 | + assert_raises CloudEvents::NotCloudEventError do |
| 424 | + http_binding.decode_event env |
| 425 | + end |
| 426 | + end |
| 427 | + |
| 428 | + it "returns nil from the legacy decode method when a content-type is not recognized" do |
| 429 | + env = { |
| 430 | + "rack.input" => StringIO.new(my_json_struct_encoded), |
| 431 | + "CONTENT_TYPE" => "application/json" |
| 432 | + } |
| 433 | + assert_nil http_binding.decode_rack_env env |
| 434 | + end |
| 435 | + |
381 | 436 | it "decodes and re-encodes a structured event using opaque" do |
382 | 437 | env = { |
383 | 438 | "rack.input" => StringIO.new(my_json_struct_encoded), |
384 | 439 | "CONTENT_TYPE" => "application/cloudevents+json" |
385 | 440 | } |
386 | | - event = minimal_http_binding.decode_rack_env env, allow_opaque: true |
| 441 | + event = minimal_http_binding.decode_event env, allow_opaque: true |
387 | 442 | assert_kind_of CloudEvents::Event::Opaque, event |
388 | 443 | refute event.batch? |
389 | 444 | headers, body = minimal_http_binding.encode_event event |
|
396 | 451 | "rack.input" => StringIO.new(my_json_batch_encoded), |
397 | 452 | "CONTENT_TYPE" => "application/cloudevents-batch+json" |
398 | 453 | } |
399 | | - event = minimal_http_binding.decode_rack_env env, allow_opaque: true |
| 454 | + event = minimal_http_binding.decode_event env, allow_opaque: true |
400 | 455 | assert_kind_of CloudEvents::Event::Opaque, event |
401 | 456 | assert event.batch? |
402 | 457 | headers, body = minimal_http_binding.encode_event event |
|
0 commit comments