|
138 | 138 | }
|
139 | 139 | )
|
140 | 140 | end
|
| 141 | + |
| 142 | + it "does not send the x-batch-validation header when :batch_validation is not provided" do |
| 143 | + resp = { |
| 144 | + "data": [ |
| 145 | + { "id": "ae2014de-c168-4c61-8267-70d2662a1ce1" }, |
| 146 | + { "id": "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb" } |
| 147 | + ] |
| 148 | + } |
| 149 | + |
| 150 | + allow(resp).to receive(:body).and_return(resp) |
| 151 | + allow(HTTParty).to receive(:send).and_return(resp) |
| 152 | + |
| 153 | + Resend::Batch.send([{ from: "me" }]) |
| 154 | + |
| 155 | + expect(HTTParty).to have_received(:send).with( |
| 156 | + :post, |
| 157 | + "#{Resend::Request::BASE_URL}emails/batch", |
| 158 | + { |
| 159 | + headers: { |
| 160 | + "Content-Type" => "application/json", |
| 161 | + "Accept" => "application/json", |
| 162 | + "Authorization" => "Bearer re_123", |
| 163 | + "User-Agent" => "resend-ruby:#{Resend::VERSION}", |
| 164 | + }, |
| 165 | + body: [{ from: "me" }].to_json |
| 166 | + } |
| 167 | + ) |
| 168 | + end |
| 169 | + |
| 170 | + it "sends the x-batch-validation header when :batch_validation is set to permissive" do |
| 171 | + resp = { |
| 172 | + "data": [ |
| 173 | + { "id": "ae2014de-c168-4c61-8267-70d2662a1ce1" } |
| 174 | + ], |
| 175 | + "errors": [ |
| 176 | + { "index": 1, "message": "Invalid email address" } |
| 177 | + ] |
| 178 | + } |
| 179 | + |
| 180 | + allow(resp).to receive(:body).and_return(resp) |
| 181 | + allow(HTTParty).to receive(:send).and_return(resp) |
| 182 | + |
| 183 | + Resend::Batch.send([{ from: "me" }], options: { batch_validation: "permissive" }) |
| 184 | + |
| 185 | + expect(HTTParty).to have_received(:send).with( |
| 186 | + :post, |
| 187 | + "#{Resend::Request::BASE_URL}emails/batch", |
| 188 | + { |
| 189 | + headers: { |
| 190 | + "Content-Type" => "application/json", |
| 191 | + "Accept" => "application/json", |
| 192 | + "Authorization" => "Bearer re_123", |
| 193 | + "User-Agent" => "resend-ruby:#{Resend::VERSION}", |
| 194 | + "x-batch-validation" => "permissive" |
| 195 | + }, |
| 196 | + body: [{ from: "me" }].to_json |
| 197 | + } |
| 198 | + ) |
| 199 | + end |
| 200 | + |
| 201 | + it "sends the x-batch-validation header when :batch_validation is set to strict" do |
| 202 | + resp = { |
| 203 | + "data": [ |
| 204 | + { "id": "ae2014de-c168-4c61-8267-70d2662a1ce1" }, |
| 205 | + { "id": "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb" } |
| 206 | + ] |
| 207 | + } |
| 208 | + |
| 209 | + allow(resp).to receive(:body).and_return(resp) |
| 210 | + allow(HTTParty).to receive(:send).and_return(resp) |
| 211 | + |
| 212 | + Resend::Batch.send([{ from: "me" }], options: { batch_validation: "strict" }) |
| 213 | + |
| 214 | + expect(HTTParty).to have_received(:send).with( |
| 215 | + :post, |
| 216 | + "#{Resend::Request::BASE_URL}emails/batch", |
| 217 | + { |
| 218 | + headers: { |
| 219 | + "Content-Type" => "application/json", |
| 220 | + "Accept" => "application/json", |
| 221 | + "Authorization" => "Bearer re_123", |
| 222 | + "User-Agent" => "resend-ruby:#{Resend::VERSION}", |
| 223 | + "x-batch-validation" => "strict" |
| 224 | + }, |
| 225 | + body: [{ from: "me" }].to_json |
| 226 | + } |
| 227 | + ) |
| 228 | + end |
| 229 | + |
| 230 | + it "handles response with errors array in permissive mode" do |
| 231 | + resp = { |
| 232 | + "data": [ |
| 233 | + { "id": "ae2014de-c168-4c61-8267-70d2662a1ce1" } |
| 234 | + ], |
| 235 | + "errors": [ |
| 236 | + { |
| 237 | + "index": 1, |
| 238 | + "message": "The 'to' field must be a valid email address" |
| 239 | + } |
| 240 | + ] |
| 241 | + } |
| 242 | + |
| 243 | + params = [ |
| 244 | + { |
| 245 | + |
| 246 | + |
| 247 | + "text": "testing", |
| 248 | + "subject": "Hey", |
| 249 | + }, |
| 250 | + { |
| 251 | + |
| 252 | + "to": ["invalid-email"], |
| 253 | + "text": "testing", |
| 254 | + "subject": "Hello", |
| 255 | + }, |
| 256 | + ] |
| 257 | + |
| 258 | + allow_any_instance_of(Resend::Request).to receive(:perform).and_return(resp) |
| 259 | + |
| 260 | + result = Resend::Batch.send(params, options: { batch_validation: "permissive" }) |
| 261 | + |
| 262 | + expect(result[:data].length).to eq 1 |
| 263 | + expect(result[:errors]).not_to be_nil |
| 264 | + expect(result[:errors].length).to eq 1 |
| 265 | + expect(result[:errors][0][:index]).to eq 1 |
| 266 | + expect(result[:errors][0][:message]).to include("valid email address") |
| 267 | + end |
141 | 268 | end
|
142 | 269 | end
|
0 commit comments