|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2019-2023, by Samuel Williams. |
| 5 | + |
| 6 | +require 'protocol/http/body/deflate' |
| 7 | + |
| 8 | +module Protocol |
| 9 | + module HTTP |
| 10 | + module Body |
| 11 | + AWritableBody = Sus::Shared("a writable body") do |
| 12 | + it "can write and read data" do |
| 13 | + 3.times do |i| |
| 14 | + body.write("Hello World #{i}") |
| 15 | + expect(body.read).to be == "Hello World #{i}" |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + it "can buffer data in order" do |
| 20 | + 3.times do |i| |
| 21 | + body.write("Hello World #{i}") |
| 22 | + end |
| 23 | + |
| 24 | + 3.times do |i| |
| 25 | + expect(body.read).to be == "Hello World #{i}" |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + with '#join' do |
| 30 | + it "can join chunks" do |
| 31 | + 3.times do |i| |
| 32 | + body.write("#{i}") |
| 33 | + end |
| 34 | + |
| 35 | + body.close |
| 36 | + |
| 37 | + expect(body.join).to be == "012" |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + with '#each' do |
| 42 | + it "can read all data in order" do |
| 43 | + 3.times do |i| |
| 44 | + body.write("Hello World #{i}") |
| 45 | + end |
| 46 | + |
| 47 | + body.close |
| 48 | + |
| 49 | + 3.times do |i| |
| 50 | + chunk = body.read |
| 51 | + expect(chunk).to be == "Hello World #{i}" |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + # it "can propagate failures" do |
| 56 | + # reactor.async do |
| 57 | + # expect do |
| 58 | + # body.each do |chunk| |
| 59 | + # raise RuntimeError.new("It was too big!") |
| 60 | + # end |
| 61 | + # end.to raise_exception(RuntimeError, message: be =~ /big/) |
| 62 | + # end |
| 63 | + |
| 64 | + # expect{ |
| 65 | + # body.write("Beep boop") # This will cause a failure. |
| 66 | + # ::Async::Task.current.yield |
| 67 | + # body.write("Beep boop") # This will fail. |
| 68 | + # }.to raise_exception(RuntimeError, message: be =~ /big/) |
| 69 | + # end |
| 70 | + |
| 71 | + # it "can propagate failures in nested bodies" do |
| 72 | + # nested = ::Protocol::HTTP::Body::Deflate.for(body) |
| 73 | + |
| 74 | + # reactor.async do |
| 75 | + # expect do |
| 76 | + # nested.each do |chunk| |
| 77 | + # raise RuntimeError.new("It was too big!") |
| 78 | + # end |
| 79 | + # end.to raise_exception(RuntimeError, message: be =~ /big/) |
| 80 | + # end |
| 81 | + |
| 82 | + # expect{ |
| 83 | + # body.write("Beep boop") # This will cause a failure. |
| 84 | + # ::Async::Task.current.yield |
| 85 | + # body.write("Beep boop") # This will fail. |
| 86 | + # }.to raise_exception(RuntimeError, message: be =~ /big/) |
| 87 | + # end |
| 88 | + |
| 89 | + # it "will stop after finishing" do |
| 90 | + # output_task = reactor.async do |
| 91 | + # body.each do |chunk| |
| 92 | + # expect(chunk).to be == "Hello World!" |
| 93 | + # end |
| 94 | + # end |
| 95 | + |
| 96 | + # body.write("Hello World!") |
| 97 | + # body.close |
| 98 | + |
| 99 | + # expect(body).not.to be(:empty?) |
| 100 | + |
| 101 | + # ::Async::Task.current.yield |
| 102 | + |
| 103 | + # expect(output_task).to be(:finished?) |
| 104 | + # expect(body).to be(:empty?) |
| 105 | + # end |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | +end |
0 commit comments