|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | +require "faraday" |
| 5 | +require "socket" |
| 6 | +require "stringio" |
| 7 | +require "zlib" |
| 8 | +require "mcp/client/oauth" |
| 9 | + |
| 10 | +module MCP |
| 11 | + class Client |
| 12 | + module OAuth |
| 13 | + class BoundedBodyTest < Minitest::Test |
| 14 | + def test_on_data_rejects_chunks_that_together_exceed_the_limit |
| 15 | + bounded = bounded_body(max_bytes: 64) |
| 16 | + |
| 17 | + error = assert_raises(too_large_error) do |
| 18 | + 4.times do |
| 19 | + bounded.on_data.call("a" * 32, 32, nil) |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + assert_includes(error.message, "exceeds 64 bytes") |
| 24 | + end |
| 25 | + |
| 26 | + def test_on_data_accepts_a_body_that_stops_at_the_limit |
| 27 | + bounded = bounded_body(max_bytes: 64) |
| 28 | + |
| 29 | + 2.times do |
| 30 | + bounded.on_data.call("a" * 32, 32, nil) |
| 31 | + end |
| 32 | + |
| 33 | + assert_equal("a" * 64, bounded.response_for(stub_response(body: "")).body) |
| 34 | + end |
| 35 | + |
| 36 | + def test_response_for_rejects_an_over_limit_body_when_the_adapter_did_not_stream |
| 37 | + bounded = bounded_body(max_bytes: 64) |
| 38 | + |
| 39 | + error = assert_raises(too_large_error) do |
| 40 | + bounded.response_for(stub_response(body: "a" * 65)) |
| 41 | + end |
| 42 | + |
| 43 | + assert_includes(error.message, "exceeds 64 bytes") |
| 44 | + end |
| 45 | + |
| 46 | + def test_response_for_falls_back_to_the_response_body_when_the_adapter_did_not_stream |
| 47 | + bounded = bounded_body(max_bytes: 64) |
| 48 | + |
| 49 | + assert_equal("a" * 64, bounded.response_for(stub_response(body: "a" * 64)).body) |
| 50 | + end |
| 51 | + |
| 52 | + def test_response_for_coerces_a_non_string_body |
| 53 | + bounded = bounded_body(max_bytes: 64) |
| 54 | + |
| 55 | + assert_equal("", bounded.response_for(stub_response(body: nil)).body) |
| 56 | + end |
| 57 | + |
| 58 | + def test_response_for_carries_the_status_through |
| 59 | + bounded = bounded_body(max_bytes: 64) |
| 60 | + |
| 61 | + assert_equal(404, bounded.response_for(stub_response(status: 404, body: "")).status) |
| 62 | + end |
| 63 | + |
| 64 | + # Served over a real socket rather than WebMock: WebMock hands back the stubbed body verbatim |
| 65 | + # whatever its `Content-Encoding`, so the adapter's inflater never runs and a stubbed version |
| 66 | + # of this test would assert the opposite of what it looks like. |
| 67 | + def test_the_cap_counts_decompressed_bytes |
| 68 | + bounded = bounded_body(max_bytes: 1024 * 1024) |
| 69 | + compressed = gzip("a" * (5 * 1024 * 1024)) |
| 70 | + |
| 71 | + assert_operator(compressed.bytesize, :<, 1024 * 1024, "the compressed body must fit under the cap") |
| 72 | + |
| 73 | + serving_gzip(compressed) do |url| |
| 74 | + assert_raises(too_large_error) do |
| 75 | + Faraday.new.get(url) do |req| |
| 76 | + req.options.on_data = bounded.on_data |
| 77 | + req.options.timeout = 5 |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + private |
| 84 | + |
| 85 | + def gzip(content) |
| 86 | + io = StringIO.new |
| 87 | + |
| 88 | + writer = Zlib::GzipWriter.new(io) |
| 89 | + writer.write(content) |
| 90 | + writer.close |
| 91 | + |
| 92 | + io.string |
| 93 | + end |
| 94 | + |
| 95 | + def serving_gzip(body) |
| 96 | + # Other test files load `webmock/minitest`, which blocks real connections process-wide. |
| 97 | + # This is the one place that needs the adapter's own request path. |
| 98 | + WebMock.disable! if defined?(WebMock) |
| 99 | + |
| 100 | + server = TCPServer.new("127.0.0.1", 0) |
| 101 | + thread = Thread.new do |
| 102 | + socket = server.accept |
| 103 | + |
| 104 | + loop do |
| 105 | + line = socket.gets |
| 106 | + |
| 107 | + break if line.nil? || line == "\r\n" |
| 108 | + end |
| 109 | + |
| 110 | + socket.write( |
| 111 | + "HTTP/1.1 200 OK\r\n" \ |
| 112 | + "Content-Type: application/json\r\n" \ |
| 113 | + "Content-Encoding: gzip\r\n" \ |
| 114 | + "Content-Length: #{body.bytesize}\r\n" \ |
| 115 | + "Connection: close\r\n\r\n", |
| 116 | + ) |
| 117 | + socket.write(body) |
| 118 | + |
| 119 | + socket.close |
| 120 | + rescue IOError, SystemCallError |
| 121 | + nil |
| 122 | + end |
| 123 | + |
| 124 | + # Scoped to start after both are assigned, so the cleanup below never runs against `nil` |
| 125 | + # and a failure to open the socket surfaces as itself rather than as a `NoMethodError`. |
| 126 | + begin |
| 127 | + yield("http://127.0.0.1:#{server.addr[1]}/") |
| 128 | + ensure |
| 129 | + thread.kill |
| 130 | + server.close |
| 131 | + end |
| 132 | + ensure |
| 133 | + WebMock.enable! if defined?(WebMock) |
| 134 | + end |
| 135 | + |
| 136 | + def bounded_body(max_bytes:) |
| 137 | + OAuth.const_get(:BoundedBody).new(max_bytes: max_bytes) |
| 138 | + end |
| 139 | + |
| 140 | + def too_large_error |
| 141 | + OAuth.const_get(:BoundedBody)::TooLargeError |
| 142 | + end |
| 143 | + |
| 144 | + def stub_response(body:, status: 200) |
| 145 | + Struct.new(:status, :body).new(status, body) |
| 146 | + end |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | +end |
0 commit comments