|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2024, by Samuel Williams. |
| 5 | + |
| 6 | +require "cloudflare/request_error" |
| 7 | + |
| 8 | +describe Cloudflare::RequestError do |
| 9 | + with ".error_string_for with hash containing :error key" do |
| 10 | + it "returns the error value" do |
| 11 | + value = {error: "Something went wrong"} |
| 12 | + result = Cloudflare::RequestError.error_string_for(value) |
| 13 | + |
| 14 | + expect(result).to be == "Something went wrong" |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + with ".error_string_for with hash containing :errors key" do |
| 19 | + it "returns comma-separated error messages" do |
| 20 | + value = { |
| 21 | + errors: [ |
| 22 | + {message: "First error"}, |
| 23 | + {message: "Second error"}, |
| 24 | + {message: "Third error"} |
| 25 | + ] |
| 26 | + } |
| 27 | + result = Cloudflare::RequestError.error_string_for(value) |
| 28 | + |
| 29 | + expect(result).to be == "First error, Second error, Third error" |
| 30 | + end |
| 31 | + |
| 32 | + it "handles empty errors array" do |
| 33 | + value = {errors: []} |
| 34 | + result = Cloudflare::RequestError.error_string_for(value) |
| 35 | + |
| 36 | + expect(result).to be == "" |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + with ".error_string_for with plain string" do |
| 41 | + it "returns inspected value" do |
| 42 | + value = "Plain error message" |
| 43 | + result = Cloudflare::RequestError.error_string_for(value) |
| 44 | + |
| 45 | + expect(result).to be == "\"Plain error message\"" |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + with ".error_string_for with other types" do |
| 50 | + it "returns inspected value for integer" do |
| 51 | + value = 42 |
| 52 | + result = Cloudflare::RequestError.error_string_for(value) |
| 53 | + |
| 54 | + expect(result).to be == "42" |
| 55 | + end |
| 56 | + |
| 57 | + it "returns inspected value for nil" do |
| 58 | + value = nil |
| 59 | + result = Cloudflare::RequestError.error_string_for(value) |
| 60 | + |
| 61 | + expect(result).to be == "nil" |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + with "#initialize" do |
| 66 | + it "creates error with hash containing :error key" do |
| 67 | + request = "GET /api/v1/test" |
| 68 | + value = {error: "Authentication failed"} |
| 69 | + error = Cloudflare::RequestError.new(request, value) |
| 70 | + |
| 71 | + expect(error.message).to be == "GET /api/v1/test: Authentication failed" |
| 72 | + expect(error.value).to be == value |
| 73 | + end |
| 74 | + |
| 75 | + it "creates error with hash containing :errors key" do |
| 76 | + request = "POST /api/v1/test" |
| 77 | + value = { |
| 78 | + errors: [ |
| 79 | + {message: "Field 'name' is required"}, |
| 80 | + {message: "Field 'email' is invalid"} |
| 81 | + ] |
| 82 | + } |
| 83 | + error = Cloudflare::RequestError.new(request, value) |
| 84 | + |
| 85 | + expect(error.message).to be == "POST /api/v1/test: Field 'name' is required, Field 'email' is invalid" |
| 86 | + expect(error.value).to be == value |
| 87 | + end |
| 88 | + |
| 89 | + it "creates error with plain string" do |
| 90 | + request = "DELETE /api/v1/test" |
| 91 | + value = "Resource not found" |
| 92 | + error = Cloudflare::RequestError.new(request, value) |
| 93 | + |
| 94 | + expect(error.message).to be == "DELETE /api/v1/test: \"Resource not found\"" |
| 95 | + expect(error.value).to be == value |
| 96 | + end |
| 97 | + end |
| 98 | +end |
0 commit comments