|
58 | 58 | end |
59 | 59 |
|
60 | 60 | describe "logging the response" do |
61 | | - let(:response) do |
62 | | - HTTP::Response.new( |
63 | | - version: "1.1", |
64 | | - status: 200, |
65 | | - headers: { content_type: "application/json" }, |
66 | | - body: '{"success": true}', |
67 | | - request: HTTP::Request.new(verb: :get, uri: "https://example.com") |
| 61 | + context "with a string body" do |
| 62 | + let(:response) do |
| 63 | + HTTP::Response.new( |
| 64 | + version: "1.1", |
| 65 | + status: 200, |
| 66 | + headers: { content_type: "application/json" }, |
| 67 | + body: '{"success": true}', |
| 68 | + request: HTTP::Request.new(verb: :get, uri: "https://example.com") |
| 69 | + ) |
| 70 | + end |
| 71 | + |
| 72 | + it "logs the response with body" do |
| 73 | + feature.wrap_response(response) |
| 74 | + |
| 75 | + expected = <<~OUTPUT |
| 76 | + ** INFO ** |
| 77 | + < 200 OK |
| 78 | + ** DEBUG ** |
| 79 | + Content-Type: application/json |
| 80 | +
|
| 81 | + {"success": true} |
| 82 | + OUTPUT |
| 83 | + assert_equal expected, logdev.string |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + context "with a streaming body" do |
| 88 | + let(:chunks) { %w[{"suc cess" :true}] } |
| 89 | + let(:stream) do |
| 90 | + fake( |
| 91 | + readpartial: proc { chunks.shift or raise EOFError }, |
| 92 | + close: nil, |
| 93 | + closed?: true |
| 94 | + ) |
| 95 | + end |
| 96 | + let(:body) { HTTP::Response::Body.new(stream) } |
| 97 | + let(:response) do |
| 98 | + HTTP::Response.new( |
| 99 | + version: "1.1", |
| 100 | + status: 200, |
| 101 | + headers: { content_type: "application/json" }, |
| 102 | + body: body, |
| 103 | + request: HTTP::Request.new(verb: :get, uri: "https://example.com") |
| 104 | + ) |
| 105 | + end |
| 106 | + |
| 107 | + it "does not consume the body" do |
| 108 | + wrapped = feature.wrap_response(response) |
| 109 | + |
| 110 | + assert_nil wrapped.body.instance_variable_get(:@streaming) |
| 111 | + end |
| 112 | + |
| 113 | + it "logs body chunks as they are streamed" do |
| 114 | + wrapped = feature.wrap_response(response) |
| 115 | + wrapped.body.to_s |
| 116 | + |
| 117 | + assert_includes logdev.string, '{"suc' |
| 118 | + assert_includes logdev.string, 'cess"' |
| 119 | + assert_includes logdev.string, ":true}" |
| 120 | + end |
| 121 | + |
| 122 | + it "preserves the full body content" do |
| 123 | + wrapped = feature.wrap_response(response) |
| 124 | + |
| 125 | + assert_equal '{"success":true}', wrapped.body.to_s |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + context "when logger level is above debug" do |
| 130 | + let(:feature) do |
| 131 | + logger = Logger.new(logdev) |
| 132 | + logger.formatter = ->(severity, _, _, message) { format("** %s **\n%s\n", severity, message) } |
| 133 | + logger.level = Logger::INFO |
| 134 | + |
| 135 | + HTTP::Features::Logging.new(logger: logger) |
| 136 | + end |
| 137 | + |
| 138 | + let(:stream) do |
| 139 | + fake( |
| 140 | + readpartial: proc { raise EOFError }, |
| 141 | + close: nil, |
| 142 | + closed?: true |
| 143 | + ) |
| 144 | + end |
| 145 | + let(:body) { HTTP::Response::Body.new(stream) } |
| 146 | + let(:response) do |
| 147 | + HTTP::Response.new( |
| 148 | + version: "1.1", |
| 149 | + status: 200, |
| 150 | + headers: { content_type: "text/plain" }, |
| 151 | + body: body, |
| 152 | + request: HTTP::Request.new(verb: :get, uri: "https://example.com") |
| 153 | + ) |
| 154 | + end |
| 155 | + |
| 156 | + it "does not wrap the body" do |
| 157 | + wrapped = feature.wrap_response(response) |
| 158 | + |
| 159 | + assert_same response, wrapped |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + describe "BodyLogger" do |
| 165 | + let(:logger) do |
| 166 | + logger = Logger.new(logdev) |
| 167 | + logger.formatter = ->(severity, _, _, message) { format("** %s **\n%s\n", severity, message) } |
| 168 | + logger |
| 169 | + end |
| 170 | + |
| 171 | + it "passes through chunks and logs them" do |
| 172 | + chunks = %w[hello world] |
| 173 | + stream = fake(readpartial: proc { chunks.shift or raise EOFError }) |
| 174 | + |
| 175 | + body_logger = HTTP::Features::Logging::BodyLogger.new(stream, logger) |
| 176 | + |
| 177 | + assert_equal "hello", body_logger.readpartial |
| 178 | + assert_equal "world", body_logger.readpartial |
| 179 | + assert_raises(EOFError) { body_logger.readpartial } |
| 180 | + assert_includes logdev.string, "hello" |
| 181 | + assert_includes logdev.string, "world" |
| 182 | + end |
| 183 | + |
| 184 | + it "exposes the underlying connection" do |
| 185 | + connection = Object.new |
| 186 | + stream = fake( |
| 187 | + readpartial: proc { raise EOFError }, |
| 188 | + connection: connection |
68 | 189 | ) |
| 190 | + |
| 191 | + body_logger = HTTP::Features::Logging::BodyLogger.new(stream, logger) |
| 192 | + |
| 193 | + assert_same connection, body_logger.connection |
69 | 194 | end |
70 | 195 |
|
71 | | - it "logs the response" do |
72 | | - feature.wrap_response(response) |
| 196 | + it "uses the stream as connection when stream has no connection method" do |
| 197 | + stream = fake(readpartial: proc { raise EOFError }) |
73 | 198 |
|
74 | | - expected = <<~OUTPUT |
75 | | - ** INFO ** |
76 | | - < 200 OK |
77 | | - ** DEBUG ** |
78 | | - Content-Type: application/json |
| 199 | + body_logger = HTTP::Features::Logging::BodyLogger.new(stream, logger) |
79 | 200 |
|
80 | | - {"success": true} |
81 | | - OUTPUT |
82 | | - assert_equal expected, logdev.string |
| 201 | + assert_same stream, body_logger.connection |
83 | 202 | end |
84 | 203 | end |
85 | 204 | end |
0 commit comments