|
| 1 | +require "./spec_helper" |
| 2 | + |
| 3 | +module X |
| 4 | + describe ResponseHandler do |
| 5 | + it "handles a successful response" do |
| 6 | + response = HTTP::Client::Response.new(200, "{\"status\":\"success\"}") |
| 7 | + handler = ResponseHandler.new |
| 8 | + result = handler.handle(response) |
| 9 | + result.should eq({"status" => "success"}) |
| 10 | + end |
| 11 | + |
| 12 | + it "raises a BadRequestError for a 400 response" do |
| 13 | + response = HTTP::Client::Response.new(400, "Bad Request") |
| 14 | + handler = ResponseHandler.new |
| 15 | + expect_raises BadRequestError, "400 Bad Request - Bad Request" do |
| 16 | + handler.handle(response) |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + it "raises a AuthenticationError for a 401 response" do |
| 21 | + response = HTTP::Client::Response.new(401, "Unauthorized") |
| 22 | + handler = ResponseHandler.new |
| 23 | + expect_raises AuthenticationError, "401 Unauthorized - Unauthorized" do |
| 24 | + handler.handle(response) |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + it "raises a ForbiddenError for a 403 response" do |
| 29 | + response = HTTP::Client::Response.new(403, "Forbidden") |
| 30 | + handler = ResponseHandler.new |
| 31 | + expect_raises ForbiddenError, "403 Forbidden - Forbidden" do |
| 32 | + handler.handle(response) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + it "raises a NotFoundError for a 404 response" do |
| 37 | + response = HTTP::Client::Response.new(404, "Not Found") |
| 38 | + handler = ResponseHandler.new |
| 39 | + expect_raises NotFoundError, "404 Not Found - Not Found" do |
| 40 | + handler.handle(response) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + it "raises a TooManyRequestsError for a 429 response" do |
| 45 | + response = HTTP::Client::Response.new(429, "Too Many Requests") |
| 46 | + handler = ResponseHandler.new |
| 47 | + expect_raises TooManyRequestsError, "429 Too Many Requests - Too Many Requests" do |
| 48 | + handler.handle(response) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + it "raises a ClientError for a 499 response" do |
| 53 | + response = HTTP::Client::Response.new(499, "Client Error") |
| 54 | + handler = ResponseHandler.new |
| 55 | + expect_raises ClientError, "499 Client Error - Client Error" do |
| 56 | + handler.handle(response) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + it "raises a ServerError for a 500 response" do |
| 61 | + response = HTTP::Client::Response.new(500, "Internal Server Error") |
| 62 | + handler = ResponseHandler.new |
| 63 | + expect_raises ServerError, "500 Server Error - Internal Server Error" do |
| 64 | + handler.handle(response) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + it "raises a ServerError for a 503 response" do |
| 69 | + response = HTTP::Client::Response.new(503, "Service Unavailable") |
| 70 | + handler = ResponseHandler.new |
| 71 | + expect_raises ServiceUnavailableError, "503 Service Unavailable - Service Unavailable" do |
| 72 | + handler.handle(response) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + it "raises an Error for an unknown response" do |
| 77 | + response = HTTP::Client::Response.new(600, "Unknown") |
| 78 | + handler = ResponseHandler.new |
| 79 | + expect_raises Error, "600 Unknown Response - Unknown" do |
| 80 | + handler.handle(response) |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments