|
| 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 | + output = ::Protocol::HTTP::Body::Writable.new |
| 14 | + |
| 15 | + Async do |
| 16 | + stream = ::Protocol::HTTP::Body::Stream.new(request.body, output) |
| 17 | + |
| 18 | + Console.debug(self, "Echoing chunks...") |
| 19 | + while chunk = stream.readpartial(1024) |
| 20 | + Console.debug(self, "Reading chunk:", chunk: chunk) |
| 21 | + stream.write(chunk) |
| 22 | + end |
| 23 | + rescue EOFError |
| 24 | + Console.debug(self, "EOF.") |
| 25 | + # Ignore. |
| 26 | + ensure |
| 27 | + Console.debug(self, "Closing stream.") |
| 28 | + stream.close |
| 29 | + end |
| 30 | + |
| 31 | + ::Protocol::HTTP::Response[200, {}, output] |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + it "should echo the request body" do |
| 36 | + chunks = ["Hello,", "World!"] |
| 37 | + response_chunks = Queue.new |
| 38 | + |
| 39 | + output = ::Protocol::HTTP::Body::Writable.new |
| 40 | + response = client.post("/", body: output) |
| 41 | + stream = ::Protocol::HTTP::Body::Stream.new(response.body, output) |
| 42 | + |
| 43 | + begin |
| 44 | + Console.debug(self, "Echoing chunks...") |
| 45 | + chunks.each do |chunk| |
| 46 | + Console.debug(self, "Writing chunk:", chunk: chunk) |
| 47 | + stream.write(chunk) |
| 48 | + end |
| 49 | + |
| 50 | + Console.debug(self, "Closing write.") |
| 51 | + stream.close_write |
| 52 | + |
| 53 | + Console.debug(self, "Reading chunks...") |
| 54 | + while chunk = stream.readpartial(1024) |
| 55 | + Console.debug(self, "Reading chunk:", chunk: chunk) |
| 56 | + response_chunks << chunk |
| 57 | + end |
| 58 | + rescue EOFError |
| 59 | + Console.debug(self, "EOF.") |
| 60 | + # Ignore. |
| 61 | + ensure |
| 62 | + Console.debug(self, "Closing stream.") |
| 63 | + stream.close |
| 64 | + response_chunks.close |
| 65 | + end |
| 66 | + |
| 67 | + chunks.each do |chunk| |
| 68 | + expect(response_chunks.pop).to be == chunk |
| 69 | + end |
| 70 | + end |
| 71 | +end |
| 72 | + |
| 73 | +AnEchoClient = Sus::Shared("an echo client") do |
| 74 | + let(:chunks) {["Hello,", "World!"]} |
| 75 | + let(:response_chunks) {Queue.new} |
| 76 | + |
| 77 | + let(:app) do |
| 78 | + ::Protocol::HTTP::Middleware.for do |request| |
| 79 | + output = ::Protocol::HTTP::Body::Writable.new |
| 80 | + |
| 81 | + Async do |
| 82 | + stream = ::Protocol::HTTP::Body::Stream.new(request.body, output) |
| 83 | + |
| 84 | + Console.debug(self, "Echoing chunks...") |
| 85 | + chunks.each do |chunk| |
| 86 | + stream.write(chunk) |
| 87 | + end |
| 88 | + |
| 89 | + Console.debug(self, "Closing write.") |
| 90 | + stream.close_write |
| 91 | + |
| 92 | + Console.debug(self, "Reading chunks...") |
| 93 | + while chunk = stream.readpartial(1024) |
| 94 | + Console.debug(self, "Reading chunk:", chunk: chunk) |
| 95 | + response_chunks << chunk |
| 96 | + end |
| 97 | + rescue EOFError |
| 98 | + Console.debug(self, "EOF.") |
| 99 | + # Ignore. |
| 100 | + ensure |
| 101 | + Console.debug(self, "Closing stream.") |
| 102 | + stream.close |
| 103 | + end |
| 104 | + |
| 105 | + ::Protocol::HTTP::Response[200, {}, output] |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + it "should echo the response body" do |
| 110 | + output = ::Protocol::HTTP::Body::Writable.new |
| 111 | + response = client.post("/", body: output) |
| 112 | + stream = ::Protocol::HTTP::Body::Stream.new(response.body, output) |
| 113 | + |
| 114 | + begin |
| 115 | + Console.debug(self, "Echoing chunks...") |
| 116 | + while chunk = stream.readpartial(1024) |
| 117 | + stream.write(chunk) |
| 118 | + end |
| 119 | + rescue EOFError |
| 120 | + Console.debug(self, "EOF.") |
| 121 | + # Ignore. |
| 122 | + ensure |
| 123 | + Console.debug(self, "Closing stream.") |
| 124 | + stream.close |
| 125 | + end |
| 126 | + |
| 127 | + chunks.each do |chunk| |
| 128 | + expect(response_chunks.pop).to be == chunk |
| 129 | + end |
| 130 | + end |
| 131 | +end |
| 132 | + |
| 133 | +[Async::HTTP::Protocol::HTTP1, Async::HTTP::Protocol::HTTP2].each do |protocol| |
| 134 | + describe protocol, unique: protocol.name do |
| 135 | + include Sus::Fixtures::Async::HTTP::ServerContext |
| 136 | + |
| 137 | + let(:protocol) {subject} |
| 138 | + |
| 139 | + it_behaves_like AnEchoServer |
| 140 | + it_behaves_like AnEchoClient |
| 141 | + end |
| 142 | +end |
0 commit comments