|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2024, by Samuel Williams. |
| 5 | + |
| 6 | +require "async/http/protocol/http" |
| 7 | +require "protocol/http/body/streamable" |
| 8 | +require "sus/fixtures/async/http" |
| 9 | + |
| 10 | +AnEchoServer = Sus::Shared("an echo server") do |
| 11 | + let(:app) do |
| 12 | + ::Protocol::HTTP::Middleware.for do |request| |
| 13 | + body = ::Protocol::HTTP::Body::Streamable.response(request) do |stream| |
| 14 | + # $stderr.puts "Server stream: #{stream.inspect}" |
| 15 | + |
| 16 | + while chunk = stream.readpartial(1024) |
| 17 | + # $stderr.puts "Server reading chunk: #{chunk.inspect}" |
| 18 | + stream.write(chunk) |
| 19 | + end |
| 20 | + rescue EOFError |
| 21 | + # Ignore. |
| 22 | + ensure |
| 23 | + # $stderr.puts "Server closing stream." |
| 24 | + stream.close |
| 25 | + end |
| 26 | + |
| 27 | + ::Protocol::HTTP::Response[200, {}, body] |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + it "should echo the request body" do |
| 32 | + chunks = ["Hello,", "World!"] |
| 33 | + response_chunks = Queue.new |
| 34 | + |
| 35 | + body = ::Protocol::HTTP::Body::Streamable.request do |stream| |
| 36 | + # $stderr.puts "Client stream: #{stream.inspect}" |
| 37 | + |
| 38 | + chunks.each do |chunk| |
| 39 | + # $stderr.puts "Client writing chunk: #{chunk.inspect}" |
| 40 | + stream.write(chunk) |
| 41 | + end |
| 42 | + |
| 43 | + # $stderr.puts "Client closing write." |
| 44 | + stream.close_write |
| 45 | + |
| 46 | + # $stderr.puts "Client reading chunks..." |
| 47 | + while chunk = stream.readpartial(1024) |
| 48 | + # $stderr.puts "Client reading chunk: #{chunk.inspect}" |
| 49 | + response_chunks << chunk |
| 50 | + end |
| 51 | + rescue EOFError |
| 52 | + # Ignore. |
| 53 | + ensure |
| 54 | + # $stderr.puts "Client closing stream." |
| 55 | + stream.close |
| 56 | + response_chunks.close |
| 57 | + end |
| 58 | + |
| 59 | + response = client.post("/", body: body) |
| 60 | + body.stream(response.body) |
| 61 | + |
| 62 | + chunks.each do |chunk| |
| 63 | + expect(response_chunks.pop).to be == chunk |
| 64 | + end |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +AnEchoClient = Sus::Shared("an echo client") do |
| 69 | + let(:chunks) {["Hello,", "World!"]} |
| 70 | + let(:response_chunks) {Queue.new} |
| 71 | + |
| 72 | + let(:app) do |
| 73 | + ::Protocol::HTTP::Middleware.for do |request| |
| 74 | + body = ::Protocol::HTTP::Body::Streamable.response(request) do |stream| |
| 75 | + chunks.each do |chunk| |
| 76 | + stream.write(chunk) |
| 77 | + end |
| 78 | + |
| 79 | + stream.close_write |
| 80 | + |
| 81 | + while chunk = stream.readpartial(1024) |
| 82 | + response_chunks << chunk |
| 83 | + end |
| 84 | + rescue EOFError |
| 85 | + # Ignore. |
| 86 | + ensure |
| 87 | + # $stderr.puts "Server closing stream." |
| 88 | + stream.close |
| 89 | + end |
| 90 | + |
| 91 | + ::Protocol::HTTP::Response[200, {}, body] |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + it "should echo the response body" do |
| 96 | + body = ::Protocol::HTTP::Body::Streamable.request do |stream| |
| 97 | + while chunk = stream.readpartial(1024) |
| 98 | + stream.write(chunk) |
| 99 | + end |
| 100 | + rescue EOFError |
| 101 | + # Ignore. |
| 102 | + ensure |
| 103 | + stream.close |
| 104 | + end |
| 105 | + |
| 106 | + response = client.post("/", body: body) |
| 107 | + body.stream(response.body) |
| 108 | + |
| 109 | + chunks.each do |chunk| |
| 110 | + expect(response_chunks.pop).to be == chunk |
| 111 | + end |
| 112 | + end |
| 113 | +end |
| 114 | + |
| 115 | +[Async::HTTP::Protocol::HTTP1].each do |protocol| |
| 116 | + describe protocol do |
| 117 | + include Sus::Fixtures::Async::HTTP::ServerContext |
| 118 | + |
| 119 | + let(:protocol) {subject} |
| 120 | + |
| 121 | + it_behaves_like AnEchoServer |
| 122 | + # it_behaves_like AnEchoClient |
| 123 | + end |
| 124 | +end |
0 commit comments